Stripe API (PHP) – Create a charge with default payment method card

huangapple go评论86阅读模式
英文:

Stripe API (PHP) - Create a charge with default payment method card

问题

我正在使用Stripe PHP API尝试创建一笔收款:

$chargeData = [
    "amount" => $amountTaxed,
    "currency" => $this->settings->currency_code,
    "source" => 'tok_visa', // 卡片令牌
    // 'customer' => $this->user->stripe_id,
    "description" => __('general.add_funds') . ' (自动充值) @' . $this->user->username,
    'metadata' => [
        'user' => $this->user->stripe_id,
        'amount' => $amountTaxed,
        'taxes' => $this->settings->tax_on_wallet ? $this->user->taxesPayable() : null,
        'type' => 'deposit'
    ]
];
$charge = \Stripe\Charge::create($chargeData);

下面是添加到客户帐户的付款方法卡片详细信息:

Stripe API (PHP) – Create a charge with default payment method card

我知道如何通过卡号获取卡片令牌:

$token = \Stripe\Token::create([
    'card' => [
        'number' => '4242424242424242',
        'exp_month' => 7,
        'exp_year' => 2024,
        'cvc' => '314',
    ],
]);

如果我手动使用卡号\Stripe\Token::create获取令牌,Stripe的Stripe\Charge::create方法可以正常工作。所以,我的唯一问题是获取源/令牌。

我的意图是从现有付款方法的卡片中获取令牌。

如果有其他从客户的现有付款方法卡片收费的方法,请告诉我。

谢谢!

英文:

I am using Strip PHP API and trying to create a charge:

$chargeData = [
    "amount" => $amountTaxed,
    "currency" => $this->settings->currency_code,
    "source" => 'tok_visa', // Card token
    //'customer' => $this->user->stripe_id,
    "description" => __('general.add_funds') . ' (Auto-recharge) @' . $this->user->username,
    'metadata' => [
        'user' => $this->user->stripe_id,
        'amount' => $amountTaxed,
        'taxes' => $this->settings->tax_on_wallet ? $this->user->taxesPayable() : null,
        'type' => 'deposit'
    ]
];
$charge = \Stripe\Charge::create($chargeData);

Below is payment method card detail added in customer account

Stripe API (PHP) – Create a charge with default payment method card

I know how to get card token via card number

$token = \Stripe\Token::create([
    'card' => [
        'number' => '4242424242424242',
        'exp_month' => 7,
        'exp_year' => 2024,
        'cvc' => '314',
    ],
]);

If I get token manually using the card number \Stripe\Token::create, stripe's charge method \Stripe\Charge::create works fine. So my only problem is to get the source/token.

My intention is to get the token from existing payment method's card.

If there is any other way to charge from customer's existing payment methods's card, please let me know.

Thank you!

答案1

得分: 1

您正在使用较旧的集成路径。Stripe的收费API仍然有效,但不建议使用。我建议从他们的指南开始,构建一个集成,用于向客户收费并保存他们的付款信息以供以后收费。

英文:

You're using an older integration path. Stripe's Charges API still work but are not recommended. I recommend starting with their guide for building an integration that charges a customer and saves their payment details for later charges.

答案2

得分: 1

我在@LauraT的帮助下找到了解决方案。

$paymentIntent = PaymentIntent::create([
    'amount' => $amountTaxed, // 以分为单位的金额(例如,$10.00)
    'currency' => $this->settings->currency_code,
    'customer' => $this->user->stripe_id, // 用Stripe中的客户ID替换
    'payment_method' => $default_payment_method_id, // 用Stripe中的支付方法ID(Visa卡)替换
    'confirm' => true, // 设置为true以立即确认付款
]);
英文:

I found the solution with the help of @LauraT.

$paymentIntent = PaymentIntent::create([
    'amount' => $amountTaxed, // Amount in cents (e.g., $10.00)
    'currency' => $this->settings->currency_code,
    'customer' => $this->user->stripe_id, // Replace with the customer ID in Stripe
    'payment_method' => $default_payment_method_id, // Replace with the payment method ID (Visa card) in Stripe
    'confirm' => true, // Set to true to confirm the payment immediately
]);

huangapple
  • 本文由 发表于 2023年7月18日 05:35:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定