Sandbox paypal test account not returning variables - php

I have two paypal test accounts. One is "testaddabusiness#shop.com" and another is "testaddapersonal#shop.com". They both are for testing my transactions. I followed a tutorial to make a shopping cart integrated with paypal. The problem is it is not returning some of the variables which i need to save in my database table. I have setup the website preference with success.php. It is not returning payment details from paypal.
success.php
<?php
session_start();
include("db/db.php");
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<html>
<title>Success</title>
<body>
<?php
if(isset($_SESSION['cart']) && isset ($_SESSION['login_email'])){
$total=0;
$ships=$_POST['shipment'];
$user=$_POST['name'];
$user_id=$_POST['id'];
$file=$_FILES['file']['name'];
//orders and order details.
$que=mysqli_query($con,"insert into orders(custom_file,date,customerid,ship_days) values('$file',CURRENT_TIMESTAMP(),'$user_id','$ships')");
if($que)
{
$m=move_uploaded_file($_FILES['file']['name'],'./ServerUploadedFiles/'.$user.$file);
$q=mysqli_query($con,"select serial from orders where customerid='$user_id' and date=CURRENT_TIMESTAMP()");
while($row1=mysqli_fetch_array($q,MYSQLI_ASSOC))
{
$order_id=$row1['serial'];
}
foreach($_SESSION['cart'] as $id => $value){
$subtotal=$value['price']*$value['quantity'];
$pid=$value['id'];
$quantity=$value['quantity'];
$color=$value['color'];
$size=$value['size'];
$total+= $subtotal;
$query= mysqli_query($con, "INSERT INTO order_detail(orderid,productid,quantity,price,color,size) VALUES ($order_id, $pid, $quantity, $subtotal, '$color', '$size')");
}
}
//payment details for paypal
$amount=$_GET['amt'];
$currency=$_GET['cc'];
$trx_id=$_GET['tx'];
$insert_payments= mysqli_query($con,"insert into payments(amount,cust_id,trx_id,currency) values('$amount','$user_id','$trx_id','$currency')");
if($amount==$total)
{
echo "<h2>Hello". $user."You have successfully done purchasing process.Please <a href='profile.php'>Go to your account</a>!</h2>";
}
else
{
echo "<h2>Hello". $user."Your payment was not successful.Please <a href='profile.php'>Go to your account</a>!</h2>";
}
}
?>
</body>
</html>
form.php
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" enctype="multipart/form-data">
<?php
$email=$_SESSION["login_email"];
$query=mysqli_query($con,"select * from customers where email='$email'");
while($row=mysqli_fetch_array($query,MYSQLI_ASSOC))
{
?>
<input type="hidden" class="input_form" name="id" value="<?=$row["serial"]?>">
<input type="hidden" class="input_form" name="name" value="<?=$row["name"]?>">
<input type="hidden" class="input_form" name="phone" value="<?= $row["phone"] ?>">
<input type="hidden" class="input_form" name="city" value="<?= $row["city"] ?>">
<input type="hidden" class="input_form" name="address" value="<?= $row["address"] ?>">
Download custom size form Provide us your custom size
<input type="file" name="file">
Select shipment interval (optional):<select id="shipment" name="shipment">
<option selected value="7" id="7">7</option>
<option value="6" id="6"> 6 </option>
<option value="5" id="5">5 </option>
<option value="4" id="4">4 </option>
<option value="3" id="3">3 </option>
</select>
<p>Before clicking submit read Terms and Policies carefully..Once ordered would not be restored.</p>
<input type="checkbox" value="I accept the terms and policies by Aada Fashion.">I accept the terms and policies by Aada Fashion.
<?php
}
?>
<!-- Identify your business so that you can collect the payments. -->
<input type="hidden" name="business" value="testaddabusiness#shop.com">
<!-- Specify a Buy Now button. -->
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<?php
if(isset($_SESSION['cart'])){
$count=1;
foreach($_SESSION['cart'] as $id => $value){
?>
<input type="hidden" name="item_number_<?php echo $count ?>" value="<?php echo $value['id'] ?>">
<input type="hidden" name="item_name_<?php echo $count ?>" value="<?php echo $value['name'] ?>">
<input type="hidden" name="quantity_<?php echo $count ?>" value="<?php echo $value['quantity'] ?>">
<input type="hidden" name="amount_<?php echo $count ?>" value="<?php echo $value['price'] ?>">
<input type="hidden" name="on1_<?php echo $count ?>" value="<?php echo $value['color'] ?>">
<input type="hidden" name="on0_<?php echo $count ?>" value="<?php echo $value['size'] ?>">
<input type="hidden" name="currency_code" value="USD">
<?php
$count++;
}
}
?>
<input type="hidden" name="return" value="http://abc/WebProject_Adda/success.php">
<input type="hidden" name="cancel_return" value="http://abc/WebProject_Adda/paypal_cancel.php">
<input type="image" name="submit" border="0" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
</form>

Related

Notify function isn't updating the database while performing paypal payments

This is my first time integrating the Paypal Payment in a project and it is in CI. So the thing is everything is working perfectly except that the Notify Url was supposed to make me able to change the payment status from 'Pending' to 'Success' but it isn't in my case. I have the code below and i really have tried many things but still don't know where i am slipping off.
<form id="paypal-pre" action="<?php echo base_url('auth/process'); ?>" method="post">
<input type="hidden" name="user_id" value="<?php echo $_POST['register_user_id']; ?>">
<input type="hidden" name="first_name" value="<?php echo $_POST['customer_first_name']; ?>">
<input type="hidden" name="last_name" value="<?php echo $_POST['customer_last_name']; ?>">
<input type="hidden" name="addr1" value="<?php echo $_POST['customer_address1']; ?>">
<input type="hidden" name="addr2" value="<?php echo $_POST['customer_address2']; ?>">
<input type="hidden" name="city" value="<?php echo $_POST['customer_city']; ?>">
<input type="hidden" name="county" value="<?php echo $_POST['customer_county']; ?>">
<input type="hidden" name="postcode" value="<?php echo $_POST['customer_postcode']; ?>">
<input type="hidden" name="amount" value="<?php echo $_POST['payment_total']; ?>">
<!-- Display the payment button. -->
<input type="image" name="submit" border="0" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" alt="PayPal - The safer, easier way to pay online">
<img alt="" border="0" width="1" height="1" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" >
</form>
Above is the billing info collection form.
function process(){
$arr['user'] = array(
'p_user_id' => $this->input->post('user_id'),
'p_billing_firstname' => $this->input->post('first_name'),
'p_billing_lastname' => $this->input->post('last_name'),
'p_billing_address1' => $this->input->post('addr1'),
'p_billing_address2' => $this->input->post('addr2'),
'p_billing_city' => $this->input->post('city'),
'p_billing_county' => $this->input->post('county'),
'p_billing_postcode' => $this->input->post('postcode'),
'p_status' => 'processing',
'p_amount' => $this->input->post('amount'),
);
$arr['pay'] = array(
'p_amount' => $this->input->post('payment_total'),
);
$this->load->model('demo_auth_model');
$this->demo_auth_model->process($arr);
$this->load->view('paypal',$arr);}
function notify() {
if($this->input->post('custom') != ''){
$id = $this->input->post('custom');
$amt = $_POST['amount'];
$sql = "UPDATE users_payments SET p_status='success' WHERE p_id='$id'";
$this->db->query($sql);
return;
}
}
The Process and notify controller function
<?php $paypal_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
$paypal_id = '******#yahoo.com';
$amount = $_POST['amount'];
$id = mysql_insert_id();?>
<form id="paypal" action="<?= $paypal_url; ?>" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="<?php echo $paypal_id; ?>">
<input type="hidden" name="amount" value="<?php echo $amount; ?>">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="item_name" value="<?php echo 'Business Register'; ?>">
<input type="hidden" name="custom" value="<?php echo $id; ?>" />
<input type='hidden' name='notify_url' value='<?php echo base_url('auth/notify'); ?>'>
<input type='hidden' name='cancel_return' value='<?php echo base_url('auth/cancel'); ?>'>
<input type='hidden' name='return' value='<?php echo base_url();?>'>
</form><script type="text/javascript">
document.getElementById('paypal').submit();
</script>
The main paypal form which is submitted automatically after saving the billing information.
public function process($arr) {
$this->db->set($arr['user']);
$this->db->insert('user_payments');
return;
}
Process Model from demo_auth_model
Any help would be really appreciated.
P.S. All database infos are correct
well for codeigniter i prefer this library
http://tutsnare.com/integrate-paypal-using-ci-merchant-in-codeigniter/
https://github.com/expressodev/ci-merchant
for 2 Reasons
1. Auto redirect to our website
2. Take all data in session to our website
Try it and enjoy

Is it possible to add discount code to a paypal button?

I'm looking to add a discount code to this Paypal button but don't know how to go about doing it.
Can anyone help or suggest how too, I just need it so a customer can enter a the code and receive 10% off the price
The code is below for the button
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<p>Please click on the link to pay</p>
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="business" value="<?php echo C_OUR_EMAIL; ?>" />
<input type="hidden" name="first_name" value="<?php echo strfordisp($ofirstname); ?>" />
<input type="hidden" name="last_name" value="<?php echo strfordisp($olastname); ?>" />
<input type="hidden" name="address1" value="<?php echo strfordisp($theorder["oaddress"]); ?>" />
<input type="hidden" name="address2" value="<?php echo strfordisp($theorder["oaddress2"]); ?>" />
<input type="hidden" name="address3" value="<?php echo strfordisp($theorder["oaddress3"]); ?>" />
<input type="hidden" name="city" value="<?php echo strfordisp($theorder["otown"]); ?>">
<input type="hidden" name="zip" value="<?php echo strfordisp($theorder["opostcode"]); ?>" />
<?php $orderdets = mysql_query("select * from c4d_orderitems where orderid='" . $_SESSION["db_order"] . "' and confirm");
$iloop = 1;
while ($orderrow = mysql_fetch_array($orderdets))
{
$itemdesc = $orderrow["itemtypedesc"];
$itemdesc .= " to " . $orderrow["dpostcode"];
$itemprice = $orderrow["cost"] + $orderrow["surcharge"] + $orderrow["insurancecost"];
?>
<input type='hidden' name="item_name_<?php echo $iloop; ?>" value='<?php echo strfordisp($itemdesc); ?>' />
<input type='hidden' name="item_number_<?php echo $iloop; ?>" value='<?php echo $orderrow["itemtype"]; ?>' />
<input type='hidden' name="amount_<?php echo $iloop; ?>" value='<?php
// pctrends
if ((strtoupper($ofirstname)=="PCTRENDSTEST") || (strtoupper($olastname)=="PCTRENDSTEST") || (substr($_SERVER['REMOTE_ADDR'],0,11)=="82.152.55.1"))
echo("0.01");
else echo $itemprice;
?>' />
<input type='hidden' name="quantity_<?php echo $iloop; ?>" value='1' />
<?
$iloop++;
}
?>
<input type="hidden" name="return" value="<?php echo C_SITE_ROOT; ?>stage7.php" />
<input type="hidden" name="cancel-return" value="<?php echo C_SITE_ROOT; ?>order-cancel.php" />
<input type="hidden" name="notify_url" value="<?php echo C_SITE_ROOT; ?>paypal.php" />
<input type="hidden" name="rm" value="2" />
<input type="hidden" name="invoice" value="<?php echo $_SESSION["db_order"]; ?>" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="no-shipping" value="1" />
<input type="hidden" name="button_subtype" value="products" />
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest" />
<input type="hidden" name="country" value="GB" />
<p class='fmenu'><input type="image" src="https://www.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.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1"></p>
</form>
You must check for the discount-code validity (with AJAX) and make the appropriate calculations to apply the discount to the amount variable (with JavaScript) before to send the payment notification.
The user writes the discount-code insiede the custom variable where you can store whatever you want for your own tracking purposes.
// Check here on keypress with JS and AJAX for discount code validity
<input type="text" name="custom" value="" />
If the discount-code is verified, apply the discount to the amount variable. Something like this:
// Javascript and jQuery
if(verified == true) {
var discounted = $("[name='amount']").val() - ($("[name='amount']").val()/100)*10);
$("[name='amount']").val(discounted);
}
else {
alert("Discount code not found");
}
Then when you send the payment and get the information from paypal (the IPN response) process it like so:
$discountCode = $_POST["custom"];
// ... Discount-code validity check ...
$discountedAmount = $originalAmount - (($originalAmount/100)*10);
// And then check if the paid amount corresponds to the due amount
if($discountedAmount == $_POST["amount"])

Disabling Shipping Cost on Paypal for Non-Shipping Items

I'm currently using a PayPal form to checkout from a website. On shippable items, everything is working fine. The issue I am running into, is the website also offers non-shippable services (downloadable content, streaming videos online). I'm retrieving the correct responses from PayPal when a purchase is successful, only if they pay shipping costs calculated for a non-shippable service. Here is an excerpt from my code:
<form id="paypal-form" action="https://www.paypal.com/cgi-bin/webscr" method="post" style="display: none;">
<?php foreach($checkout as $key => $value): ?>
<input name="item_name_<?php echo $key + 1; ?>" value="<?php echo $value['name']; ?>" type="hidden">
<input name="amount_<?php echo $key + 1; ?>" value="<?php echo $value['amount']; ?>" type="hidden">
<input name="quantity_<?php echo $key + 1; ?>" value="<?php echo $value['quantity']; ?>" type="hidden">
<input name="item_number_<?php echo $key + 1; ?>" value="<?php echo $value['number']; ?>" type="hidden">
<?php endforeach; ?>
<input name="no_shipping" value="2" type="hidden">
<input name="return" value="WEB_SITE/thank-you.html" type="hidden">
<input name="notify_url" value="WEB_SITE/process.html" type="hidden">
<input name="cancel_return" value="WEB_SITE/shopping-cart.html" type="hidden">
<input name="business" value="EMAIL" type="hidden">
<input name="currency_code" value="USD" type="hidden">
<input name="cmd" value="_cart" type="hidden">
<input name="upload" value="1" type="hidden">
<input name="rm" value="2" type="hidden">
<input name="charset" value="utf-8" type="hidden">
</form>
I'm not finding support for the task I'm looking for on this forum or PayPal's. Is what I'm asking for possible?
Okay, after further research and trying new things I found a method that works. Simply using the field "shipping_(n)" and setting the value to "0" overrides the shipping calculation and marks it as $0.00. Thanks everyone for trying. Here is my corrected code:
<form id="paypal-form" action="https://www.paypal.com/cgi-bin/webscr" method="post" style="display: none;">
<?php foreach($checkout as $key => $value): ?>
<input name="item_name_<?php echo $key + 1; ?>" value="<?php echo $value['name']; ?>" type="hidden">
<input name="amount_<?php echo $key + 1; ?>" value="<?php echo $value['amount']; ?>" type="hidden">
<input name="quantity_<?php echo $key + 1; ?>" value="<?php echo $value['quantity']; ?>" type="hidden">
<input name="item_number_<?php echo $key + 1; ?>" value="<?php echo $value['number']; ?>" type="hidden">
<?php if(strpos($value['number'], 'PROGRAMS') !== FALSE): ?>
<input name="shipping_<?php echo $key + 1; ?>" value="0" type="hidden">
<?php endif; ?>
<?php endforeach; ?>
<input name="no_shipping" value="2" type="hidden">
<input name="return" value="WEB_SITE/thank-you.html" type="hidden">
<input name="notify_url" value="WEB_SITE/process.html" type="hidden">
<input name="cancel_return" value="WEB_SITE/shopping-cart.html" type="hidden">
<input name="business" value="EMAIL" type="hidden">
<input name="currency_code" value="USD" type="hidden">
<input name="cmd" value="_cart" type="hidden">
<input name="upload" value="1" type="hidden">
<input name="rm" value="2" type="hidden">
<input name="charset" value="utf-8" type="hidden">
</form>

I keep getting the shopping cart is empty....?

Where am I going wrong? the company email is correct I tried using the merchant Id but still no joy.....??
<?php $items = unserialize($_SESSION['items']); ?>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="business" value="cherished4ever#hotmail.co.uk">
<input type="hidden" name="currency_code" value="GBP">
<input type="hidden" name="return" value="http://www.cherished4ever.com/">
<?php
$suffix = 1;
foreach($items as $item):
?>
<input type="hidden" name="item_name_<?php echo $suffix; ?>" value="<?php echo $item['name']; ?>">
<input type="hidden" name="amount_<?php echo $suffix; ?>" value="<?php echo $item['price']; ?>">
<?php
$suffix++;
endforeach;
?>
Ok so i have got it working now however I just need some help with the checkout page showing the option name and not the item_name value set as the hidden output:
<select name="amount" style="width:150px;font:9px;margin-top:4px">
<option value="35.00">10 x 10</option>
<option value="55.00">20 x 20</option>
<option value="75.00">30 x 30</option>
</select><br>
Be sure to remember your session_start(); at the top of each page that uses the $_SESSION variable.
For more information, take a look at: http://www.php.net/session_start

Why is the PayPal sandbox telling me my shopping cart is empty?

On my local machine, when I submit this form, and I do have items inside the $_SESSION['cart'], the sandbox does recognize the items inside it. But when I tried it on a live server, and I submitted the form while there's an item inside $_SESSION['cart'], the sandbox says my cart is empty... Why is that?
<form action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" <?php if(empty($_SESSION['cart'])){echo "onsubmit=\"return false;\"";} ?>>
<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="upload" value="1" />
<input type="hidden" name="business" value="<?=$ppemail['email']?>" />
<?php
$i = 1;
foreach($_SESSION['cart'] as $id => $qty):
$product = $myCart->getProduct($id);
?>
<input type="hidden" name="item_name_<?php echo $i; ?>" value="<?php echo $product['ProductName']; ?>">
<input type="hidden" name="item_number_<?php echo $i; ?>" value="<?php echo $product['ProductID']; ?>">
<input type="hidden" name="amount_<?php echo $i; ?>" value="<?php echo $product['ProductOverridePrice']; ?>">
<input type="hidden" name="quantity_<?php echo $i; ?>" value="<?php echo $qty; ?>">
<?php
$i++;
endforeach;
?>
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="shipping_1" value="<?php echo $shipping; ?>">
<input type="hidden" name="return" value="<?php echo $the_class->settings[0]['DomainName'];?>shopping-cart-details.php">
<input type="hidden" name="cancel_return" value="<?php echo $the_class->settings[0]['DomainName'];?>">
<input type="hidden" name="notify_url" value="<?php echo $the_class->settings[0]['DomainName'];?>classes/paypal.php">
<input type="image" src="images/continue-btn.png" name="pay now" value="pay" />
</form>
i found the bug...the table that was at the live server....i meant, one of the field names was small caps..errr

Categories