在Stripe中收费特定的客户卡

时间:2018-10-21 14:12:47

标签: javascript node.js stripe-payments

TLDR;我想允许客户使用自己选择的应用程序中以前使用过的/已保存的卡下订单。这要求能够指定对“以前”与客户关联的“哪张”卡进行收费。 This should be possible in theory according to the docs,但我在实践中没有运气。

好的...通话很便宜,这是代码:

// pseudo code of a customer placing an order for the first time
var stripe = require("stripe")(".....");

(async function(userId, cardToken) {
    // Create a Customer AND save the payment method
    const customer = await stripe.customers.create({
        source: cardToken.id, // e.g. 'tok_mastercard'  (retrieved from stripe.js)
        email: 'paying.user@example.com',
    });

    // Charge the Customer instead of the card:
    const charge = await stripe.charges.create({
        amount: 1000,
        currency: 'usd',
        customer: customer.id
    });

    // save customer id to DB
    await setCustomerIdToDatabase(userId, customer.id);

    // save Card ID (not the card token ID) to DB
    await addCustomerCardToDatabase(userId, cardToken.card.id); // <= note cardToken".card".id  e.g. card_kldlkns...

})(userId, cardToken /* <= provided by app*/);

到目前为止,一切都很好。 Stripe将指定令牌中的卡保存为新创建客户的默认付款方式,然后成功对其进行收费。但是后来我的痛苦开始了...

// pseudo code of a customer placing subsequent orders (WITH NEW CARDS)
var stripe = require("stripe")(".....");

(async function(userId, cardToken) {

    const customerId = await getCustomerIdFromDatabase(userId);

    // Create a Customer AND save the payment method
    await stripe.customers.createSource(
        customerId,
        { 
            source: cardToken.id, // e.g. 'tok_mastercard'  (retrieved from stripe.js)
        }
    );

    // Attempt to charge the newly added card instead the Customer:
    const charge = await stripe.charges.create({
        amount: 1000,
        currency: 'usd',
        customer: customerId,
        source: cardToken.card.id // <= note cardToken".card".id  e.g. card_kldlkns...
    });

    /////////ERROR TRHOWN: No such token: card_kldlkns...
    ///////// what? I just saved it with .customers.createSource and you said it was OK???

    /// unreachable code

    // save Card ID (not the card token ID) to DB
    await addCustomerCardToDatabase(userId, cardToken.card.id); // <= note cardToken".card".id  e.g. card_kldlkns...

})(userId, cardToken /* <= provided by app*/);

惨败...这也是

// pseudo code of a customer placing subsequent orders (WITH SAVED CARDS)
var stripe = require("stripe")(".....");

(async function(userId, savedCardId) {

    const customerId = await getCustomerIdFromDatabase(userId);

    // Attempt to charge the newly added card instead the Customer:
    const charge = await stripe.charges.create({
        amount: 1000,
        currency: 'usd',
        customer: customerId,
        source: savedCardId  //  e.g card_kldlkns...
    });

    /////////ERROR TRHOWN: No such token: card_kldlkns...
    ///////// what??????

})(userId, savedCardId /* <= provided by app*/);

我什么都不来? The docs clearly says Stripe wants the card.id and the customer.id back,如果我想为特定的卡收费。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

结果证明这根本不是问题。

存在错误的情况是代码调用了错误的Stripe API方法