如何向Paypal Express Checkout提交地址?

时间:2013-12-02 16:08:13

标签: php paypal-sandbox paypal

我正在编写一些代码,以便向Paypal Express Checkout提交订单。不幸的是,我似乎无法使整个地址工作起作用,而且我似乎也无法在API文档中找到有关它的更多信息。到目前为止,这是我的代码。

$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);

总结......

你会看到我的问题在整个代码中分发,但在这里总结了一下。

  1. PaymentDetailsType() SetExpressCheckoutRequestType()都具有address-ish属性。哪一个会传递给Paypal?
  2. 如果您传递送货地址,API是否要求您传入帐单邮寄地址?
  3. 如果你没有将AddressOverride设置为1,它是否应该显示你传入的地址?
  4. 但最后,我最重要的是只想知道如何让地址传递给工作。 =)。现在,无论我尝试什么,我似乎无法将任何地址传递给Paypal。

2 个答案:

答案 0 :(得分:1)

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

如果您愿意,可能需要查看我的PHP class library for PayPal。可能有点糟糕,从另一个图书馆开始,但它使它非常快速和简单。它具有使用所有参数准备好的文件以及所有准备好的内容,因此您需要做的就是填写值,每次都可以使用。 :)

答案 1 :(得分:0)

经过一番激烈的研究,我能够使用Paypal的API explorer找出问题。事实证明,不推荐在SetExpressCheckoutRequestType()对象上设置 - >地址。正确的方法是为PaymentDetailsType()对象设置 - > ShipToAddress。

然而,在我的情况下,没有任何工作的原因是因为我的地址错了[掌心掌到前额]。我在美国俄亥俄州哥伦布地区的拉链是技术上。因此,Paypal产生了一个错误。

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

从那时起,我还发现只需使用以下内容即可查看详细的错误信息:

var_dump($setECResponse);