Solved

Help needed changing Paypal button amount

Posted on 2014-02-21
7
635 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Are you using email marketing software? If not, you're missing out on effortless marketing and the reaching of desired conversion rates through email marketing software.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

809 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