gpt4 book ai didi

javascript - 如何将送货地址详细信息传递给 token 、客户或收费对象?

转载 作者:行者123 更新时间:2023-11-28 03:37:55 25 4
gpt4 key购买 nike

我有一个自定义 HTML 表单,其中包含要结帐的 Stripe 卡输入元素。我似乎找不到一种方法将从表单中检索到的运输信息传递到 Stripe 仪表板上的付款发票。我的 HTML 代码:

<form action="admin/charge.php" method="post" id="payment-form">
<div class="form-row">
<input type="hidden" name="total" value="<?= $stripe_total?>"> </input>
<input name = "first" type="text" placeholder="First Name*" required maxlength="20" id="first">
<input name = "last" type="text" placeholder="Last Name*" required maxlength="20" id="last">
<input type="email" name="email" placeholder="Email Address*" id="email" maxlength="30" required>
<input type="tel" name="phone" id="phone" placeholder="Phone" maxlength="10">
<input name = "addy1" type="text" placeholder="Address Street Name*" required maxlength="35" id="addy1">
<input name = "addy2" type="text" placeholder="Address 2" maxlength="15" id="addy2">
<input type="text" name="zip" id="zip" placeholder="Zip Code*" required maxlength="11">
<input type="text" name="city" id="city" placeholder="City*" required maxlength="20">
<select name="state" id="state">
<option value="State">State</option>
//every state
</select>
<select required name="country" id="country">
<option value='Countries'>Countries</option>
//every country option
</select>



<div id="card-element"></div>

<div id="card-errors" role="alert"></div>
</div>

<button>Submit Payment</button>
</form>

JavaScript:

// JavaScript Document
// Create a Stripe client.
var stripe = Stripe('pk_test_PHnlKn4Jd72hlJjWPtQqDR1G00U2vyOtMP');

// Create an instance of Elements.
var elements = stripe.elements();

// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
base: {
color: '#32325d',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: '#fa755a',
iconColor: '#fa755a'
}
};

// Create an instance of the card Element.
var card = elements.create('card', {
style: style,
hidePostalCode:true,
})
;

// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');

// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});

// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();

stripe.createToken(card,tokenData).then(function(result) {
if (result.error) {
// Inform the user if there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});

// Submit the form with the token ID.
function stripeTokenHandler(token) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);

// Submit the form
form.submit();
}

和 PHP:

<?php
require_once('../vendor/autoload.php');
require_once('config.php');
\Stripe\Stripe::setApiKey('sk_test_key');

$POST = filter_var_array($_POST, FILTER_SANITIZE_STRING);
$token = $POST['stripeToken'];
$first = $POST['first'];
$last = $POST['last'];
$email = $POST['email'];
$phone = $POST['phone'];
$addy1 = $POST['addy1'];
$addy2 = $POST['addy2'];
$zip = $POST['zip'];
$city = $POST['city'];
$state = $POST['state'];
$country = $POST['country'];
$total = $POST['total'];


$customer = \Stripe\Customer::create(array(
"email" => $email,
"source" => $token,
"address" => array(
"line1" => "Test line 1",
"line2" => "Test Line 2",
"city" => "My City",
"postal_code" => "90210",
"state" => "CA",
)
));

$charge = \Stripe\Charge::create(array(
"billingAddress"=> true,
"shippingAddress"=>true,
"amount" => $total,
"currency" => "usd",
"description" => "BFM Purchase",
"customer" => $customer->id,
"shipping" => array(
"address" => array(
"name" => $first,
))
));



print_r($charge);
echo " break ";
print_r($customer);
?>

好吧,就我尝试过的事情而言,我尝试将 billing_details 添加到费用和客户对象中,但我收到一条错误,指出 billing_details 位于未知参数中。我认为在某个地方我可以将该参数设置为“true”,但我不知道在哪里以及从那里做什么。我从 Stripe 文档中看到的下一件事是,我可以将 js 中的创建 token 函数传递给第二个参数,其中包含我需要的附加详细信息的对象,但是,我不知道如何在 javascript 中执行此操作。这里:https://stripe.com/docs/stripe-js/reference文档将其称为 dataToken,那么我如何创建带有运输详细信息的自定义 Stripe token ,并将其附加到带有卡信息的 Stripe token 中?或者,我认为我可以创建一个全新的 token ,将 strip 创建的 token 中的重要数据传递给它,然后在 php 文件中扩展新 token 中的运输详细信息。如有任何帮助,我们将不胜感激,谢谢。

最佳答案

我独自一人找到了答案:(。但基本上,将这些详细信息添加到源并对该源收费而不是 token 要容易得多。我按照此链接中的文档进行操作: https://stripe.com/docs/sources/customers希望这可以帮助到正在奋斗的同胞们。

关于javascript - 如何将送货地址详细信息传递给 token 、客户或收费对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57550294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com