简体   繁体   English

如何向Paypal Express Checkout提交地址?

[英]How do I submit an address to Paypal Express Checkout?

I'm in the process of writing some code that will submit an order to Paypal Express Checkout. 我正在编写一些将向Paypal Express Checkout提交订单的代码。 Unforunately I can't seem to get the whole address thing to work, and I also can't seem to find much info on it in the API docs. 不幸的是,我似乎无法使整个地址工作正常工作,而且在API文档中似乎也找不到很多信息。 Here's my code so far. 到目前为止,这是我的代码。

$config = array (
    'mode' => 'sandbox' , 
    'acct1.UserName' => '****removed*****',
    'acct1.Password' => '******removed*******', 
    'acct1.Signature' => '*********removed***********'
);

$paypalService = new PayPal\Service\PayPalAPIInterfaceServiceService($config);
$paymentDetails= new PayPal\EBLBaseComponents\PaymentDetailsType();

// Dummy shipping address
// Obviously, in the final version, this would be passed in from a form
$shipping_address = new PayPal\EBLBaseComponents\AddressType();
$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 PayPal\EBLBaseComponents\PaymentDetailsItemType();
$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 PayPal\CoreComponentTypes\BasicAmountType();
$itemTotal->currencyID = 'USD';
$itemTotal->value = ($itemAmount * $itemQuantity);

// Shipping total
$shippingTotal = new PayPal\CoreComponentTypes\BasicAmountType();
$shippingTotal->currencyID = 'USD';
$shippingTotal->value = 2.00;

// Tax total
$taxTotal = new PayPal\CoreComponentTypes\BasicAmountType();
$taxTotal->currencyID = 'USD';
$taxTotal->value = $itemTotal->value * $sales_tax_rate;

// Order total
$orderTotal = new PayPal\CoreComponentTypes\BasicAmountType();
$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 PayPal\EBLBaseComponents\SetExpressCheckoutRequestDetailsType();
$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 PayPal\PayPalAPI\SetExpressCheckoutRequestType();
$setECReqType->Version = '104.0';
$setECReqType->SetExpressCheckoutRequestDetails = $setECReqDetails;

$setECReq = new PayPal\PayPalAPI\SetExpressCheckoutReq();
$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);

Summed up... 总结...

You'll see my questions distributed throughout the code, but here they are all summed up. 您会看到我的问题遍及整个代码,但在这里将它们全部汇总。

  1. Both PaymentDetailsType() and SetExpressCheckoutRequestType() have address-ish properties. PaymentDetailsType()SetExpressCheckoutRequestType()都具有地址属性。 Which one will be passed on to Paypal? 哪一个会传递给Paypal?
  2. Does the API require that you pass in a billing address if you pass in a shipping address? 如果您输入送货地址,API是否要求您输入帐单地址?
  3. If you don't set AddressOverride to 1, should it show the address that you passed in at all? 如果您没有将AddressOverride设置为1,它是否应该显示您传入的地址?

But in the end, I most importantly just want to know how to get address passing to work . 但是最后, 最重要的是我只想知道如何使地址传递给工作 =). =)。 Right now I can't seem to get any address to pass to Paypal no matter what I try. 现在,无论我如何尝试,我似乎都无法获得任何地址传递给Paypal。

It would help more to see the raw API requests getting generated as opposed to the code generating the requests. 与生成请求的代码相比,查看原始API请求的生成将有更多帮助。 The library you're using should give you some way to see that. 您正在使用的库应为您提供一些查看方式。 Then you just need to make sure the address parameters are getting passed correctly in DoExpressCheckoutPayment. 然后,您只需要确保在DoExpressCheckoutPayment中正确传递了地址参数即可。

If you like, you might want to take a look at my PHP class library for PayPal . 如果愿意,您可能需要看一下我的PayPal PHP类库 Might kinda suck to start over with another library, but it makes it very quick and easy. 可能有点烂,可以重新开始使用另一个库,但这使它变得非常快速和容易。 It has files prepared with all the parameters and everything ready to go so all you need to do is fill in the values and it'll work every time. 它具有准备好所有参数的文件,并且准备就绪,因此您所需要做的就是填写值,并且每次都可以使用。 :) :)

After some furious research, I was able to figure out the problem using Paypal's API explorer . 经过一番激烈的研究,我能够使用Paypal的API Explorer解决问题。 As it turns out, setting ->Address on the SetExpressCheckoutRequestType() object is deprecated. 事实证明,不赞成在SetExpressCheckoutRequestType()对象上设置-> Address。 The correct method is to set ->ShipToAddress for your PaymentDetailsType() object. 正确的方法是为PaymentDetailsType()对象设置-> ShipToAddress。

In my case, however, the reason that nothing was working was because I had my address wrong [slaps palm into forehead]. 但是,就我而言,什么都不起作用的原因是因为我的地址有误[手掌打在额头上]。 My zip, while in the region of Columbus, OH is technically in Dublin, OH. 我的拉链在俄亥俄州哥伦布地区时,从技术上讲是在俄亥俄州都柏林。 So, Paypal was generating an error. 因此,Paypal产生了一个错误。

Paypal wasn't showing me error information, however, so I had no way to know what in particular was causing the error. 但是,Paypal并没有向我显示错误信息,因此我无法知道导致错误的具体原因。 This is where the API explorer came in handy; 这是API资源管理器派上用场的地方; I filled in the API explorer and tried my request--and it gave me detailed error information. 我填写了API资源管理器并尝试了我的请求-它为我提供了详细的错误信息。

Since then, I've also found out that I can see detailed error information simply by using: 从那时起,我还发现,只需使用以下命令即可查看详细的错误信息:

var_dump($setECResponse);

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

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