如何向Paypal Express Checkout提交地址



我正在编写一些代码,这些代码将向Paypal Express Checkout提交订单。不幸的是,我似乎无法让整个地址的事情发挥作用,而且我似乎也无法在API文档中找到太多关于它的信息。这是我迄今为止的代码。

$config = array (
    'mode' => 'sandbox' , 
    'acct1.UserName' => '****removed*****',
    'acct1.Password' => '******removed*******', 
    'acct1.Signature' => '*********removed***********'
);
$paypalService = new PayPalServicePayPalAPIInterfaceServiceService($config);
$paymentDetails= new PayPalEBLBaseComponentsPaymentDetailsType();
// Dummy shipping address
// Obviously, in the final version, this would be passed in from a form
$shipping_address = new PayPalEBLBaseComponentsAddressType();
$shipping_address->Name = "John Smith";
$shipping_address->Street1 = "123 Market Street";
$shipping_address->Street2 = "";
$shipping_address->CityName = "Columbus";
$shipping_address->StateOrProvince = "OH";
$shipping_address->PostalCode = "43017";
$shipping_address->Country = "US";
// A dummy item
// Once again, in a final version this would be passed in
$itemDetails = new PayPalEBLBaseComponentsPaymentDetailsItemType();
$itemDetails->Name = 'Electro Lettuce Feeders';
$itemAmount = 1250.00;
$itemDetails->Amount = $itemAmount;
$itemQuantity = 1;
$itemDetails->Quantity = $itemQuantity;
// Add all items to the list
$paymentDetails->PaymentDetailsItem[0] = $itemDetails;
// The company is in NYS, so in the final version the
// sales tax rate will be passed in (NYS is destination-based)
$sales_tax_rate = 0.07;
// Order sub-total
$itemTotal = new PayPalCoreComponentTypesBasicAmountType();
$itemTotal->currencyID = 'USD';
$itemTotal->value = ($itemAmount * $itemQuantity);
// Shipping total
$shippingTotal = new PayPalCoreComponentTypesBasicAmountType();
$shippingTotal->currencyID = 'USD';
$shippingTotal->value = 2.00;
// Tax total
$taxTotal = new PayPalCoreComponentTypesBasicAmountType();
$taxTotal->currencyID = 'USD';
$taxTotal->value = $itemTotal->value * $sales_tax_rate;
// Order total
$orderTotal = new PayPalCoreComponentTypesBasicAmountType();
$orderTotal->currencyID = 'USD';
$orderTotal->value = $itemTotal->value + $taxTotal->value + $shippingTotal->value;
$paymentDetails->TaxTotal = $taxTotal;
$paymentDetails->ItemTotal = $itemTotal;
$paymentDetails->ShippingTotal = $shippingTotal;
$paymentDetails->OrderTotal = $orderTotal;
$paymentDetails->PaymentAction = 'Sale';
// ***** Is the address from this object passed to Paypal?
$paymentDetails->ShipToAddress = $shipping_address;
$setECReqDetails = new PayPalEBLBaseComponentsSetExpressCheckoutRequestDetailsType();
$setECReqDetails->PaymentDetails[0] = $paymentDetails;
$setECReqDetails->CancelURL = 'https://devtools-paypal.com/guide/expresscheckout/php?cancel=true';
$setECReqDetails->ReturnURL = 'https://devtools-paypal.com/guide/expresscheckout/php?success=true';
// ***** Or is this the address that will be passed to Paypal?
$setECReqDetails->Address = $shipping_address;
// ***** And can you choose to not pass in the billing address? Or is it required? *****
$setECReqDetails->BillingAddress = $shipping_address;
// ***** If this is set to 0, will the previously provided shipping address be shown
// ***** at all? Or will it just be "modify-able" unless you set this to 1?
$setECReqDetails->AddressOverride = 1;
$setECReqType = new PayPalPayPalAPISetExpressCheckoutRequestType();
$setECReqType->Version = '104.0';
$setECReqType->SetExpressCheckoutRequestDetails = $setECReqDetails;
$setECReq = new PayPalPayPalAPISetExpressCheckoutReq();
$setECReq->SetExpressCheckoutRequest = $setECReqType;
$setECResponse = $paypalService->SetExpressCheckout($setECReq);
//var_dump($setECResponse);
$redirect_url = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=".$setECResponse->Token;
return $app->redirect($redirect_url);

总结

您将看到我的问题分布在整个代码中,但在这里它们都是总结的。

  1. PaymentDetailsType()和SetExpressCheckoutRequestType()都具有类似地址的属性。哪一个会传递给Paypal
  2. 如果您输入了发货地址,API是否要求您输入账单地址
  3. 如果您没有将AddressOverride设置为1,它是否应该显示您传入的地址

但最后,我最重要的只是想知道如何让地址传递工作。=)。现在,无论我尝试什么,我似乎都无法获得任何地址传递给贝宝。

与生成请求的代码相比,查看生成的原始API请求会有更多帮助。你正在使用的库应该给你一些方法来查看它。然后,您只需要确保地址参数在DoExpressCheckoutPayment中正确传递。

如果你喜欢,你可能想看看我的PHP类库为贝宝。从另一个库开始可能有点糟糕,但它让它变得非常快速和简单。它有准备好所有参数的文件,一切都准备好了,所以你只需要填写值,它每次都会起作用。:)

经过一番激烈的研究,我使用Paypal的API资源管理器解决了这个问题。事实证明,不赞成在SetExpressCheckoutRequestType()对象上设置->Address。正确的方法是为PaymentDetailsType()对象设置->ShipToAddress。

然而,就我而言,什么都不起作用的原因是我的地址错了[手掌拍打前额]。我的邮政编码,在哥伦布地区,从技术上讲,俄亥俄州是俄亥俄州都柏林的。所以,贝宝产生了一个错误。

然而,Paypal没有向我显示错误信息,所以我无法知道是什么导致了错误。这就是API资源管理器派上用场的地方;我填写了API资源管理器并尝试了我的请求,它给了我详细的错误信息。

从那以后,我还发现,只需使用就可以看到详细的错误信息

var_dump($setECResponse);

最新更新