Javascript 如何将配送地址详细信息传递给令牌、客户或收费对象?
我有一个自定义HTML表单,带有一个要签出的stripecard输入元素。我似乎找不到一种方法将从表单检索到的装运信息传递到stripe仪表板上的付款发票。 我的HTML代码:Javascript 如何将配送地址详细信息传递给令牌、客户或收费对象?,javascript,php,stripe-payments,Javascript,Php,Stripe Payments,我有一个自定义HTML表单,带有一个要签出的stripecard输入元素。我似乎找不到一种方法将从表单检索到的装运信息传递到stripe仪表板上的付款发票。 我的HTML代码: <form action="admin/charge.php" method="post" id="payment-form"> <div class="form-row"> <input type="hidden" name="total" value="<?= $strip
<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>
我孤零零地想出了答案。
但基本上,将这些详细信息添加到源并向该源收费比向令牌收费要容易得多
希望这能帮助一个奋斗的同伴。我孤零零地想出了答案:(。
但基本上,将这些详细信息添加到源并向该源收费比向令牌收费要容易得多
希望这能帮助其他奋斗者。在php中使用库而不使用包含或使用语句似乎很少见,您是否尝试过使用\Stripe\Stripe;
然后在对象上实例化“\Stripe\Stripe::setApiKey('sk\u test\u key'));`?我不确定你的意思。代码运行良好,API工作正常,等等,唯一的问题是在仪表板的“付款”部分,地址显示为“无地址”。目前我唯一关心的是如何将表单中的地址值与费用相关联。地址确实为客户显示,但是,我想要付款发票显示发货地址。通过var\u dump($\u POST)检查它是否进入php文件
array。POST数据确实到达了正确的位置。它正在寻找一种方法将该数据传递给条带费用对象,这就是问题所在。我进行了var转储,php中要求的所有POST数据都到达了。上面的代码起作用并创建了测试费用。但是,我只是不知道如何将地址变量附加到费用对象中在php中使用库而不使用包含或使用语句是非常少见的,您是否尝试使用\Stripe\Stripe;
然后在对象上实例化?`\Stripe\Stripe::setApiKey('sk\u test\u key'));`?我不确定你的意思。代码运行良好,API工作正常,等等,唯一的问题是在仪表板的“付款”部分,地址显示为“无地址”。目前我唯一关心的是如何将表单中的地址值与费用相关联。地址确实为客户显示,但是,我想要付款发票显示发货地址。通过var\u dump($\u POST)检查它是否进入php文件
array。POST数据确实到达了正确的位置。它正在寻找一种方法将该数据传递给条带费用对象,这就是问题所在。我进行了一次var转储,php中要求的所有POST数据都到达了。上面的代码起作用并创建了测试费用。但是,我根本不知道如何将地址变量附加到费用对象
// 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
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);
?>