该客户没有附加的付款来源或默认付款方式

时间:2020-03-04 16:59:54

标签: php stripe-subscriptions

我正在尝试在应用程序上集成Stripe订阅系统。我正在关注以下教程:https://stripe.com/docs/billing/subscriptions/set-up-subscription

但是我遇到以下错误:

Stripe \ Exception \ InvalidRequestException 该客户没有附加的付款来源或默认付款方式。

客户端代码:

       <script src="https://js.stripe.com/v3/"></script>

                       <script>
                        var stripe = Stripe('<?= \app\nplugins\settings\Stripe::i()->key ?>');
                        var elements = stripe.elements();

                           </script>

                           <form id="subscription-form">
  <div id="card-element" class="MyCardElement">
    <!-- Elements will create input elements here -->
  </div>

  <!-- We'll put the error messages in this element -->
  <div id="card-errors" role="alert"></div>
  <button type="button" id="btnSubmit">Subscribe</button>
</form>
    <script>
        var style = {
  base: {
    color: "#32325d",
    fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
    fontSmoothing: "antialiased",
    fontSize: "16px",
    "::placeholder": {
      color: "#aab7c4"
    }
  },
  invalid: {
    color: "#fa755a",
    iconColor: "#fa755a"
  }
};

var cardElement = elements.create("card", { style: style });
cardElement.mount("#card-element");

$("#btnSubmit").on("click",function(event){
    console.log("Submit Clicked");
    stripe.createPaymentMethod({
      type: 'card',
      card: cardElement,
      billing_details: {
        email: '<?= $order->od_shp_email ?>',
      },
    }).then(stripePaymentMethodHandler);
    console.log("Submit Clicked2");
})


card.addEventListener('change', function(event) {
  var displayError = document.getElementById('card-errors');
  if (event.error) {
    displayError.textContent = event.error.message;
  } else {
    displayError.textContent = '';
  }
});



var form = document.getElementById('subscription-form');

form.addEventListener('submit', function(event) {
  // We don't want to let default form submission happen here,
  // which would refresh the page.

});
function stripePaymentMethodHandler(result, email) {
  if (result.error) {
    // Show error in payment form
  } else {
    // Otherwise send paymentMethod.id to your server
    fetch('<?= yii\helpers\Url::to(["process"]) ?>', {
      method: 'post',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        email: '<?= $order->od_shp_email ?>',
        payment_method: result.paymentMethod.id
      }),
    }).then(function(result) {
     return result.json();
    }).then(function(customer) {
      // The customer has been created
    });
  }
}
        </script>

服务器端代码:

\Stripe\Stripe::setApiKey('sk_test_Profa0D8IS1Pw3P4IJyEVkPs');

        $customer = \Stripe\Customer::create([
          'email' => Yii::$app->request->post("email"),
          'payment_method' => Yii::$app->request->post("payment_method"),          

          'invoice_settings' => [
            'default_payment_method' => Yii::$app->request->post("payment_method"),
          ],
        ]);

        $subscription = \Stripe\Subscription::create([
            'customer' => $customer->id,

            'items' => [
              [
                'plan' => \app\nplugins\settings\Stripe::i()->subscription_plan,
              ],
            ],
            'expand' => ['latest_invoice.payment_intent'],
        ]);

0 个答案:

没有答案