首次成功付款后,条纹计费订阅不收取“后续费用”

时间:2019-09-16 14:24:59

标签: javascript php stripe-payments recurring-billing

我已遵循this文档来实施“条纹定期付款”,这对于非3d-Secure卡和启用3d-Secure的卡都可以正常工作。对于首次付款,我从Stripe获得了3d-Secure Payment身份验证模式,以使用3D-Secure Test卡对付款进行身份验证。

订阅已创建,Stripe为我生成了发票。一切似乎都还可以,但是3D-Secure Enabled(条纹测试卡)的第一次付款成功完成,但是我将间隔设置为1天(用于测试)的后续付款被Stripe“失败”了。据我了解,Stripe再次需要客户身份验证才能继续订阅。


this中,他们说

  

遇到3D Secure时,配置您的结算设置以发送   指向您客户的托管链接以完成流程。

那完全可以,但是如果第一次付款成功并且开始订阅,他们为什么还要再次要求身份验证步骤?我已经浏览了所有相关文档,但没有发现任何可以防止这种情况的东西。

如果客户不检查他/她的电子邮件以返回我的应用程序以验证付款怎么办? 或

代码有什么问题吗?我的意思是,Stripe应该已经保存了付款方式,即卡号,因此每次新的计费周期开始时(每月/每半年),都不需要客户对后续收费进行认证。

我已经在互联网上搜索并找到并回答说

  

您正在使用不支持3ds的stripe.createToken   身份验证。您需要将客户端代码迁移到   stripe.createPaymentMethod确实支持它

是吗?任何回应表示赞赏

我的Payment.js

// set your stripe publishable key
var stripe = Stripe('pk_test_key');
var elements = stripe.elements();
var style = {
    base: {
        color: '#32325d',
        lineHeight: '18px',
        fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
        fontSmoothing: 'antialiased',
        fontSize: '16px',
        '::placeholder': {
            color: '#aab7c4'
        }
    },
    invalid: {
        color: '#fa755a',
        iconColor: '#fa755a'
    }
};

var cardNumber = elements.create('cardNumber', {
    style: style
});
cardNumber.mount('#cardNumber');
var cardExpiry = elements.create('cardExpiry', {
    style: style
});
cardExpiry.mount('#cardExpiry');
var cardCvc = elements.create('cardCvc', {
    style: style
});
cardCvc.mount('#cardCVC');

var cardholderName = $('#custName').val();
var amount = $('#amount').val();

var cardButton = document.getElementById('makePayment');
cardButton.addEventListener('click', function(ev) {
     // alert();
    ev.preventDefault();




    stripe.createToken(cardNumber).then(function(result) {

    if (result.error) {


    } else {
      //$body.addClass("loading");
      fetch('process.php', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ 
            token_id: result.token.id          
            })
      }).then(function(result) {


        // Handle server response (see Step 3)
        result.json().then(function(json) {
          handleServerResponse(json);
          //alert();
        })
      });
    }
  });
});
function handleServerResponse(response) {
            if (response.error) {
                // Show error from server on payment form
            } else if (response.requires_action) {
                // Use Stripe.js to handle required card action
                var action = response.next_action;
                if (action && action.type == 'redirect_to_url') {
                    window.location = action.redirect_to_url.url;
                }

                handleAction(response);

            } else {
                console.log("3D" + response);
            }
}

        function handleAction(response) {
           var paymentIntentSecret = response.payment_intent_client_secret;
            stripe.handleCardPayment(paymentIntentSecret).then(function(result) {
              if (result.error) {
                // Display error.message in your UI.
              } else {
                // The payment has succeeded. Display a success message.
                alert('Payment succeeded!');
              }
});

}

Process.php

<?php 

require_once('stripe-php/init.php');   
\Stripe\Stripe::setApiKey('sk_test_key');

$json_str = file_get_contents('php://input');

$json_obj = json_decode($json_str);
 //print_r($json_obj->token_id);die;

  //$intent = null;
try {
    if (isset($json_obj->token_id)) {
     //Create Customer   
    $customer = \Stripe\Customer::create([
    "email" => "test@gmail.com",
    "name" => "Haroon",  
    "source" => $json_obj->token_id,
]);
    //create product
    $product = \Stripe\Product::create([
    'name' => 'Water',
    'type' => 'service',
]);
    //create a plan
    $plan = \Stripe\Plan::create([
      'product' => $product->id,
      'nickname' => 'Water',
      'interval' => 'day',
      'currency' => 'eur',
      'amount' => 1200.00,
    ]);   

    //add subscription to stripe
    $subscription = \Stripe\Subscription::create([

        'customer' => $customer->id,
        'items' => [[
        "plan" => $plan->id],],
        'expand' => ['latest_invoice.payment_intent'],
]);    
}

      $intent = \Stripe\PaymentIntent::retrieve($subscription->latest_invoice->payment_intent->id);

      $subscription = \Stripe\Subscription::retrieve($subscription->id);
    //   $intent->confirm();
    // }
    generatePaymentResponse($intent,$subscription);
  }catch (\Stripe\Error\Base $e) {
    # Display error on client
    echo json_encode([
      'error' => $e->getMessage()
    ]);
  }  

   function generatePaymentResponse($intent,$subscription) {
    if ($intent->status == 'requires_action' && $subscription->status == 'incomplete' &&
        $intent->next_action->type == 'use_stripe_sdk' ) {
        # Tell the client to handle the action

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

        # The payment didn’t need any additional actions and completed!
        # Handle post-payment fulfillment
        echo json_encode([
            'success' => true
        ]);
    } else if ($intent->status == 'requires_payment_method' && $subscription->status == 'incomplete') {
        echo "Subscription failed";

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

1 个答案:

答案 0 :(得分:-1)

如果您要为已经存在的卡执行此操作,那么我将在一天前执行此操作。请参阅本文档https://stripe.com/docs/payments/3d-secure

这表明您需要创建paymentIntent并需要确认此付款意图。在3ds中也有测试卡4242 4242 42424242。该卡不需要3ds的任何身份验证。在下面的链接中提供测试卡

https://stripe.com/docs/payments/3d-secure/web#three-ds-cards