简体   繁体   English

Angular 和 Stripe 订阅保存付款细节并创建订阅

[英]Angular and Stripe subscriptions save payment details and create the subscription

I following the Stripe subscription documentation .我遵循Stripe 订阅文档 I having difficulty converting the JavaScript code to TypeScript.我很难将 JavaScript 代码转换为 TypeScript。 In the stripe documentation, they are passing an object to createPaymentMethod function.在条带文档中,他们将一个对象传递给 createPaymentMethod 函数。 In Angular, it doesn't allow it pass because the object has to have 3 properties but in the documentation, they are only passing one property.在 Angular 中,它不允许它通过,因为对象必须具有 3 个属性,但在文档中,它们只传递一个属性。

Error:错误:

error TS2345: Argument of type '{ card: any; }' is not assignable to parameter of type '{ card: any; isPaymentRetry: any; invoiceId: any; }'.
  Type '{ card: any; }' is missing the following properties from type '{ card: any; isPaymentRetry: any; invoiceId: any; }': isPaymentRetry, invoiceId

Code:代码:

export class StripeSubsriptionCheckoutComponent implements AfterViewChecked {
  card: any;

  constructor(private http: HttpClient) {}

  ngAfterViewChecked() {
    // Set up Stripe.js and Elements to use in checkout form
    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 = window['elements'].create("card", { style: style });
    console.log('card element', cardElement)
    cardElement.mount("#card-element");
    cardElement.on('change', this.showCardError);
    this.card = cardElement;
  }

  showCardError(event) {
    let displayError = document.getElementById('card-errors');
    if (event.error) {
      displayError.textContent = event.error.message;
    } else {
      displayError.textContent = '';
    }
  }

  savePaymentDetails() {
    var card = this.card
      console.log('savePaymentDetails')
      const latestInvoicePaymentIntentStatus = localStorage.getItem(
        'latestInvoicePaymentIntentStatus'
      );
      console.log('lastestInvoicePay', latestInvoicePaymentIntentStatus)

      if (latestInvoicePaymentIntentStatus === 'requires_payment_method') {
          console.log('fjd')
          const invoiceId = localStorage.getItem('latestInvoiceId');
          const isPaymentRetry = true;

         
          // Create new payment method & retry payment on invoice with new payment method
          this.createPaymentMethod({
              card,
              isPaymentRetry,
              invoiceId
          });
      }
      else {
         this.createPaymentMethod({ card});
      }
  }

  createPaymentMethod({ card, isPaymentRetry, invoiceId}) {
      console.log('createPaymentMethod')
      console.log(card)
        // Set up payment method for recurring usage
      let billingName = 'tom'
      console.log(billingName)

      window['stripe'].createPaymentMethod({ 
        type: 'card',
        card: card,
        billing_details: {
            name: billingName
        }
      })
      .then((result) => {
          console.log('line 90', result)
          if(result.error) {
            //  displayError(result);
          }
          else {
              console.log('line 95: else statement')
              if(isPaymentRetry) {
                console.log('line 97', result)
              }
              else {
                  console.log('result', result)
                  // Create the subscription
                  this.createSubscription({
                      customerId: result,
                      paymentMethodId: result.paymentMethod.id,
                    priceId: result
                  })
              }
            }
          
      })

  }

  createSubscription({ customerId, paymentMethodId, priceId }) {
      console.log('createSubscriptiton')
  }
}

The problem is the lines问题是线条

else {
  this.createPaymentMethod({ card});
}

Here you are calling this.createPaymentMethod with an object that only contains card , but the method signature requires card , isPaymentRetry and invoiceId :在这里,您使用仅包含card的对象调用this.createPaymentMethod ,但方法签名需要cardisPaymentRetryinvoiceId

createPaymentMethod({ card, isPaymentRetry, invoiceId}) {

You'll want to change this to either pass in values for the missing parameters or specify that isPaymentRetry and invoiceId are optional, for example:您需要将其更改为传递缺失参数的值或指定isPaymentRetryinvoiceId是可选的,例如:

createPaymentMethod({ card: any, isPaymentRetry?: bool, invoiceId?: string }) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建支付意图等于 $ 0 的条带或其他替代方案的订阅? - How to create subscriptions with payment intent equal to $ 0 in stripe or some alternative to it? 分条订阅-计费返回返回subscription_payment_intent_requires_action的3D安全卡号 - Stripe Subscriptions - Charing a 3D Secure card number returning subscription_payment_intent_requires_action 如何在不支付条纹的情况下保存卡详细信息? - how to save card details without making payment on stripe? 无法在 Stripe 上创建订阅 - Can't create subscriptions on Stripe 将客户详细信息附加到 Stripe Payment Intent - Attach customer details to Stripe Payment Intent 条纹付款快递js:缺少交易详细信息 - Stripe payment express js :transaction details is missing 使用 Stripe Payment Intent 自定义支付流程传递额外的客户详细信息 - Passing extra customer details with Stripe Payment Intent custom payment flow stripe 使用 nodeJS 创建支付意图 - stripe create payment intent with nodeJS 首次成功付款后,条纹计费订阅不收取“后续费用” - Stripe billing Subscription not charging the “subsequent charges” after initial successful Payment Stripe Checkout 订阅付款失败重定向到过期链接页面 - Stripe Checkout Subscription Payment failure redirects to Expire Link Page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM