stripe-payments - 条纹结帐送货地址
问题描述
我正在使用 Stripe Checkout API 来指导用户付款。我也想获得送货地址来运送它们。这是我在 WordPress 小部件中使用的代码:
<!-- Load Stripe.js on your website. -->
<script src="https://js.stripe.com/v3"></script>
<!-- Create a button that your customers click to complete their purchase. Customize the styling to suit your branding. -->
<button
style="background-color:#6772E5;color:#FFF;padding:8px 12px;border:0;border-radius:4px;font-size:1em"
id="checkout-button-price_1HhPY2IT1E1kKJAOCUoQDPxI"
role="link"
type="button"
>
Checkout
</button>
<div id="error-message"></div>
<script>
(function() {
var stripe =Stripe('pk_live_51H7oCMIT1E1kKJAOeiJKZvF4R2uIJfFCIrOJ1hW8Krned1tfG0abtsQdMD6pRmRyqh5gNNnfxVCzltFc29K7C5Iq00YJyFHBZZ');
var checkoutButton = document.getElementById('checkout-button-price_1HhPY2IT1E1kKJAOCUoQDPxI');
checkoutButton.addEventListener('click', function () {
/*
* When the customer clicks on the button, redirect
* them to Checkout.
*/
stripe.redirectToCheckout({
lineItems: [{price: 'price_1HhPY2IT1E1kKJAOCUoQDPxI', quantity: 1}],
mode: 'payment',
/*
* Do not rely on the redirect to the successUrl for fulfilling
* purchases, customers may not always reach the success_url after
* a successful payment.
* Instead use one of the strategies described in
* https://stripe.com/docs/payments/checkout/fulfill-orders
*/
successUrl: window.location.protocol + '//GLOBESITY.FOUNDATION/success',
cancelUrl: window.location.protocol + '//GLOBESITY.FOUNDATION/canceled',
})
.then(function (result) {
if (result.error) {
/*
* If `redirectToCheckout` fails due to a browser or network
* error, display the localized error message to your customer.
*/
var displayError = document.getElementById('error-message');
displayError.textContent = result.error.message;
}
});
});
})();
</script>
此代码在获得付款时工作正常,但它没有获得送货地址。另外我希望送货地址默认国家是美国。谢谢!
解决方案
您需要使用 Checkout 的服务器+客户端版本(仅客户端版本不支持此功能),并按照此示例创建 CheckoutSession 。具体来说,您需要shipping_address_collection
通过allowed_countries: ['US']
.
推荐阅读
- php - 对子数组进行排序,以便每个子数组中的对应值按数字顺序排列?
- java - 如何使用单独的 Mysql 表使用 JPA 实现枚举
- python - 训练多标签分类模型时内存不足
- regex - Delphi正则表达式编写
- django - 如何在表单字段Django中更改日期格式
- r - 在 R DataFrame 中查找 Pattern 之后的匹配字符
- python - 使用 lmfit 进行高斯拟合
- reactjs - 如何找到错误认证护照并做出反应?
- amazon-web-services - 如何在 GameLift 中实现好友列表?
- javascript - 使用 JQuery 将 JSON 数组放入 HTML 列表