How to Send Multiple Variables Through PayPal IPN - php

This is the flow of customers:
Visit Website -> Fill Out Form With Their Info -> Click "Buy Now" and Checkout with PayPal -> Be Returned Back To The Website -> Use Information Passed From Checkout From to Run PHP script on server via ajax (assuming the checkout was successful). Well, that's the plan anyways. The problem I am having is figuring out how to actually pass the variables (there are 8 total... array???. I currently have a quick checkout button on the website, which redirects the user away to PayPal to complete the transaction. I've been searching for a good tutorial on YouTube or here at S.O., but have been unsuccessful. Any ideas how I may accomplish this?
Thanks

In your PayPal form, simply use something like the following for your custom field..
Example:
<input type="hidden" name="custom" value="<?php echo $custom1.','.$custom2.','.$custom3; ?>">
or;
<input type="hidden" name="custom" value="custom1,custom2,custom3">
In short the custom name is accepted by PayPal, and you would use commas to separate the variables.
PayPal will pass the custom field back to you, in the IPN.
EDIT:
Here's an example of what you might do with custom:
$custom = $_POST['custom'];
$pattern = ",";
$pieces = explode($pattern,$custom, 3);
// 3 is how many custom fields there are
$custom1 = $pieces[0];
$custom2 = $pieces[1];
if (isset($pieces[2])) {
//an example checking to see if there is a third custom variable
$custom3 = $pieces[2];
}

You can use json to pass multiple values via the custom variable in PayPal..
Create your values:
$custom = array();
$custom['value1'] = "foo";
$custom['value2'] = "bar";
$custom = json_encode($custom);
Then you can POST this data to somewhere:
<input type="hidden" name="custom" value="<?php echo htmlentities($custom) ?>"/>
then get the data from somewhere like this:
$data = json_decode($_POST['custom']);
echo $data->value2;
The output should be bar

Related

Save variables until redirected back on PHP [duplicate]

I have setup a PayPal IPN file. When the user is at the site and press submit details about the transaction is uploaded to the db. The relevant id is sent via PayPal as the custom field. When payment complete IPN used to update DB as transaction completed based on id.
All is fine.
However, this is the tricky bit. I also need to update another table - a discount/coupon code db. The update is based on the code entered and also the number of times the code can still be used. Basically if it was 50 times, after used once the db would be updated with 49. So I need to pass the code and the remaining uses allowed so can say update table where code = XXXX (update new value of 49 etc).
I can work out how to pass all these values in the custom field, but cannot work out how to parse them out again? Read about separating them with : etc, but need some advice from someone who has done before.
This is how IPN details currently comes back:
$custom = $_POST['custom'];
Thank you.
I did just this recently,
Send your paypal custom field to data as your would, inside that custom field, use a separator to split your data.
In the below example, the values are split using a "|", you can use any character you want available in your charset.
$member_id = 1;
$some_other_id = 2;
<input type="hidden" name="custom" value="<?php echo $member_id.'|'.$some_other_id ?>"/>
This will output:
<input type="hidden" name="custom" value="1|2"/>
When you get the information from paypal (the IPN response) process it like so:
$ids = explode('|', $_POST['custom']); // Split up our string by '|'
// Now $ids is an array containing your 2 values in the order you put them.
$member_id = $ids[0]; // Our member id was the first value in the hidden custom field
$some_other_ud = $ids[1]; // some_other_id was the second value in our string.
So basically, we send a string with a custom delimiter that we choose to paypal, paypal will return it to us in the IPN response. We then need to split it up (using the explode() function) and then do what you would like with it.
When you get your value from the database your select it using normal methods, then just decrement it by 1 using:
$val_from_db--; // Thats it!, Takes the current number, and minus 1 from it.
This expands on JustAnil's solution.
HTML:
<input type="hidden" name="custom" value="some-id=1&some-type=2&some-thing=xyz"/>
and your IPN script would look something like this:
<?php
parse_str($_POST['custom'],$_CUSTOMPOST);
echo $_CUSTOMPOST['some-id'];
echo $_CUSTOMPOST['some-type'];
echo $_CUSTOMPOST['some-price'];
?>
You may want to double check that parse_str performs urldecode on resulting array elements.
Here is an example using JSON:
<?php
$arr = array($member_id, $coupon);
$data = json_encode($arr);
?>
<input type="hidden" name="custom" value="<?= $data ?>"/>
Then on the other side:
$custom = json_decode($_POST['custom'], true);
$member_id = $custom[0];
$coupon = $custom[1];
You can also parse associative arrays too:
<?php
$arr = array('id' => $member_id, 'coupon' => $coupon);
$data = json_encode($arr);
?>
<input type="hidden" name="custom" value="<?= $data ?>"/>
Then on the other side:
$custom = json_decode($_POST['custom'], true);
$member_id = $custom['id'];
$coupon = $custom['coupon'];
There's a nice symmetry when using JSON to parse the data.

How to forward information from one page, trough another, and then finally to the third

This is my first post, so excuse me if I will not provide the information correctly.
So my problem is the following:
This is the first form:
<h1>Modificare carti</h1>
<br />
<form action="UTLcrt.php" method="post">
Cod Carte: <br /><input type="numeric" name="cod"><br>
Nume: <br /><input type="text" name="nume"><br>
Autor: <br /><input type="text" name="autor"><br>
Editura: <br /><input type="text" name="editura"><br>
Disponibilitate: <br /><input type="text" name="disp"><br>
Pret: <br /><input type="numeric" name="pret"><br>
<select name="vmod">
<option value="mod">Modificare carte</option>
<option value="str">Sterge carte</option>
<option value="src" >Cauta carte</option>
</select>
<input type="submit">
</form>
The UTLcrt.php contains the following code:
<?php
if (isset($_POST['vmod'])) {
$urls = array(
'mod' => 'modcrt.php',
'str' => 'strcrt.php',
'src' => 'srccrt.php'
);
$url = $urls[$_POST['vmod']];
header("Location: " . $url);
}
?>
And each php page does the following:
modcrt.php changes the entry in our database with the same"cod" with the info provided in the first form
strcrt.php deletes the register in our database, if the "cod" we entered in the first form finds a match
srccrt.php searches in the database if the register with the "cod" provided in the first form was found and shows a possitive message.
My problem is the following: the information I put in the first form doesn't get in the modcrt.php,strcrt.php,src.php pages... the $_Post's are empty...
How to send the information from the first page, trough the second and then to the third?
You can keep them in Session, by using
$_SESSION['info1']=$info1;
The POST values are empty because the third page isn't receiving a POST request. The order of events is this:
User requests the first page.
User POSTs a form to the second page, with values.
Second page tells the user to issue a GET request to the third page.
User requests the third page.
There are a few different ways to keep the information in the chain. You can:
Add it to the query string for the redirect
Store it in session
Store it in a database
etc.
The first one might look like this:
header("Location: " . $url . "?key=value");
Where the key/value pair is similar to those in a POST. In this case the values would be available to the third page in GET:
$_GET['key']
If you use session, the values stay server-side. So in the second page you can set the value:
$_SESSION['key'] = $value;
And then retrieve it in the third page:
$value = $_SESSION['key'];
Note that these session values will continue to live on the server until the session times out. You may want to unset them from the session once you're done with them if it starts to add confusion to other pages the user visits which also make use of these values.
Page 1
<?php
// this starts the session
session_start();
// this sets variables in the session
$_SESSION['color']='red';
$_SESSION['size']='small';
$_SESSION['shape']='round';
?>
Page 2
<?php
$color = $_SESSION['color'];
$size = $_SESSION['size'];
$shape = $_SESSION['shape'];
?>
and so on...

How to call upon a PHP function on button click?

I am trying to setup a website that converts a Steam user ID into an auth ID. It will ask for the visitor to input their regular Steam ID and then hit a button to convert it to auth ID. Steam provides us with the function for ID conversion from one type to the other.
Steam function for converting IDs:
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
Below is my attempt at setting up a basic HTML page that gets user input and then uses the function to convert that input to something else.
<INPUT TYPE = "Text" VALUE ="ENTER STEAM:ID" NAME = "idform">
<?PHP
$_POST['idform'];
$steamid = $_POST['idform'];
?>
Also, this is what the default Steam user ID looks like:
STEAM_0:1:36716545
Thank you for all the help!
If you can make it into two seperate files, then do so.
foo.html
<form method="POST" action="foo.php">
<input type="text" value="ENTER STEAM:ID" name="idform" />
<input type="submit" />
</form>
foo.php
<?php
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
$id = $_POST['idform'];
convert_steamid_to_accountid($id)
?>
if you don't have an option of making two seperate files, you can add the php code to 'foo.html' file and make the form to submit to the same file. However if you do this, check if the file is getting requested the first time, or it is requested because the form is submitted, BEFORE you call convert_steamid_to_accountid() function.
You can do this by:
if ($_SERVER['REQUEST_METHOD']=='POST'){
// your php code here that should be executed when the form is submitted.
}

String Encode, Array Decode

I'm new to PHP, I started about 3 weeks ago.
I have a string, which is used with $_POST to pass it to another page, the second page uses $_GET to get these url and split it as desired.
My problem is that, in my first page I use a String, and I want to encrypt it, so that I can pass it as a plan text. In the second page I must decrypt it and get it as an array.
So is there any encryption method or function I can use which is compatible with $_POST ( so I can send it to another page ) and decrypt it as an array ?
I need this method, because the second page is actually connecting to website and is a payment method. So i don't want users to manually edit the url and lower the amount of $ for the product they get.
tnx for your help.
You're thinking about this wrong. You NEVER trust information coming from the user's side.
For example, if your user sends a form that says what item they want, DO NOT include the price in the form. Instead, get the price from the server (database), where it can be trusted.
What you probably want to do is pass the contents of the users cart (i.e. the items he'd like to order) to the payment site.
Therefore, you should create a form like:
<form action="URL/to/paymentPage.php" method="post">
<!-- Item 1 -->
<input type="hidden" name="items[0]" value="productID1"/>
<input type="hidden" name="quantity[0]" value="quantity1"/>
<!-- Item 2 -->
<input type="hidden" name="items[1]" value="productID2"/>
<input type="hidden" name="quantity[1]" value="quantity2"/>
<!-- ... -->
<!-- Item n -->
<input type="hidden" name="items[n]" value="productIDn"/>
<input type="hidden" name="quantity[n]" value="quantityn"/>
<input type="submit" value="Order"/>
</form>
On the server in the file "URL/to/paymentPage.php" you can access the items using the following code:
<?php
$items = $_POST['items']; // Array of items ..
$quantities = $_POST['quantity']; // The array of quantities for each item ..
// Calculate the total price ..
$totalPrice = 0;
foreach($items as $idx => $itemID) {
if($quantities[$idx]>0) {
totalPrice += getPriceFromDB($itemID) * $quantities[$idx];
}
}
echo 'Total Price to pay: '.$totalPrice;
?>
where the function getPriceFromDB actually retrieves the price for the item/product with the id $itemID from your database or elsewhere... :)
However, the user items are usually stored in the session, and, therefore, there is no need to submit the again.. ;)
Despite not fully understanding what you're trying to achieve, you can use base64 encoding:
$encoded_string = base64_encode ($string);
$decoded_string = base64_decode ($encoded_string);

Passing and Parse PayPal IPN Custom field

I have setup a PayPal IPN file. When the user is at the site and press submit details about the transaction is uploaded to the db. The relevant id is sent via PayPal as the custom field. When payment complete IPN used to update DB as transaction completed based on id.
All is fine.
However, this is the tricky bit. I also need to update another table - a discount/coupon code db. The update is based on the code entered and also the number of times the code can still be used. Basically if it was 50 times, after used once the db would be updated with 49. So I need to pass the code and the remaining uses allowed so can say update table where code = XXXX (update new value of 49 etc).
I can work out how to pass all these values in the custom field, but cannot work out how to parse them out again? Read about separating them with : etc, but need some advice from someone who has done before.
This is how IPN details currently comes back:
$custom = $_POST['custom'];
Thank you.
I did just this recently,
Send your paypal custom field to data as your would, inside that custom field, use a separator to split your data.
In the below example, the values are split using a "|", you can use any character you want available in your charset.
$member_id = 1;
$some_other_id = 2;
<input type="hidden" name="custom" value="<?php echo $member_id.'|'.$some_other_id ?>"/>
This will output:
<input type="hidden" name="custom" value="1|2"/>
When you get the information from paypal (the IPN response) process it like so:
$ids = explode('|', $_POST['custom']); // Split up our string by '|'
// Now $ids is an array containing your 2 values in the order you put them.
$member_id = $ids[0]; // Our member id was the first value in the hidden custom field
$some_other_ud = $ids[1]; // some_other_id was the second value in our string.
So basically, we send a string with a custom delimiter that we choose to paypal, paypal will return it to us in the IPN response. We then need to split it up (using the explode() function) and then do what you would like with it.
When you get your value from the database your select it using normal methods, then just decrement it by 1 using:
$val_from_db--; // Thats it!, Takes the current number, and minus 1 from it.
This expands on JustAnil's solution.
HTML:
<input type="hidden" name="custom" value="some-id=1&some-type=2&some-thing=xyz"/>
and your IPN script would look something like this:
<?php
parse_str($_POST['custom'],$_CUSTOMPOST);
echo $_CUSTOMPOST['some-id'];
echo $_CUSTOMPOST['some-type'];
echo $_CUSTOMPOST['some-price'];
?>
You may want to double check that parse_str performs urldecode on resulting array elements.
Here is an example using JSON:
<?php
$arr = array($member_id, $coupon);
$data = json_encode($arr);
?>
<input type="hidden" name="custom" value="<?= $data ?>"/>
Then on the other side:
$custom = json_decode($_POST['custom'], true);
$member_id = $custom[0];
$coupon = $custom[1];
You can also parse associative arrays too:
<?php
$arr = array('id' => $member_id, 'coupon' => $coupon);
$data = json_encode($arr);
?>
<input type="hidden" name="custom" value="<?= $data ?>"/>
Then on the other side:
$custom = json_decode($_POST['custom'], true);
$member_id = $custom['id'];
$coupon = $custom['coupon'];
There's a nice symmetry when using JSON to parse the data.

Categories