简体   繁体   English

带卡 ID 的条纹创建令牌

[英]stripe creating token with card id

I'm currently trying to create charges with the server side stripe api.我目前正在尝试使用服务器端条带 api 创建费用。

But i'm facing a problem;但我面临一个问题; I'm using two ways to proceed with the paiement:我正在使用两种方式进行付款:

  • either the user can pay using the stripe element => I'm therefore using the generated token ('tok_somethingId') to effectuate the paiement要么用户可以使用 stripe 元素付款 => 因此我使用生成的令牌 ('tok_somethingId') 来实现付款

  • or, if the user already added some cards on is account he can select on of them in a list => The server then use the card id ('card_somethingId') and the customer id ('cus_smoethingId')或者,如果用户已经在帐户上添加了一些卡,他可以在列表中选择它们 => 服务器然后使用卡 ID ('card_somethingId') 和客户 ID ('cus_smoethingId')

I was wondering if there was a way to generate a token with a card id and a customer id in order to use this token to create the charge instead of using the card id and the customer id to charge the user.我想知道是否有办法生成带有卡 ID 和客户 ID 的令牌,以便使用此令牌来创建费用,而不是使用卡 ID 和客户 ID 向用户收费。

I already tried with https://stripe.com/docs/api/node#create_card_token but it doesn't seem to work.我已经尝试过https://stripe.com/docs/api/node#create_card_token但它似乎不起作用。

stripe.tokens.create({
  card: cardId,
  customer: customerId
}, function(err, token) {
  // do smthg here with the token
});

This give me this error message这给了我这个错误信息在此处输入图片说明

If you are doing payment with stored card then no need to get token,如果您使用存储卡付款,则无需获取令牌,

1- Create customer on stripe 1- 在条带上创建客户

$customer = \Stripe\Customer::create([
    'email' => $customer_email,
]);

$response = ['status' => 'success', 'response' => $customer];

when you have created customer then you have customer_id当您创建客户时,您就有了 customer_id

if(isset($response ['response']['id']))
    $response ['customer_id']  = $response ['response']['id'];

2 - you can add card on stripe by the customer id and card token 2 - 您可以通过客户 ID 和卡令牌在条带上添加卡

$customer = \Stripe\Customer::retrieve($customer_id);
$creditCard = $customer->sources->create(array("source" => $cardToken));

$response = ['status' => 'success', 'response' => $creditCard];

Now you have card id like this现在你有这样的卡号

"id": "card_1D4plsDExLRkbD8k1UWdqwIr"

3- you can store multiple cards on customer and also can have retreive 3-您可以在客户上存储多张卡,也可以检索

$cards = \Stripe\Customer::retrieve($customer_id)->sources->all(array(
    "object" => "card"
));

4 -you can payment via card card that is stored on customer account 4 - 您可以通过存储在客户帐户中的卡付款

$params = [
    'currency' => 'USD',
    'amount' => $total_amount * 100,  
// converting dollars to cents
    'description' => $description, //it may be blank
    'customer' => $customer_id,
    "card" => $card_id'
];

$transaction = \Stripe\Charge::create($params); 

$response = ['status' => 'success', 'response' => $transaction['id']];

Here we are not using 'source' parameter because it is used when we are payment via card token.这里我们没有使用“source”参数,因为它在我们通过卡令牌付款时使用。

You can not create a new token for an existing card as this would not make sense.您不能为现有卡创建新令牌,因为这没有意义。 The card is already saved on the customer and you can charge it.该卡已保存在客户身上,您可以对其进行充电。

The easiest solution here is likely to do the reverse and save the card on a customer when you get a token tok_XXXX .这里最简单的解决方案可能是反过来,当您获得令牌tok_XXXX时将卡保存在客户tok_XXXX This way, you always charge a card by passing the customer parameter as the customer id cus_XXXX and the source parameter as the card id card_XXXX .这样,您始终通过将customer参数作为客户 ID cus_XXXXsource参数作为卡 ID card_XXXX来对卡card_XXXX

Otherwise, you need to handle this dynamically so that you know if you are getting a token (tok_XXXX) or a card id (card_XXXX) and pass different parameters based on that decision.否则,您需要动态处理此问题,以便您知道是获取令牌 (tok_XXXX) 还是卡 ID (card_XXXX) 并根据该决定传递不同的参数。 Your front-end code should know which case you ended up in.您的前端代码应该知道您最终处于哪种情况。

just pass the values in the body ,只需传递正文中的值

[{"key":"amount","value":"1000","description":""},{"key":"currency","value":"usd","description":""},{"key":"customer","value":"cus_ID","description":""},{"key":"description","value":"\"Charge for jenny.rosen@example.com\"","description":""},{"key":"card","value":"card_ID","description":""}]

this working for me这对我有用

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

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