简体   繁体   English

如何在条带中的付款意图示例中自定义付款方式

[英]How to customize the payment method in payment intent example in stripe

I am trying to follow the example shown in the following link https://stripe.com/docs/payments/accept-a-payment .我正在尝试按照以下链接https://stripe.com/docs/payments/accept-a-payment 中显示的示例进行操作。

I have the following code in the client side我在客户端有以下代码

 var cardNumber = elements.create('cardNumber', {
    placeholder:'',
    style: style
});
var cardexpiry = elements.create('cardExpiry',{
    placeholder:'',
    style:style
});
var cardCVV = elements.create('cardCvc',{
    placeholder:'',
    style:style
});


// Add an instance of the card Element into the `card-element` <div>.
cardNumber.mount('#card-element');
cardexpiry.mount("#card-expiry")
cardCVV.mount("#card-cvv")

instead of this而不是这个

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

Because the I wanted to some UI manipulation.因为我想要一些 UI 操作。 According to the code posted in the link I should do the following根据链接中发布的代码,我应该执行以下操作

var submitButton = document.getElementById('submit');

submitButton.addEventListener('click', function(ev) {
 stripe.confirmCardPayment(clientSecret, {
   payment_method: {card: card}
 }).then(function(result) {
 if (result.error) {
  // Show error to your customer (e.g., insufficient funds)
  console.log(result.error.message);
 } else {
  // The payment has been processed!
  if (result.paymentIntent.status === 'succeeded') {
    // Show a success message to your customer
    // There's a risk of the customer closing the window before callback
    // execution. Set up a webhook or plugin to listen for the
    // payment_intent.succeeded event that handles any business critical
    // post-payment actions.
    }
 }
});
});

However in the example above in the payment_method the card object is passed, which is not the case in my code.但是,在上面的 payment_method 示例中,传递了卡对象,而在我的代码中并非如此。 How can I pass my card number and exp/date as well CVC separately in the stripe.confirmCardPayment(clientSecret, { payment_method: {card: card}如何在 stripe.confirmCardPayment(clientSecret, { payment_method: {card: card} 中分别传递我的卡号和 exp/date 以及 CVC

There was a similar question about how to call stripe.createToken() when you aren't using a card element .有一个关于如何在不使用card element 时调用stripe.createToken()的类似问题。

According to the Stripe documentation for createToken :根据createToken 的 Stripe 文档

The cardNumber Element you wish to tokenize data from.您希望从中标记数据的 cardNumber 元素。 If applicable, the Element pulls data from other elements you've created on the same instance of Elements to tokenize—you only need to supply one Element as the parameter.如果适用,Element 会从您在同一 Elements 实例上创建的其他元素中提取数据以进行标记化——您只需提供一个 Element 作为参数。

Now, for this case in particular, the section for confirmCardPayment says:现在,特别是对于这种情况, confirmCardPayment 部分说:

Use stripe.confirmCardPayment with payment data from an Element by passing a card or cardNumber Element as payment_method[card] in the data argument.通过在 data 参数中将 card 或cardNumber元素作为 payment_method[card] 传递,将 stripe.confirmCardPayment 与来自元素的支付数据一起使用。

Basically you just have to pass the cardNumber element to payment_method["card"] and it will pull the data from the other elements you've created.基本上,您只需将cardNumber元素传递给payment_method["card"] ,它就会从您创建的其他元素中提取数据。

...
stripe.confirmCardPayment(clientSecret, {
  payment_method: {card: cardNumber}
})
...

I ended up using this code我最终使用了这段代码

var stripe = Stripe('#YOUR KEY');
var elements = stripe.elements();

/// STYLE HERE
var style = {
    base: {
        fontSize: '16px',
        color: "#32325d",
        '::placeholder': {
            color: '#CFD7E0',
            fontSize: '12px'
        }
    }
};

// Create an instance of the card Element.
var card = elements.create('card', {
    hidePostalCode: true,
    placeholder: '',
    style: style,



});
card.mount('#card-element');

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM