你能在PHP中向Stripe Checkout传递送货地址吗?

wbrvyc0a  于 2023-05-16  发布在  PHP
关注(0)|答案(2)|浏览(187)

我正在使用Stripe Checkout API引导网站用户进行付款。
有没有一种方法可以将送货地址传递给托管的结账页面,以便从推荐人那里收集,而不是从Stripe本身收集?

function createSession()
{
    require 'vendor/autoload.php';
    \Stripe\Stripe::setApiKey('[API_KEY_REDACTED]');
    
    $YOUR_DOMAIN = '[SITE_URL_REDACTED]';
    
    // Calculate price
    $price = 50;
    
    $checkout_session = \Stripe\Checkout\Session::create([
        'billing_address_collection' => 'required',
        'payment_method_types' => ['card'],
        'line_items' => [[
            'price_data' => [
                'currency' => 'gbp',
                'unit_amount' => $price,
                'product_data' => [
                    'name' => 'Product'
                ],
            ],
            'quantity' => 1,
        ]],
        'mode' => 'payment',
        'success_url' => $YOUR_DOMAIN . '/success',
        'cancel_url' => $YOUR_DOMAIN . '/cancel',
    ]);
    
    return json_encode(['id' => $checkout_session->id]);
}

您可以添加以下行,使Stripe请求送货地址,但我想从referrer传递此信息。

'shipping_address_collection' => [
    'allowed_countries' => ['US', 'CA'],
],
3gtaxfhh

3gtaxfhh1#

为此,您需要create a Customer并提供他们的送货地址,然后在创建结账会话时提供现有客户:

$checkout_session = \Stripe\Checkout\Session::create([
    'customer' => 'cus_123',
    ...
]);
gajydyqb

gajydyqb2#

无法将收货地址传递给Stripe checkout。另外,您不应该使用下面的shipping_address_collection,因为您需要在每次进入Stripe checkout时手动填写Shipping address,因为它不会保存:

'shipping_address_collection' => [
    'allowed_countries' => ['US', 'CA'],
],

因此,您应该使用shipping参数创建a customer,如下图所示,然后收货地址保存在客户详细信息中,您可以在Stripe Dashboard上查看:

$customer = $stripe->customers->create([
    'shipping' => array(
        'country' => 'USA',
        'state' => 'CA',
        'city' => 'San Francisco',
        'line1' => '58 Middle Point Rd',
        'line2' => '',
        'postal_code' => '94124'
    ),
]);

然后,将客户设置为a Checkout session,如下所示。实际上这样做的话,您在Stripe checkout无法看到和更改收货地址,但您也不需要每次前往Stripe checkout时,都在Stripe checkout手动填写收货地址

$checkout_session = $stripe->checkout->sessions->create([
    'customer' => $customer['id'], // Here
    ...
]);

然后对于第二次付款,如果你想更新送货地址,你可以search the customer与电子邮件如下所示:

$email = 'apple@gmail.com';
$customer = $stripe->customers->search([
    'query' => "email:'$email'",
]);

然后,您可以update the shipping address,如下所示:

$customer = $stripe->customers->update(
    $customer['data'][0]['id'],
    'shipping' => array(
        'country' => 'USA',
        'state' => 'FL',
        'city' => 'Miami Beach',
        'line1' => '1551 Jefferson Ave',
        'line2' => '',
        'postal_code' => '33139'
    ),
);

然后将客户设置为一个Checkout会话,如下图所示:

$checkout_session = $stripe->checkout->sessions->create([
    'customer' => $customer['id'], // Here
    ...
]);

相关问题