繁体   English   中英

Paypal Checkout - 非会员不询问收货地址?

[英]Paypal Checkout - don't ask for delivery address for non-members?

我刚开始玩这个模块:

https://github.com/paypal/paypal-checkout

我正在尝试弄清楚如何关闭客户的送货地址。 我知道在订购版本中您可以在 URL 中执行&NOSHIPPING=1 ,但我找不到有关 API 4 版本的任何信息。 我的代码是:

paypal.Button.render({

    // Pass the client ids to use to create your transaction on sandbox and production environments
    locale: 'fr_FR',

    //env: 'production',
    env: 'sandbox',

    client: {
        sandbox: "...",
        production: "..."
    },

    // Pass the payment details for your transaction
    // See https://developer.paypal.com/docs/api/payments/#payment_create for the expected json parameters

    payment: function() {
        return paypal.rest.payment.create(this.props.env, this.props.client, {
            transactions: [
                {
                    amount: {
                        total:    window.my_config.grand_total,
                        currency: 'EUR',
                        details: {
                              "subtotal": window.my_config.price,
                              "tax": window.my_config.vat_amount
                        }
                    },
                }
            ]
        });
    },

    // Display a "Pay Now" button rather than a "Continue" button

    commit: true,

    // Pass a function to be called when the customer completes the payment

    onAuthorize: function(data, actions) {
        return actions.payment.execute().then(function() {
            console.log('The payment was completed!');
            console.log(data, actions)

            if (error === 'INSTRUMENT_DECLINED') {
                actions.restart();
            }

        });
    },

    // Pass a function to be called when the customer cancels the payment

    onCancel: function(data) {
        console.log('The payment was cancelled!');
    },
    style: {
      shape:  'rect',
      size: "medium"
    }

}, '#paypalContainerEl');

使用“shipping_preference:'NO_SHIPPING'。”

createOrder: function(data, actions) {
    $('#paypalmsg').html('<b>' + 'WAITING ON AUTHORIZATION TO RETURN...' + '</b>');
    $('#chkoutmsg').hide()
    return actions.order.create({
        purchase_units: [{
            description: 'GnG Order',
            amount: {
                value: cartTotal
            }
        }],
        application_context: {
          shipping_preference: 'NO_SHIPPING'
        }

    });
},

你需要在payment功能中通过experience下的no_shipping选项,像这样:

return actions.payment.create(
{
    payment:
    {
        transactions: [
        {
            amount:
            {
                total: "10",
                currency: 'EUR'
            }
        }]
    },
    experience:
    {
        input_fields:
        {
            no_shipping: 1
        }
    }
});

在文档中, 这里这里 不过请注意,客人仍会被要求提供账单地址,即使不再询问他们的送货地址。

对于那些通过PHP 中的 PayPal REST API集成的人,设置 no_shipping 属性:

          apiContext = $this->apiContext;

          $payer = new \PayPal\Api\Payer();
          $payer->setPaymentMethod('paypal');

          $inputFields = new \PayPal\Api\InputFields();
          $inputFields->setNoShipping(1); //<-- NO SHIPPING!!!!!!!!!!

          $webProfile = new \PayPal\Api\WebProfile();
          $webProfile->setName($uid); // <-- UNIQUE NAME FOR THE TRANSACTION
          $webProfile->setInputFields($inputFields);

          $createProfileResponse = $webProfile->create($apiContext);
          $webProfile = \PayPal\Api\WebProfile::get($createProfileResponse->getId(), $apiContext);

          $amount = new \PayPal\Api\Amount();
          $amount->setCurrency('EUR')
            ->setTotal($this->deposit_eur);

          $transaction = new \PayPal\Api\Transaction();
          $transaction->setAmount($amount);


          $redirectUrls = new \PayPal\Api\RedirectUrls();
          $redirectUrls->setReturnUrl($this->return_url)
          ->setCancelUrl($this->cancel_url);


          $payment = new \PayPal\Api\Payment();
          $payment->setIntent('sale')
            ->setPayer($payer)
            ->setRedirectUrls($redirectUrls)
            ->setTransactions(array($transaction))
            ->setExperienceProfileId($webProfile->getId()); //<-- SET EXPERIENCE PROFILE

          try{
            $payment->create($apiContext);
          } catch (\Exception $ex) {
            debug($ex);
            exit;
          }

          $approvalUrl = $payment->getApprovalLink();

对于使用 C# 通过 PayPal REST API 集成此功能的不幸小伙子来说,这有点棘手。

您可以像在Paypal Repo Example中一样创建一个WebProfile

    var experienceProfile = new WebProfile()
    {
        name = Guid.NewGuid().ToString(), // required field
        input_fields = new InputFields()
        {
            no_shipping = 1
        }
    };

    var experienceId = experienceProfile .Create(_apiContext).id;

    new Payment
        {
            intent = "sale",
            payer = new Payer
            {
                payment_method = "paypal"
            },
            transactions = new List<Transaction>
            {
              // ...
            },
            redirect_urls = new RedirectUrls
            {
                return_url = "..",
                cancel_url = ".."
            },
            experience_profile_id = experienceId
        };

如果有人在使用 v2 API REST 方法时遇到了这个问题,那么在正文中的 create order 中设置application_contextshipping_preference确实对我有用。

https://developer.paypal.com/docs/api/orders/v2/#definition-experience_context_base

  const response = await fetch(url, {
    method: 'post',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${accessToken}`,
    },
    body: JSON.stringify({
      intent: 'CAPTURE',
      application_context: {
        shipping_preference: 'NO_SHIPPING',
      },
      purchase_units: []
   })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM