普通网友 2019-08-02 12:10
浏览 70
已采纳

条纹 - 付款意图(3d安全问题)

I did implement Payments intents on my website and it works fine with the testing card 4242 4242 4242 4242, but on any other card that require 3d secure i fall on error "Invalid PaymentIntent status"

Code I use is standard code from https://stripe.com/docs/payments/payment-intents/quickstart#manual-confirmation-flow enriched with some code to manage mysql, emails, metadata etc.

Where do I go wrong? Thanks in Advance.

simplified js code conected to index.php

var stripe = Stripe('pk_test_xxx');
var elements = stripe.elements();
var cardElement = elements.create('card', {style: style});

cardElement.mount('#card-element');

var cardholderName = document.getElementById('cardholder-name');
var cardButton = document.getElementById('card-button');
var amount = $('#amount').val();

cardButton.addEventListener('click', function(ev) {
    ev.preventDefault();
    stripe.createPaymentMethod('card', cardElement, {
        billing_details: {name: cardholderName.value}
    }).then(function(result) {

    if (result.error) {


    } else {
      $body.addClass("loading");
      fetch('https://test.com/server.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ 
            payment_method_id: result.paymentMethod.id, 
            amount: amount
            })
      }).then(function(result) {


        // Handle server response (see Step 3)
        result.json().then(function(json) {
          handleServerResponse(json);
        })
      });
    }
  });
});

function handleServerResponse(response) {
  if (response.error) {

  } else if (response.requires_action) {

    stripe.handleCardAction(
      response.payment_intent_client_secret
    ).then(function(result) {
      if (result.error) {

      } else {
        // The card action has been handled
        // The PaymentIntent can be confirmed again on the server
        fetch('https://test.com/server.php', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ 
            payment_method_id: result.paymentMethod.id, 
             amount: amount


          })
        }).then(function(confirmResult) {
            console.log(confirmResult);
          return confirmResult.json();
        }).then(handleServerResponse);
      }
    });
  } else {


  }
}

simplified code on server.php

<?php

  # vendor using composer
  require_once('stripe6400/init.php');

  \Stripe\Stripe::setApiKey('sk_test_xxx');

  header('Content-Type: application/json');

  # retrieve json from POST body
  $json_str = file_get_contents('php://input');
  $json_obj = json_decode($json_str);

    $paymentid = $json_obj->payment_method_id;
    $amount = $json_obj->amount;

  $intent = null;
  try {
    if (isset($json_obj->payment_method_id)) {
      # Create the PaymentIntent
      $intent = \Stripe\PaymentIntent::create([
        'payment_method' => $json_obj->payment_method_id,
        'amount' => $json_obj->amount,
        'payment_method_types' => ["card"],
        'currency' => 'gbp',
        'confirmation_method' => 'manual',
        'confirm' => true,
      ]);
    }
    if (isset($json_obj->payment_intent_id)) {
      $intent = \Stripe\PaymentIntent::retrieve(
        $json_obj->payment_intent_id
      );
      $intent->confirm();
    }
    generatePaymentResponse($intent);
  } catch (\Stripe\Error\Base $e) {
    # Display error on client
    echo json_encode([
      'error' => $e->getMessage()
    ]);
  }

  function generatePaymentResponse($intent) {
    if ($intent->status == 'requires_action' &&
        $intent->next_action->type == 'use_stripe_sdk') {

      echo json_encode([
        'requires_action' => true,
        'payment_intent_client_secret' => $intent->client_secret
      ]);
    } else if ($intent->status == 'succeeded') {


Stripe\Customer::create([
    "email" => $email,
    "name" => $customer_name,
    "source" => "tok_visa" // obtained with Stripe.js
]);


      echo json_encode([
        "success" => true
      ]);



    } else {
      # Invalid status
      http_response_code(500);
      echo json_encode(['error' => 'Invalid PaymentIntent status']);
    }
  }
?>
  • 写回答

1条回答 默认 最新

  • douxing9641 2019-08-02 13:16
    关注

    It looks like you might have the same error I just had. The status of the response from stripe is requires_source_action not requires_action so your if statement falls through to Invalid PaymentIntent status.

    // change this
    // $intent->status == 'requires_action'
    
    // to this
    $intent->status == 'requires_source_action'
    

    In my case I'm checking for both so my code is ready for when I do update the stripe SDK.

    https://stripe.com/docs/payments/payment-intents/quickstart#confirm-again (line 33 in the code)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊