简体   繁体   English

使用 Stripe Payment Intent 自定义支付流程传递额外的客户详细信息

[英]Passing extra customer details with Stripe Payment Intent custom payment flow

I'm trying to integrate Stripe's custom payment flow following this tutorial: https://stripe.com/docs/payments/integration-builder我正在尝试按照本教程集成 Stripe 的自定义支付流程: https://stripe.com/docs/payments/integration-builder

This is what I tried这是我试过的

 // A reference to Stripe.js initialized with your real test publishable API key. var stripe = Stripe("pk_test.."); var formData = { 'email': document.getElementById("email").value, 'cardholder-name': document.getElementById("cardholder-name").value, 'user_id': document.getElementById("user_id").value }; // Disable the button until we have Stripe set up on the page document.querySelector("button").disabled = true; fetch("create.php", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(formData), }).then(function(result) { return result.json(); }).then(function(data) { var elements = stripe.elements(); var style = { base: { color: "#32325d", fontFamily: 'Arial, sans-serif', fontSmoothing: "antialiased", fontSize: "16px", "::placeholder": { color: "#32325d" } }, invalid: { fontFamily: 'Arial, sans-serif', color: "#fa755a", iconColor: "#fa755a" } }; var card = elements.create("card", { style: style }); // Stripe injects an iframe into the DOM card.mount("#card-element"); card.on("change", function (event) { // Disable the Pay button if there are no card details in the Element document.querySelector("button").disabled = event.empty; document.querySelector("#card-error").textContent = event.error? event.error.message: ""; }); var form = document.getElementById("payment-form"); form.addEventListener("submit", function(event) { event.preventDefault(); // Complete payment when the submit button is clicked payWithCard(stripe, card, data.clientSecret); }); }); // Calls stripe.confirmCardPayment // If the card requires authentication Stripe shows a pop-up modal to // prompt the user to enter authentication details without leaving your page. var payWithCard = function(stripe, card, clientSecret) { loading(true); stripe.confirmCardPayment(clientSecret, { payment_method: { card: card } }).then(function(result) { if (result.error) { // Show error to your customer showError(result.error.message); } else { // The payment succeeded. orderComplete(result.paymentIntent;id); } }); }; /* ------- UI helpers ------- */ // Shows a success message when the payment is complete var orderComplete = function(paymentIntentId) { loading(false). document.querySelector(".result-message a"),setAttribute( "href": "https.//dashboard.stripe;com/test/payments/" + paymentIntentId ). document.querySelector(".result-message").classList;remove("hidden"). document.querySelector("button");disabled = true; }; // Show the customer the error from Stripe if their card fails to charge var showError = function(errorMsgText) { loading(false). var errorMsg = document;querySelector("#card-error"). errorMsg;textContent = errorMsgText. setTimeout(function() { errorMsg;textContent = "", }; 4000); }. // Show a spinner on payment submission var loading = function(isLoading) { if (isLoading) { // Disable the button and show a spinner document.querySelector("button");disabled = true. document.querySelector("#spinner").classList;remove("hidden"). document.querySelector("#button-text").classList;add("hidden"). } else { document.querySelector("button");disabled = false. document.querySelector("#spinner").classList;add("hidden"). document.querySelector("#button-text").classList;remove("hidden"); } };
 <form id="payment-form"> <input type="hidden" id="user_id" value="<? echo $user_id; ?>"> <div class="form-group"> </div> <label> <input id="cardholder-name" class="field is-empty" placeholder="Cardholder Name" style="height: 50px;" /> </label> <label> <input class="field is-empty" type="email" name="email" id="email" placeholder="Your Email" style="height: 50px;" /> </label> <label> <div id="card-element" class="field form-control"><.--Stripe,js injects the Card Element--></div><br><br> </label> <button id="submit"> <div class="spinner hidden" id="spinner"></div> <span id="button-text">Submit Payment</span> </button> <p id="card-error" role="alert"></p> <p class="result-message hidden"> Payment succeeded. see the result in your <a href="" target="_blank">Stripe dashboard.</a> Refresh the page to pay again. </p> </form>

Now, the payment is going through successfully.现在,付款正在顺利进行。 I just don't know how to access my formData values on the server side.我只是不知道如何在服务器端访问我的 formData 值。 Here is my create.php:这是我的 create.php:

require 'vendor/autoload.php';
\Stripe\Stripe::setApiKey('sk_test_...');  
header('Content-Type: application/json');

try {
  // retrieve JSON from POST body
  $json_str = file_get_contents('php://input');
  $json_obj = json_decode($json_str);

  $email = $json_obj['email'];
  
$paymentIntent = \Stripe\PaymentIntent::create([
    'amount' => 1000,
    'currency' => 'gbp',
    'description' => $user_id,
    "receipt_email" => $email,
   "payment_method_options" => [
    "card" =>   [  "installments"  => null,
                "network" => null,
                 "request_three_d_secure" => "automatic" ]
  ],
  "payment_method_types" => ["card"] ]);

  $output = [
    'clientSecret' => $paymentIntent->client_secret,
  ];
            
  echo json_encode($output);
} catch (Error $e) {
  http_response_code(500);
  echo json_encode(['error' => $e->getMessage()]);
}

The email and user_id is still not being passed and is returning empty on Stripe dashboard. email 和 user_id 仍未传递,并且在 Stripe 仪表板上返回为空。 But, as I said the payment is successful.但是,正如我所说,付款成功。 But, I need to link it to a user_id, email. to use it.但是,我需要将它链接到 user_id,email。才能使用它。 Please help.请帮忙。 Thanks in advance.提前致谢。

In the snippet you shared, $user_id and $email are not defined anywhere, only provided in the parameters.在您共享的代码段中, $user_id$email未在任何地方定义,仅在参数中提供。

Assuming you've confirmed the payload reaches your server as intended from the client, did you mean to set these:假设您已经确认有效负载按照客户端的预期到达您的服务器,您是否打算设置这些:

$user_id = $json['user_id'];
$email = $json['email'];

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

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