【问题标题】:Stripe PaymentIntent PHP client_secret resolved to nullStripe PaymentIntent PHP client_secret 解析为 null
【发布时间】:2020-08-07 13:24:32
【问题描述】:

我正在尝试将 Stripe 集成到一个 php 项目中,除了支付意图 client_secret 始终为 null 并且我实际上知道为什么但不知道如何修复它,我正在使用一个 javascript 文件其中有一个用于创建付款意图的提交按钮的侦听器。问题是侦听器在创建之前尝试获取 json 数据,client_secret 值为 null,因为我在创建付款意图后设置它的值,如何解决这个问题?任何建议都会有所帮助,谢谢。

这是我写的代码: 意图.php:

$intent= \Stripe\PaymentIntent::create(
array(
'amount' => $price + ($price * $tva),
'currency' => 'EUR',
    'setup_future_usage' => 'off_session',

)
);


$intentcls=$intent->client_secret;  

intent.js:

var form = document.getElementById('payment-form');

form.addEventListener('submit', function(event) {

    var response = fetch('/secret').then(function(response) {
        return response.json();
    }).then(function(responseJson) {
        var clientSecret = responseJson.client_secret;
        stripe.confirmCardPayment(
            clientSecret,
            {
                payment_method: {card: card}
            }
        ).then(function(result) {
            if (result.error) {
                var errorElement = document.getElementById('card-errors');
                errorElement.textContent = result.error.message;
            } else {
                var errorElement = document.getElementById('card-errors');
                errorElement.textContent = 'Paiement effectué avec succées';
                stripeTokenHandler(result.token);
            }
        });

secret.php:

echo json_encode(array('clientsecret' => $intentcls));

【问题讨论】:

    标签: php json stripe-payments


    【解决方案1】:

    您需要重新安排您的逻辑,以确保在您向后端的获取请求之前(或期间)创建 PaymentIntent。

    如果您已经知道金额,则可以在页面加载时创建 PaymentIntent。

    更好的解决方案是在调用/secret 时创建它,然后在对Stripe API 的创建调用完成后返回client_secret

    secret.php 可能是:

    $intent = \Stripe\PaymentIntent::create(
      array(
        'amount' => $amount, // calculate amount before this block
        'currency' => 'EUR',
        'setup_future_usage' => 'off_session',
      )
    );  
    
    echo json_encode(array('client_secret' => $intent->client_secret));
    
    // perhaps store the ID in your database here
    

    【讨论】:

    • 你的答案一定是对的,我也是通过内联php文件中的js代码解决的,谢谢。
    猜你喜欢
    • 2021-02-04
    • 2020-10-23
    • 1970-01-01
    • 2019-11-20
    • 2019-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多