如何在Stripe中创建变量订阅

时间:2016-03-18 03:58:14

标签: php api variables stripe-payments

我使用条纹来捕获信用卡。我有我的表单,因此它们是可变输入,并且它工作得很好。我将信息从<input>传递到charge.php文件并成功捕获。

当我尝试使用此信息创建订阅时,我无法使用可变金额。我只能创建具有设定金额的订阅。

我希望使用$finalamount来设置订阅金额。 我可以将nameidamount相同。

如何根据用户输入的内容创建包含自定义amountnameid的变量订阅?

<?php

require_once('init.php');

\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;

\Stripe\Plan::create(array(
  "amount" => $finalamount, //this does not work. It only works if there is a present amount. 
  "interval" => "month",
  "name" => "Green Plan",
  "currency" => "usd",
  "id" => "green")
);

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


// Charge the Customer instead of the card
\Stripe\Charge::create(array(
  "amount" => $finalamount, // amount in cents, again
  "currency" => "usd",
  "customer" => $customer->id)
);

?>

1 个答案:

答案 0 :(得分:0)

您需要删除此代码

\Stripe\Charge::create(array(
  "amount" => $finalamount, // amount in cents, again
  "currency" => "usd",
  "customer" => $customer->id)
);

因为当您创建客户指定计划时,您的客户会自动订阅并收费。

希望它有所帮助:)