4

我对如何使用 Stripe 处理订阅 + 费用支付有点困惑,

这是我得到的:

HTML:

<form id="req" action="/thank-you" method="post">

<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="pk_test_RrE21sY9Xbtwq9ZvbKPpp0LJ"
data-name="Wizard.Build Hosting Package"
data-description=""
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto"
data-currency="usd">
</script>
...

感谢页面:

require_once('lib-stripe/init.php');

// create api key
\Stripe\Stripe::setApiKey("sk_TESTING");

// create customer
$customerO =\Stripe\Customer::create(array(
  "email" => $_POST["e"]
));
$customer = $customerO->jsonSerialize();

//var_dump($customer);

//create subscription (Package Bronze, Silver or Gold)
$subscriptionO = \Stripe\Subscription::create(array(
  "customer" => $customer["id"],
  "items" => array(
    array(
        "plan" => $_POST["pack"],
    ),
  )
));
$subscription = $subscriptionO->jsonSerialize();

//var_dump($subscription);

//create invoice (3rdparty cost)
$invoiceO = \Stripe\Invoice::create(array(
    "customer" => $customer["id"],
    "amount" =>$p3price,
    "currency" => "usd",
    "description" => "3rdp Cost",
    "subscription" => $subscription["id"]
));
$invoice = $invoiceO->jsonSerialize();

//var_dump($invoice);

我显然错过了整个过程的工作方式......

我将填充电子邮件字段,但我如何在该弹出表单中请求每月重新发生的设置费 + 订阅?

理想情况下,工作流程如下:

带有表单的我的网站页面:用户填写姓名、电子邮件、商品名称[需要此元数据]、商品价格、包裹名称[需要此元数据]、包裹价格

点击提交按钮,Stripe弹出表单出现预填充电子邮件,用户同意打包每月付款+商品价格,用户输入卡信息,用户提交条纹表单,

用户现在需要支付第 1 个月的包裹 + 商品价格,每个月都会自动为包裹付款。

返回包含所有元数据、价格、包裹等的 url,带有密码或某种安全表单字段,然后我从返回的 url 数据中自动处理订单,

请帮助,感谢社区!

4

3 回答 3

4

我想我可以指导您完成该过程,您必须遵循非常简单的步骤并检查一切是否完美。

  1. 使用 Stripe 仪表板或 Stripe API 创建计划(下面的 PHP 代码示例)

    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $plan = \Stripe\Plan::create([
      'product' => {'name' => 'Plan name'},
      'nickname' => 'Plan code',
      'interval' => 'month',
      'currency' => 'usd',
      'amount' => 10,
    ]);
    
  2. 使用 JS/PHP 等在 Stripe 中创建客户并保存 id 以供参考。(下​​面的 PHP 代码示例)

    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $customer = \Stripe\Customer::create([
      'email' => 'customer@example.com',
    ]);
    

    此调用的响应将为在条带创建的客户提供一个 json

    {
      "id": "<UNIQUE ID>",
      ...
    }
    

    您需要将 id 保存在变量或数据库中

  3. 为客户订阅计划。(下面的 PHP 示例代码)

    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $subscription = \Stripe\Subscription::create([
      'customer' => '<UNIQUE ID>',
      'items' => [['plan' => '<PLAN ID>']],
    ]);
    

您可以在官方 Stripe 文档中找到更多详细信息

于 2018-02-15T12:24:35.113 回答
1

简而言之,您应该实现基本的 Stripe Charge API 流程(创建客户、创建费用等),并在付款返回的承诺中调用订阅计划 ID,将其附加到用户(成功收费)。

您不必以编程方式创建此计划(除非您需要为每个客户动态创建独特的计划),您可以通过松弛仪表板的用户计划部分来执行此操作。您只需要打电话给客户“订阅”您给定的计划。

首先创建一个用户,成功创建此用户后,“charge”调用传入该客户的唯一 ID,稍后当收费成功时(在您的成功回调中,而不是错误一个)调用您之前创建的 subscribe 调用计划编号。

当您使用测试模式并使用此设置付款时,请查看付款详细信息(确保仪表板切换到测试模式),您将看到此费用并且用户已附加订阅计划,将会发生什么是条带将尝试在此订阅期结束内再次收费(您可以通过仪表板、每周/每月计划等进行设置),最好使用 stripe 自己的工具进行测试(您不必等待此期间进行测试,查看它在相关的 api 文档部分)

如果您也需要代码方面的帮助,请告诉我,但是一旦您了解了我在上面解释的这个简单流程,就可以很简单地按照 api 文档进行操作。

仪表板网址:https ://dashboard.stripe.com/ (打开“查看测试数据”以查看您的测试付款详情)

API 调用参考: https ://stripe.com/docs/api#create_customer https://stripe.com/docs/api#create_charge https://stripe.com/docs/api#create_subscription(您不必通过 api 执行此操作,从仪表板执行此操作,更容易)

看看我的测试代码中的代码段,我使用 NodeJS,但您也可以在前端将它与您的 php 项目一起使用,只需在入门文档中查找在前端设置条带

    stripe.customers.create({
      description: "NR user twitter: " + req.body.twusername + ", job title being paid for: " + req.body.jobRawTitle,
      source: req.body.token,
      email: req.body.email
    }, function(err, customer) {
      if (err) {
          // bad things
          console.log("DEBUG payment charge error: " + JSON.stringify(err));
      } else {
        //update user with given Email
        // Charge the user's card:
        return stripe.charges.create({
          amount: 29900, //the last 2 0s are cents
          currency: "usd",
          customer: customer.id,
          description: "Jobs 1 month paid for nextreality job titled:" + req.body.jobRawTitle
        }, function(err, charge) {
          if (err) {
              // bad things
              console.log("DEBUG payment charge error: " + JSON.stringify(err) );
               console.log("charge: "+ charge);
          } else {
              // successful charge
              return stripe.subscriptions.create({
                customer: customer.id,
                items: [
                  {
                    plan: "jobs_monthly",
                  },
                ],
              }, function(err, subscription) {
                // asynchronously called
                console.log("DEBUG payment subscription error: " + err + ' subs:' + JSON.stringify(subscription) );
                return Response(req, res, {});

              });
          }
        });
      }
    });
于 2018-02-15T22:08:26.437 回答
1

生成计划代码或使用管理面板并将计划ID放入订阅方法..确保您是否使用管理面板生成计划ID然后它将仅在实时密钥中运行并使用它运行两个密钥的代码生成计划ID..

$plan = \Stripe\Plan::create([
  'product' => {'name' => 'Basic Product'},
  'nickname' => 'Basic Monthly',
  'interval' => 'month',
  'currency' => 'usd',
  'amount' => 0,
]); 
于 2018-02-11T18:27:40.993 回答