Solved

Help needed changing Paypal button amount

Posted on 2014-02-21
7
645 Views
Last Modified: 2016-03-24
I have a hosted BuyNow button and would like to dynamically change the purchase amount. The only tutorials I can find to simply do this involve changing the hidden input amount.  I would rather use the BMUpdateButton API call to dynamically update the button's amount. However I can't find a good tutorial that will help walk me through the process.

I tried the following, along with a few other techniques, but couldn't get it to work. Any help would be greatly appreciated.

ini_set('track_errors', true);

//set API URL
$url = trim('https://www.sandbox.paypal.com/cgi-bin/webscr'); 

//Create request body content

$body_data = array( 

'USER' => "XXXXXXX", 
'PWD' => "XXXXXXX",
'SIGNATURE' => "XXXXXXX", 
'VERSION' => "109.0",
'METHOD' => "BMUpdateButton",
'HOSTEDBUTTONID' => "XXXXXXX",
'BUTTONTYPE' => "BUYNOW",
'BUTTONCODE' => "HOSTED",
'BUTTONSUBTYPE' => "SERVICES",
'BUTTONIMAGE' => "REG",
'L_BUTTONVAR0' => "amount=.05",
'L_BUTTONVAR1' => "item_name=Test Item Name",
'L_BUTTONVAR2' => "currency_code=USD",
'L_BUTTONVAR3' => "cancel_return=http://www.mysite.com/whynot",
'L_BUTTONVAR4' => "return=http://www.mysite.com/thankyou",
);

//URL encode the request body content array
$body_data = http_build_query($body_data, '', chr(38));

try
{

//create request and add headers
$params = array('http' => array(
'method' => "POST",
'content' => $body_data,
));


//create stream context
$ctx = stream_context_create($params);

//open the stream and send request
$fp = @fopen($url, 'r', false, $ctx);

//get response
$response = stream_get_contents($fp);

//check to see if stream is open
if ($response === false) {
throw new Exception("php error message = " . "$php_errormsg");
}
//echo $response . "<br/>";


//close the stream
fclose($fp);


//parse key from the response
$key = explode("&",$response);
//print_r ($key);
//set url to approve the transaction

$payPalURL = "https://www.sandbox.paypal.com/cgi-bin/webscr" . str_replace("TOKEN", "token", htmlspecialchars(urldecode($key[0])));

If ( $key[5] == 'ACK=Success')
{
header( "Location: https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XXXXXXXXXX" );
}
else
{
//Or if unsuccessful, display error
echo $response;
echo $key[3];
echo 'ERROR Code: "' . str_replace("L_ERRORCODE0=", "", htmlspecialchars(urldecode($key[5]))) . '"<br/> Error Long Message: "' . str_replace("L_LONGMESSAGE0=", "", htmlspecialchars(urldecode($key[7]))) . '"';
}
}

catch(Exception $e)
{
echo 'Message: ||' .$e->getMessage().'||';
}

Open in new window

0
Comment
Question by:jej07
  • 4
  • 3
7 Comments
 
LVL 11

Accepted Solution

by:
Andrew Angell earned 500 total points
ID: 39878798
Is there some reason in particular you want to stick with Payments Standard?

My first instinct, since you're familiar with API's, is to dump Payments Standard altogether and switch to Express Checkout.  That would give you all the freedom you need to handle pricing on your site or in your shopping cart.  

So instead of worrying about updating buttons all the time, you can just output products/pricing on your site straight out of your database.  Once everything is ready to go, you just use the following API calls:


Then if you ever want to throw in subscriptions you can work with CreateRecurringPaymentsProfile as well.  

All of those calls are very quick and easy with my PHP class library for PayPal.
0
 
LVL 1

Author Comment

by:jej07
ID: 39879234
I'm new to PayPal and open to suggestions. This method seemed like the easiest, quickest and most secure way to integrate PayPal into my form. I'm not able to implement SSL at this time, and didn't like the option of adding hidden amount inputs.

The form is simple, and has a radio button group with different options and set prices. It also has one additional option that can be added by checkbox. My thought was I could post the form, insert into MySQL, then display a summary page with the Buynow button and only send the total amount to PayPal for processing.

Again, I'm not familiar enough with PayPal to be able to sort through all the different options they seem to have. It just seemed like the best way to accomplish my goal and keep everything secure.

I'm assuming the express checkout would require SSL.
0
 
LVL 1

Author Comment

by:jej07
ID: 39879632
I've had a chance to take a closer look a the links you sent angelleye. All I can say is WOW, you've put a lot of work into creating a great resource for those wanting to learn more about PayPal integration.

I installed your library, and played around with an example you had here: http://code.tutsplus.com/tutorials/how-to-process-credit-cards-with-paypal-payments-pro-using-php--net-25397.

I'm not sure I want to use the DoDirectPayment method, as I really don't want to deal with the credit card info. I would much rather sent the items and amount to PayPal and let users login to pay. What method(s) would I need to accomplish that?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Expert Comment

by:Andrew Angell
ID: 39879961
Express Checkout does not require an SSL.  With that you're not handling credit card data directly.  It's very similar to Payments Standard except that it's all API based instead of forms and hidden fields.  I always recommend EC over Standard just because it gives you the greatest flexibility.  

So again, to do what you're after Express Checkout would be what you want, and that involves SetExpressCheckout, GetExpressCheckoutDetails, and DoExpressCheckoutPayment.
0
 
LVL 1

Author Comment

by:jej07
ID: 39892291
I believe I've got Express Checkout setup correctly. However, I have a couple of additional questions I hope you can help with.

I wanted to enable credit card payment, without a PayPal account, so I made the SolutionType = Sole. It seems to work, but when redirected to the PayPal checkout, the user has to click the "Pay with a debit or credit card" link to see the form. Can I force that option to be expanded automatically?

I'm working in the sandbox environment. When moved live, will PayPal email a receipt? It currently isn't.

After calling DoExpressCheckoutPayment, I'm showing a success message with the transaction ID on my page. Should I be displaying more transaction details and possibly emailing a receipt from my end?

Do I need to have PayPal redirect back to my site for a success page, or can I utilize their success page?
0
 
LVL 11

Expert Comment

by:Andrew Angell
ID: 39894629
Along with SOLUTIONTYPE=Sole, set LANDINGPAGE=Billing.  That will force the credit card option to be more prominent.  

Yes.  The sandbox is sending them, too, but it sends them to the "notifications" section of your developer account rather than sending out actual emails.

Yes, when DECP is completed successfully this is a good time to display a full receipt and any info you need the user to know about their completed payment.

With Express Checkout the final API calls have to happen from your site.  Nothing is complete until that DECP call is complete from your end.

That said, you can set things up so that you can use PayPal's review pages and skip the final review on your site.  The redirect will still happen, but then you can just immediately call DECP and display nothing but the final receipt page so you don't have to include the additional review.
0
 
LVL 1

Author Closing Comment

by:jej07
ID: 39908157
Thank you very much for your help! I was finally able to get everything setup and working perfectly.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why is my wamp get_include_path() wrong? 2 21
backup program with robocopy 6 45
XML extra information 8 29
Do Wend Macro not working 22 33
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question