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



我有一个自定义 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');
StripeStripe::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 = StripeCustomer::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 = StripeCharge::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 中传递第二个参数的创建令牌函数,其中包含我需要的其他详细信息的对象,但是,我不知道如何在 javascript 中执行此操作。 在这里: https://stripe.com/docs/stripe-js/reference 文档将其称为 dataToken,那么我如何创建一个包含运输详细信息的自定义 Stripe 令牌并将其附加到带有卡信息的 Stripe 令牌中?或者,我想我可以创建一个全新的令牌,从条带创建的令牌中传递重要数据,然后在 php 文件中扩展新令牌中的运输详细信息。 任何帮助,非常感谢。

我想出了答案,这一切都在我寂寞的:(上。 但基本上,将这些详细信息添加到源中并对该源而不是令牌收费要容易得多。我遵循了此链接中的文档: https://stripe.com/docs/sources/customers 希望这能帮助一个奋斗者。

最新更新