【问题标题】:How to create variable subscriptions in Stripe如何在 Stripe 中创建变量订阅
【发布时间】:2016-07-04 16:55:40
【问题描述】:

我正在使用条纹来捕获信用卡。我有我的表格,所以它们是可变输入,而且效果很好。我将<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)
);

?>

【问题讨论】:

  • 您在尝试代码时遇到的错误是什么?
  • 我没有收到错误,只是没有处理。只有在有一定数量时才会处理。
  • 您应该启用 PHP 错误记录。默认情况下,这是禁用的,它将停止处理。所以不处理 = 一个没有向您显示的错误。

标签: php api variables stripe-payments


【解决方案1】:

您需要删除此代码

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

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

希望对你有帮助:)

【讨论】:

  • 您好,谢谢您的来信。不幸的是,删除该行并没有帮助我处理订阅。订阅确实发布了,这很好。但是由于某种原因它删除了email,仍然没有amount。我认为$finalamount 会设置这个,但不是出于某种原因。
  • 我建议您记录 $finalamount,通过电子邮件检查它们是否有任何值。请告诉我您收到的任何错误。我也许可以帮助你,因为我自己已经在一个项目中实现了类似的功能。
猜你喜欢
  • 1970-01-01
  • 2020-09-18
  • 2014-04-08
  • 2015-03-02
  • 2020-04-23
  • 2017-08-28
  • 2021-10-24
  • 2017-12-23
  • 2016-08-04
相关资源
最近更新 更多