Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Help needed changing Paypal button amount

Posted on 2014-02-21
7
Medium Priority
?
748 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 11

Accepted Solution

by:
Andrew Angell earned 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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.
Suggested Courses

604 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