canMakePayment()在LOCALHOST

时间:2019-11-14 14:02:12

标签: angular stripe-payments

我正在尝试显示“付款”按钮,但是paymentRequest.canMakePayment()始终返回null。我正在chrome(版本73.0.3683.75)上的LOCALHOST上执行此代码,并且卡也保存在chrome上。

import { Component, OnInit } from '@angular/core';  
declare let Stripe;

@Component({
  selector: 'hts-card-details',
  templateUrl: './card-details.component.html'
})
  export class demo implements OnInit {
  card: any;
  stripe_value: any;
  elements: any;
  paymentRequest: any;
  prButton: any;
  constructor() { }

  ngOnInit() {
     this.displayStripeCard('pk_test_mMvKPSkT746S4xp6FMhMOk3W'); // for testing 
  }

  displayStripeCard(stripeKey) {
    this.stripe_value = Stripe(stripeKey);
    this.elements = this.stripe_value.elements();
    // Custom styling can be passed to options when creating an Element.
    this.card = this.elements.create('card', {style});

    // Mount the Card Element on the page.
    this.card.mount('#card-element');

    // Handle real-time validation errors from the card Element.
    this.card.addEventListener('change', function(event) {
      const displayError = document.getElementById('card-errors');
      if (event.error) {
        displayError.textContent = event.error.message;
      } else {
        displayError.textContent = '';
      }
    });

    // create payment method
    this.paymentRequest = this.stripe_value.paymentRequest({
      country: 'US',
      currency: 'usd',
      total: {
        label: 'Demo total',
        amount: 200,
      },
      requestPayerName: true,
      requestPayerEmail: true,
    });
     this.prButton = this.elements.create('paymentRequestButton', {
      paymentRequest: this.paymentRequest,
    });
    this.mountButton();

  }
  async mountButton() {
    await this.paymentRequest.canMakePayment().then(function (result) {
        debugger;
        console.log(result); // returns null
    });


  }
}

1 个答案:

答案 0 :(得分:0)

即使在本地环境中,您也必须使用 https。在“先决条件”下查看更多信息:https://stripe.com/docs/stripe-js/elements/payment-request-button