将客户添加到条纹中的订阅时出错

时间:2019-04-15 15:12:25

标签: javascript php html stripe-payments

我正在尝试创建客户并创建订阅。

  • 但是当我这样做时,我会出错。
  • 我尝试通过node.js和php方式进行操作。通过node.js
  • 我已经安装了stripe,并通过php安装了composer和stripe / stripe-php。

当我使用javascript方式进行操作时,一切都会找到并且很方便,没有出现错误,但是当我转到Stripe仪表板时,在测试模式数据中没有新创建的客户或订阅。

<?php // Create a customer using a Stripe token

// If you're using Composer, use Composer's autoload:
require_once('vendor/autoload.php');

// Be sure to replace this with your actual test API key
// (switch to the live key later)
\Stripe\Stripe::setApiKey("pk_test_pjUIBAynO2WA1gA7woSLBny3");

try
{
  $customer = \Stripe\Customer::create([
    'email' => $_POST['stripeEmail'],
    'source'  => $_POST['stripeToken'],
  ]);

  $subscription = \Stripe\Subscription::create([
    'customer' => $customer->id,
    'items' => [['plan' => 'gold']],
  ]);

  if ($subscription->status != 'incomplete')
  {
    header('Location: thankyou.html');
  }
  else
  {
    header('Location: payment_failed.html');
    error_log("failed to collect initial payment for subscription");
  }
  exit;
}
catch(Exception $e)
{
  header('Location:oops.html');
  error_log("unable to sign up customer:" . $_POST['stripeEmail'].
    ", error:" . $e->getMessage());
}

这是html文件

<form action="/create_subscription.php" id="start" method="POST">
                <script
                  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                  data-key="pk_test_pjUIBAynO2WA1gA7woSLBny3"

                  data-name="500 a month package"
                  data-description="Subscription will be billed every 30 days"
                  data-amount="50000"
                  data-label="Get Started!">
                </script>
                <script>
                  var stripe = require("stripe")("pk_test_pjUIBAynO2WA1gA7woSLBny3");
                  var stripeToken = req.body.stripeToken;
                  stripe.subscriptions.create({
                  customer: id,
                  items: [
                    {
                       plan: "prod_EtGDP9qICl6tvW",
                       interval: "month"
                      },
                  ]

                      }  , function(err, subscription) {
                  // asynchronously called
                  function(err, custumor){
                  if(err){
                    res.send({
                      success: false,
                      message:'Error'
                    });
                  } else {
                    const { id } = customer;
                  }
           };
              stripe.customers.create({
                email:"jenny@rosen.com",
                source: stripeToken,
               },
                function(err, custumor){
                  if(err){
                    res.send({
                      success: false,
                      message:'Error'
                    });
                  } else {
                    const { id } = customer;
                  }
                });
                </script>
              </form>

0 个答案:

没有答案