Solved

Help needed changing Paypal button amount

Posted on 2014-02-21
7
619 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 11

Expert Comment

by:Andrew Angell
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you very much for your help! I was finally able to get everything setup and working perfectly.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now