Passing variables rather than hard coded data to paypal buy now button - php

I am working on a paypal button and I am trying to figure out how to pass variables to paypal to bounce them back to my handler. I currently have the following in my paypal button code.
<input name="custom" type="hidden" value='{"firstName" : "Lucas","lastName" : "Harvmaster","email" : "shrike321#hotmail.com","companyName" : "Matchbox","password" : "123Cupcakes"}'>
This works, but isn't espectially dynamic. I am unsure how to pass that data as variables I collect from the user rather than hard coded. I don't think I can just create fillable spots in the form because it needs to be added together and formatted to send as the custom variable spot for paypal.
Edit: I saw some folks do something like this, but I wasn't quite able to get it working.
<?php
$firstName = "Lucas";
$lastName = "Harvmaster";
$email = "shrike321#hotmail.com";
$companyName = "Matchbox";
$password = "123Cupcakes";
?>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input name="custom" type="hidden" value='{"firstName" : <?php echo $firstName ?>,"lastName" : "Harvmaster","email" : "shrike321#hotmail.com","companyName" : "Matchbox","password" : "123Cupcakes"}'>
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="89924XE27QRNY">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>

<input type="hidden" name="hosted_button_id" value="89924XE27QRNY">
This is a hosted button, it doesn't use dynamic variables stored in the PayPal account (editable via https://www.paypal.com/buttons )
While it's possible to create a non-hosted HTML button with other set variables, there is no reason to do so.
Instead, use a smart button: https://developer.paypal.com/demo/checkout/#/pattern/client
You can personalize one via https://www.paypal.com/buttons/smart
Then edit the purchase_units object to describe what it's selling. Your own data can be set in the custom_id string as documented here: https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit_request

Related

send a json encoded var via custom field of PayPal Buy Now button

The idea is that I have a standard PayPal Buy Now button in my test website. I was able to learn how to pass a single variable to the PayPal payment processing page and get it back in my listener/success page. I do this by introducing a value to the hidden "custom" field in the form containing the button. So far no problem.
However, since I need an array of data, not just a single value for a single variable, I am trying to apply JSON ENCODE method to send the data and then use the JSON DECODE method in my success message url. I have three defined variables: $st_id,$selected_mod,$selected_tut.
Sadly, none of them can pass through. How can I make it work? Thanks.
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="JKXXXXXXXXXXXXX">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="custom" value="<?php json_encode( array($st_id,$selected_mod,$selected_tut,JSON_FORCE_OBJECT) ) ;?>">
<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
// THE FOLLOWING IS MY CODE IN SUCCESS MESSAGE URL
<?php
$ipn_post_data = $_POST;
//$st_id=$_POST['custom']; //this works if I use a simple variable than a json encoded array
$posted_json = json_decode($_POST['custom']);
$json_st_id= $posted_json['0'];
$json_mod_code= $posted_json['1'];
$json_tut_id= $posted_json['2'];
echo "Wow! Payment was done.";
echo "Your student ID:&nbsp".$json_st_id."<br />";
echo "Module:&nbsp".$json_mod_code."<br />";
echo "Selected Tutor:&nbsp".$json_tut_id."<br />";
echo "Please save the above information until you will receive a confirmation from us. Thank you."
?>
First, you aren't echo-ing the JSON encoded array so your value attribute will be empty.
Second, you will get nested double quotes for any string values in your array, eg
value="[123,"some mod","some tut",16]"
You need to properly encode the HTML attribute value
value="<?= htmlspecialchars(json_encode([$st_id,$selected_mod,$selected_tut])) ?>"
Also, I really wouldn't use JSON_FORCE_OBJECT (which you were doing incorrectly) with an array.
See http://php.net/manual/function.htmlspecialchars.php

PayPal button: How to return a specified value when a payment is successful?

I'm trying to add a PayPal "pay now" type of button, which redirects users to make a payment. But after that's done, how would my database and program know if a payment was successful, and which thing was paid for?
I need PayPal to take a simple id variable, and return it to a specified page only after a payment happens. I think if PayPal can simply hold my "item_id" variable, it would be enough.
On my payments page:
<?php
$item_id = $item["id"];
?>
<td>
<!-- code from PayPal: -->
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="RZ3N8DZLEYN5L">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_paynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</td>
On the payment_complete page:
<?php
if(isset($item_id)){
$query = "UPDATE items SET status = 'paid' WHERE id = {$item_id}";
$result = mysqli_query($db, $query);
redirect_to("paywalled_page.php");
}else{
redirect_to("payments.php");
}
?>
At PayPal, on the button creation page, under "advanced features" it looks like it lets me type a variable to return to a specified URL, but even if I got that to work I don't think a hardcoded "$paid = true" would be enough.
Looks like the "custom" passthrough variable might do it
https://developer.paypal.com/docs/classic/paypal-payments-standard/integration-guide/formbasics/

customizing paypal checkout, including php variable

I'm trying to add a "custom" value to the 3rd party checkout PayPal option. The problem I'm having is the PHP variable is being displayed on the screen. I'm passing the shopping cart values in a session variable.
if(isset($_SESSION['checkout'])){
$orderData = '<table border="1"><th style="width:80px">Item</th>
<th sytle="width:250px">Size</th>
<th style="width:60px">Quantity</th>';
for ($i=0; $i<count($_SESSION['checkout']); $i++){
$orderData .= '<tr><td style="text-align:center">'.$_SESSION['checkout'][$i][0].'</td><td style="text-align:center">'.$_SESSION['checkout'][$i][1].'</td><td style="text-align:center">'.$_SESSION['checkout'][$i][2].'</td></tr>';
}
}
I want that table to be passed along, and based on the PayPal documentation, all I need to do is include it. I'm echoing out the PayPal form like this:
echo '<form>
//other hidden values
<input type="hidden" name="custom" value="'.$orderData.'">
//input submit button here
</form>
And like I said, the input is supposed to be hidden, but it gets displayed before the PayPal button. How come?
UPDATE: Those code passes the amount perfectly:
<input type="hidden" name="amount" value="';?><?php if(isset($_SESSION['disc'])){print_r($_SESSION['disc']);}?><?php echo '">
It doesn't display on the page but the variable amount is correct in the page source.
UPDATE2: Here's my entire cart script:
echo '<div style="position:absolute; left:770px; top:50px">
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="myemail#example.com">
<input type="hidden" name="item_name" value="Merchandise">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="no_shipping" value="0">
<input type="hidden" name="custom" value="'.$orderData.'">
<input type="hidden" name="return" value="backtomywebsite.com">
<input type="hidden" name="cancel_return" value="backtomywebsite.com">
<input type="hidden" name="amount" value="';?><?php if(isset($_SESSION[$disc])){print_r($_SESSION[$disc]);}?><?php echo '">
<input type="image" src="http://www.mywebsite.com/images/BuyNow.png" name="submit" alt="Make payments with PayPal - it\'s fast, free and secure!" width="300" height="204">
</form>
</div>';
The script works when instead of $orderData I have a print_r session with the item information. That information gets sent to my paypal account but it is in an array form and looks ugly. If there was a way to make everything (member information, order information) into a php variable and pass it into the custom field, that would be great. Anyway...here's what is on the screen:
Hope you're still willing to help me out.
What do you mean it gets displayed before the PayPal button? If you are viewing the source code of the page, you will see the value in the page.
If you are saying you are passing amount over, but are not seeing it you may be using the wrong variable. The variable amount is a valid PayPal variable but depending what button you are specifically using the amount varialbe is slightly different. I could not tell from your code as I didn't see what you were passing over for "cmd", but if you are using a buy now button or an add to cart button that the variable "amount" would be the correct variable to use. If you are usnig the cart upload method, then you want to use the variable "amount_x". So if I passed two items over, and the first one was 0.99 and the second was 1.99 I would pass over the variable/value of "amount_1=0.99" and "amount_2=1.99".

I want to seek opinion of experts regarding a form paypal

i have a php page with a form named "BuyForm" and a separate submit button..
like this
<form id="BuyForm" name="BuyForm" method="post" action="purchase.php" enctype="multipart/form-data">
// form goes here
<input type="submit" name="submit" value="Buy Now" class="buyButton">
</form>
now when i want to add a paypal button it gave me seperate form code like this
Paypal button code
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="SOMEVALUE">
<input type="image" src="https://www.paypalobjects.com/en_GB/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal — The safer, easier way to pay online.">
<img alt="" border="0" src="https://www.paypalobjects.com/en_GB/i/scr/pixel.gif" width="1" height="1">
</form>
I m redirecting this form action="purchase.php" to this page only because all validation is in this page only, so what is in my mind is that on checking my form "BuyForm" for any errors and on successful i want it to redirect to another page where there is this paypal form.. Am I thinking right?
user have to click twice, once on submit button then it redirfects to paypal form where he'll click again on purchase button to make his purchase.. or what else can be done??
It's not the right way to do this. You should redirect the user from purchase page instead of letting user click twice. On purchase page, where you are redirecting user to paypal form, write this code and directly redirect user to paypal from there by this :
$paypal_email = "dummy#gmail.com";
$url='https://www.paypal.com/webscr?cmd=_xclick';
$currency="USD";
$paypal_redirect .= $url;
$paypal_redirect .= '&amount='.$cost;
$item_name = 'Item name';
$notify_url = "http://example.com/notify.php";
$cancel_url = "http://example.com/cancel.php";
$paypal_redirect .= '&return = http://example.com/thanks.php&paymentaction=authorization&business='.$paypal_email.'&item_name='.$item_name.'&no_shipping=1&no_note=1&currency_code='.$currency.'&charset=UTF-8&notify_url='.urlencode($notify_url).'&cancel_url='.urlencode($cancel_url).'&rm=2';
header("Location:".$paypal_redirect);
This is the right way to do this.

PayPal and User payment system

I'm playing around with some eCommerce stuff with PayPal Sandbox. So far this is how the application flows:
User logs in, server stores user_id in a session from the database. User can then click a buy now button once logged in. Takes them to paypal, they login and do payment, and IPN receives the notification fine :)
The only thing I now want to do to extend it, is create away of the IPN receiving the users_id back so I can set a flag on their database entry. Can this be done in PayPal?
I have tried the following with the view:
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="--ID-FROM-PAYPAL--">
<input type="hidden" name="user_id" value="<?php echo $user_id;?>">
<input type="image" src="https://www.sandbox.paypal.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.sandbox.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
Thanks for the help :)
<input type="hidden" name="custom" id="custom" value="<?php echo $user_id;?>"/>
Your IPN will receive the userID in the $_POST['custom'] variable.
If you want to pass more than one value to Paypal and back to your IPN:
<script type="text/javascript">
// using prototype
function checkCustom(){
var custom1 = $F('custom1');
var custom2 = $F('custom2');
$('custom').value = '{"userID":"'+ custom1 +'","publicDonation":"'+ custom2 +'"}';
}
</script>
<input type="hidden" name="custom1" id="custom1" value="<?php echo $user_id;?>"/>
<input type="hidden" name="custom2" id="custom2" value="<?php echo $user_email;?>"/>
For further information check the paypal IPN:
custom
Custom value as passed by you, the merchant. These are pass-through
variables that are never presented to your customer Length: 255
characters
https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_IPNandPDTVariables#id091EAB0105Z

Categories