首页 文章

条纹付款流程

提问于
浏览
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弹出窗体显示预填充电子邮件,用户同意打包每月付款项目价格,用户输入卡信息,用户提交条形码,

用户现在收取第一个月的包裹物品价格,包裹将自动每月支付一次 .

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

请帮忙,谢谢社区!

3 回答

  • 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,
    ]);
    
    • 使用JS / PHP等在Stripe中创建客户并保存id以供参考 . (下面的PHP代码示例)
    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $customer = \Stripe\Customer::create([
      'email' => 'customer@example.com',
    ]);
    

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

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

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

    • 订阅客户以获取计划 . (下面的PHP示例代码)
    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $subscription = \Stripe\Subscription::create([
      'customer' => '<UNIQUE ID>',
      'items' => [['plan' => '<PLAN ID>']],
    ]);
    

    你可以在Official Stripe documentation找到更多细节

  • 3

    生成计划代码或使用管理面板并将计划ID放入订阅方法 . 确保如果您使用管理面板生成计划ID,那么它将仅在实时密钥中生成并使用代码生成计划ID,它运行两个密钥 .

    $plan = \Stripe\Plan::create([
      'product' => {'name' => 'Basic Product'},
      'nickname' => 'Basic Monthly',
      'interval' => 'month',
      'currency' => 'usd',
      'amount' => 0,
    ]);
    
  • 1

    在要点中,您应该实施基本的Stripe Charge API流程(创建客户,创建费用等),并在付款的退回承诺中调用订阅计划ID,将其附加到用户(成功收费) .

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

    首先创建一个用户,在成功创建此用户后,“收费”呼叫将传递给该客户的唯一ID,稍后当收费成功时(在您成功回调而不是错误1中)调用您之前创建的订阅呼叫计划ID .

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

    如果您需要有关代码的帮助,请告诉我,但是一旦您了解了我上面解释的这个简单流程,就可以非常直接地了解api文档 .

    仪表板URL:https://dashboard.stripe.com/(切换"view test data"开以查看测试付款详细信息)

    API调用ref: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, {});
    
                  });
              }
            });
          }
        });
    

相关问题