duandanxiu6965 2017-09-10 03:43
浏览 86

Stripe Checkout - 使用已识别条带用户的订阅问题

I have Stripe working great. Upon a customer's donation, a new Subscription is created, and it works great - except if Stripe recognizes the email and says, "Enter the verification code."

If the customer does that, for some reason, a new subscription is not created and the customer is not charged.

Here is my charge-monthly.php

<?php

require_once('init.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_**************");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$amount = $_POST['amount'];
$finalamount = $amount * 100;
$dollars = ".00";
$plan = "/month"; 
$dash = " - "; 
$monthlyplan = $amount .$dollars .$plan .$dash .$email; 


//Create monthly plan
$plan = \Stripe\Plan::create(array(
  "name" => $monthlyplan,
  "id" => $monthlyplan,
  "interval" => "month",
  "currency" => "usd",
  "amount" => $finalamount,
));


// Create a Customer
$customer = \Stripe\Customer::create(array(
  "source" => $token,
  "description" => "MONTHLY DONATION",
    "plan" => $monthlyplan, 
  "email" => $email, )
);


?>

Any ideas why when Stripe recognizes the user and he is "logged in" it does not allow me to create a subscription?

In the Stripe log, I receive this 400 error:

{
   "error": {
   "type": "invalid_request_error",
   "message": "Plan already exists."
   }
 }

But there definitely isn't a plan created... ah!

  • 写回答

1条回答 默认 最新

  • duanmanmian7589 2017-09-10 18:13
    关注

    The reason your request is failing is because if a user comes back with the same email address and wants to sign up for the same plan, you already have an existing plan with that name,

    $monthlyplan = $amount .$dollars .$plan .$dash .$email;

    so your call to \Stripe\Plan::create will return an error and cause the rest of your calls to fail here.

    You could add something like a unique id or time to your plan id.

    http://php.net/manual/en/function.time.php http://php.net/manual/en/function.uniqid.php

    Some other ways that folks typically handle this are:

    • Create a single plan for $1, and then adjust the quantity when creating your subscription. So a monthly plan for $1 with quantity of 100, would charge $100 month.

    • Store the amount that a customer will pay within your application. Subscribe your customers to a $0/month plan. Use webhooks to listen for invoice.created events. Have your webhook handler add an Invoice Item every month for the balance.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号