NVP Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

NVP Problem

Post by koolsamule »

Hi Chaps,

I'm integrating my app with PayPal WebPaymentsPro and at the ExpressCheckout part.

I've contructed a parameter in NVP format:

Code: Select all

$nvpstr="&ADDRESSOVERRIDE=1$shiptoAddress&".$joined_nvp."&MAXAMT=".(string)$maxamt."&AMT=".(string)$amt."&ITEMAMT=".(string)$itemamt."&CALLBACKTIMEOUT=4&".$joined_nvp_ship."&INSURANCEAMT=0.00&INSURANCEOPTIONOFFERED=false&CALLBACK=https://www.ppcallback.com/callback.pl&SHIPPINGAMT=".$CALC_SHIP."&SHIPDISCAMT=0.00&TAXAMT=0.00&ReturnUrl=".$returnURL."&CANCELURL=".$cancelURL ."&CURRENCYCODE=".$currencyCodeType."&PAYMENTACTION=".$paymentType;
This, as an example, echo's:
&ADDRESSOVERRIDE=1&SHIPTONAME=Name&SHIPTOSTREET=&SHIPTOCITY=&SHIPTOSTATE=&SHIPTOCOUNTRYCODE=GB&SHIPTOZIP=&L_NAME0=ITEM!&L_NUMBER0=40&L_DESC0=S&L_QTY0=1&L_AMT0=22.50&L_NAME1=ITEM2&L_NUMBER1=186&L_DESC1=S/M&L_QTY1=1&L_AMT1=14.00&L_NAME2=ITEM3&L_NUMBER2=105&L_DESC2=8&L_QTY2=1&L_AMT2=23.00&MAXAMT=66.5&AMT=59.5&ITEMAMT=59.5&CALLBACKTIMEOUT=4&L_SHIPPINGOPTIONLABEL0=UK - Free Delivery (UK orders over £50)&L_SHIPPINGOPTIONNAME0=UK - Free Delivery (UK orders over £50)&L_SHIPPINGOPTIONAMOUNT0=0&L_SHIPPINGOPTIONISDEFAULT0=true&L_SHIPPINGOPTIONLABEL1=UK-Recorded Delivery - £3.00 (plus 50p charge per extra item)&L_SHIPPINGOPTIONNAME1=UK-Recorded Delivery - £3.00 (plus 50p charge per extra item)&L_SHIPPINGOPTIONAMOUNT1=4&L_SHIPPINGOPTIONISDEFAULT1=false&L_SHIPPINGOPTIONLABEL2=UK-Special Next Day Delivery - £6.00 (plus 50p charge per extra item)&L_SHIPPINGOPTIONNAME2=UK-Special Next Day Delivery - £6.00 (plus 50p charge per extra item)&L_SHIPPINGOPTIONAMOUNT2=7&L_SHIPPINGOPTIONISDEFAULT2=false&INSURANCEAMT=0.00&INSURANCEOPTIONOFFERED=false&CALLBACK=https://www.ppcallback.com/callback.pl& ... CTION=Sale
If I attempt to pass the parameter to PayPal, I get an error stating that the Flat-rate shipping options are missing.

However, if I echo the above code into a browser, then copy and paste in place of the variable parameter (so it is simply text):

Code: Select all

$nvpstr="&ADDRESSOVERRIDE=1&SHIPTONAME=Name&SHIPTOSTREET=&SHIPTOCITY=&SHIPTOSTATE=&SHIPTOCOUNTRYCODE=GB&SHIPTOZIP=&L_NAME0=ITEM!&L_NUMBER0=40&L_DESC0=S&L_QTY0=1&L_AMT0=22.50&L_NAME1=ITEM2&L_NUMBER1=186&L_DESC1=S/M&L_QTY1=1&L_AMT1=14.00&L_NAME2=ITEM3&L_NUMBER2=105&L_DESC2=8&L_QTY2=1&L_AMT2=23.00&MAXAMT=66.5&AMT=59.5&ITEMAMT=59.5&CALLBACKTIMEOUT=4&L_SHIPPINGOPTIONLABEL0=UK - Free Delivery (UK orders over £50)&L_SHIPPINGOPTIONNAME0=UK - Free Delivery (UK orders over £50)&L_SHIPPINGOPTIONAMOUNT0=0&L_SHIPPINGOPTIONISDEFAULT0=true&L_SHIPPINGOPTIONLABEL1=UK-Recorded Delivery - £3.00 (plus 50p charge per extra item)&L_SHIPPINGOPTIONNAME1=UK-Recorded Delivery - £3.00 (plus 50p charge per extra item)&L_SHIPPINGOPTIONAMOUNT1=4&L_SHIPPINGOPTIONISDEFAULT1=false&L_SHIPPINGOPTIONLABEL2=UK-Special Next Day Delivery - £6.00 (plus 50p charge per extra item)&L_SHIPPINGOPTIONNAME2=UK-Special Next Day Delivery - £6.00 (plus 50p charge per extra item)&L_SHIPPINGOPTIONAMOUNT2=7&L_SHIPPINGOPTIONISDEFAULT2=false&INSURANCEAMT=0.00&INSURANCEOPTIONOFFERED=false&CALLBACK=https://www.ppcallback.com/callback.pl&SHIPPINGAMT=0&SHIPDISCAMT=0.00&TAXAMT=0.00&ReturnUrl=http%3A%2F%2Fwww.xxxxx.co.uk%3A80%2Fshop%2Fphp_nvp_samples%2FReviewOrder.php%3FcurrencyCodeType%3DGBP%26paymentType%3DSale&CANCELURL=http%3A%2F%2Fwww.xxxxx.co.uk%3A80%2Fshop%2Fphp_nvp_samples%2FSetExpressCheckout.php%3FpaymentType%3DSale&CURRENCYCODE=GBP&PAYMENTACTION=Sale"
This works, which has really stumped me.

How can a parameter made up of variables fail, but the same output in it's place does?

Is there something obvious that I am missing, because this is doing my brain in!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: NVP Problem

Post by Weirdan »

It's likely you have html entities in the source string, that are converted to actual characters when you echo the variable. You definitely need to apply proper escaping to all parts of the string - the easiest way to do that would be to use http_build_query() function:

Code: Select all

$nvpstr = http_build_query(array(
   "ADDRESSOVERRIDE" =>1,
   "SHIPTONAME" => "Name",
   "SHIPTOSTREET" => "",
   // .. lots of parameters skipped
   "L_SHIPPINGOPTIONLABEL0" => "UK - Free Delivery (UK orders over £50)",
   "L_SHIPPINGOPTIONNAME0" => "UK - Free Delivery (UK orders over £50)",
   "L_SHIPPINGOPTIONAMOUNT0" => 0,
   "L_SHIPPINGOPTIONISDEFAULT0" => "true",
   // .. lots of parameters skipped
   "CALLBACK" => "https://www.ppcallback.com/callback.pl",
   "ReturnUrl" => "http://www.xxxxx.co.uk:80/shop/php_nvp_samples/ReviewOrder.php?currencyCodeType=GBP&paymentType=Sale",
   "CANCELURL" => "http://www.xxxxx.co.uk:80/shop/php_nvp_samples/SetExpressCheckout.php?paymentType=Sale",
   // .. lots of parameters skipped
));
koolsamule
Forum Contributor
Posts: 130
Joined: Fri Sep 25, 2009 10:03 am

Re: NVP Problem

Post by koolsamule »

Hi,

Thanks for the reply, I have tried your method and the parameter was contructed correctly using http_build_query, however this was not accepted by PayPal (giving 10001 Internal Error).

I have followed PayPal's own samples, in which they use simliar process as to my first example, but really don't know why it won't work as variables, but will as a string?!
Post Reply