Stripe付款流程

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弹出窗口表单出现,预先填好电子邮件,用户同意包月付款+物品价格,用户输入卡信息, 用户提交Stripe表单,

用户现在被收取了第一个月的包费和物品价格,每个月自动进行付款。

返回一个带有所有元数据、价格、包等信息的URL,带有秘密代码或某种安全表单字段,然后我从返回的URL数据自动处理订单,

请帮助,感谢社区!


你想要他们的默认表单还是你自己的定制表单? - Lakindu Gunasekara
我需要他们的UI弹出付款窗口,但我需要在后端进行创建用户、订阅和发票的处理,只是不确定所有这些如何联系在一起... - Luc Laverdure
现在弹出窗口显示了,但后端处理没有工作。 - Luc Laverdure
你能否检查一下是否从他们的弹出窗口传递了卡片令牌?你似乎创建了客户、添加了订阅等,但在你的 PHP 代码中从未处理 source,即卡片令牌。 - Máté
我不希望客户输入超过1个表格,是否可以在弹出窗口中添加订阅功能?我找到了令牌ID,但我希望用户在单个表格上支付订阅费和设置费用,我该怎么做? - Luc Laverdure
显示剩余5条评论
3个回答

4

我认为我可以指导您完成这个过程,您需要遵循非常简单的步骤,并检查一切是否完美。

  1. Create a plan using Stripe dashboard or Stripe API (PHP code example below)

    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $plan = \Stripe\Plan::create([
      'product' => {'name' => 'Plan name'},
      'nickname' => 'Plan code',
      'interval' => 'month',
      'currency' => 'usd',
      'amount' => 10,
    ]);
    
  2. Create a customer in Stripe using the JS/PHP etc and save the id for reference.(PHP code example below)

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

    The response of this call will give a json for customer created at stripe

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

    You will need to persist the id in a variable or database

  3. Subscribe customer to for plan.(PHP example code below)

    \Stripe\Stripe::setApiKey("<YOUR SECRET KEY>");
    
    $subscription = \Stripe\Subscription::create([
      'customer' => '<UNIQUE ID>',
      'items' => [['plan' => '<PLAN ID>']],
    ]);
    
您可以在Stripe官方文档中了解更多有关IT技术的详细信息。

1
生成计划可以通过编码或使用管理面板完成,并将计划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,将其附加到用户上(成功收费)。您不必以编程方式创建此计划(除非您需要为每个客户动态创建唯一的计划),您可以通过Slack仪表板的用户计划部分完成。您只需要调用“订阅”将客户订阅到您指定的计划即可。首先创建一个用户,在成功创建此用户后,“charge”调用会传递此客户的唯一ID,稍后当收费成功时(与错误回调相反),使用先前创建的计划ID调用订阅调用。当您使用测试模式并使用此设置进行付款时,请查看付款详细信息(确保仪表板已切换到测试模式),您将看到此收费和用户已附加订阅计划,Stripe将在此订阅期结束之前再次尝试收费(您可以通过仪表板设置所有这些内容,每周/每月计划等),最好使用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即可。

    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, {});

              });
          }
        });
      }
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接