0

我的 Stripe 工作得很好。客户捐款后,会创建一个新订阅,并且效果很好 - 除非 Stripe 识别出电子邮件并说“输入验证码”。

如果客户这样做,出于某种原因,不会创建新订阅并且不会向客户收费。

这是我的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, )
);


?>

任何想法为什么当 Stripe 识别出用户并且他“登录”时它不允许我创建订阅?

在 Stripe 日志中,我收到了 400 错误:

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

但是绝对没有制定计划……啊!

4

1 回答 1

1

您的请求失败的原因是,如果用户返回时使用相同的电子邮件地址并想要注册相同的计划,那么您已经有一个具有该名称的现有计划,

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

所以你的调用\Stripe\Plan::create将返回一个错误,并导致你的其余调用在这里失败。

您可以在计划 ID 中添加诸如唯一 ID 或时间之类的内容。

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

人们通常处理此问题的其他一些方法是:

  • 创建一个 1 美元的计划,然后在创建订阅时调整数量。因此,数量为 100 的 1 美元的月度计划将收取 100 美元的月费。

  • 在您的应用程序中存储客户将支付的金额。为您的客户订阅每月 0 美元的计划。使用 webhook 监听invoice.created事件。让您的 webhook 处理程序每​​月为余额添加一个发票项目。

于 2017-09-10T18:13:29.393 回答