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

时间:2019-12-10 19:48:58

标签: node.js stripe-payments

我正在尝试遵循以下链接https://stripe.com/docs/payments/accept-a-payment中显示的示例。

我在客户端有以下代码

 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")

代替此

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

因为我想对UI进行一些操作。根据链接中发布的代码 我应该执行以下操作

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.
    }
 }
});
});

但是,在上面的示例中,在payment_method中,传递了卡对象,而在我的代码中情况并非如此。如何在stripe.confirmCardPayment(clientSecret,{        Payment_method:{card:card}

2 个答案:

答案 0 :(得分:8)

关于how to call stripe.createToken() when you aren't using a card element,存在类似的问题。

根据the Stripe documentation for createToken

您希望标记数据的cardNumber元素。如果适用,元素会从您在同一Element实例上创建的其他元素中提取数据进行标记化-您只需提供一个Element作为参数即可。

现在,尤其是在这种情况下,the section for confirmCardPayment说:

通过在数据参数中传递卡或 cardNumber 元素作为Payment_method [card],将stripe.confirmCardPayment与来自Element的支付数据一起使用。

基本上,您只需要将cardNumber元素传递到payment_method["card"],它将从您创建的其他元素中提取数据。

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

答案 1 :(得分:1)

我最终使用了这段代码

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 = '';
    }
});