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

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

Related

Sandbox paypal test account not returning variables

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>

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"])

Joomla 1.5 with Virtuemart 1.1.9

I want add changing on category page, I customize page websitelink.com/components\com_virtuemart\themes\default\templates\browse\browse_3.php
I add code
<div class="addtocart_buttonList">
<?php
$button_lbl = $VM_LANG->_('PHPSHOP_CART_ADD_TO');
$button_cls = 'addtocart_button';
if( CHECK_STOCK == '1' && ( $product_in_stock < 1 ) ) {
$button_lbl = $VM_LANG->_('VM_CART_NOTIFY');
$button_cls = 'notify_button';
$notify = true;
} else {
$notify = false;
}
?>
<form action="<?php echo $mm_action_url ?>index.php" method="post" name="addtocart" id="addtocart<?php echo $i ?>" class="addtocart_form" <?php if( $this->get_cfg( 'useAjaxCartActions', 1 ) && !$notify ) { echo 'onsubmit="handleAddToCart( this.id );return false;"'; } ?>>
<input type="submit" class="<?php echo $button_cls ?>" value="<?php echo $button_lbl ?>" title="<?php echo $button_lbl ?>" />
<input type="hidden" name="category_id" value="<?php echo #$_REQUEST['category_id'] ?>" />
<input type="hidden" name="product_id" value="<?php echo $product_id ?>" />
<input type="hidden" name="prod_id[]" value="<?php echo $product_id ?>" />
<input type="hidden" name="page" value="shop.cart" />
<input type="hidden" name="func" value="cartadd" />
<input type="hidden" name="Itemid" value="<?php echo $sess->getShopItemid() ?>" />
<input type="hidden" name="option" value="com_virtuemart" />
<input type="hidden" name="set_price[]" value="" />
<input type="hidden" name="adjust_price[]" value="" />
<input type="hidden" name="master_product[]" value="" />
</form>
</div>
After adding this line add to cart button appear on list items, but than i click on add to cart button error will come
Please enter a valid quatity for this item
How i can solve this?
You need to specify a quantity as an input field, hidden or otherwise. We only wanted the customer to be able to order one of an item, so added this code:
<input type="hidden" value="1" name="quantity">
Having that named input field satisifed the Virtuemart validation.

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

No Credit card option with paypal payment method

I am trying to learn integrating paypal for shopping checkouts. I have created a website with custom cart. Now I have added the paypal integration code after watching this http://www.youtube.com/watch?v=qDbTqeSUrQY&feature=relmfu
It works but only problem is when I try it out with sandbox, or real account from buyers perspective I can't use credit card i.e i need paypal account to perform. I have no idea whether I need to change the code or change settings in paypal. I tried going through their documents nothing is mention, it says i can accept credit card payments in express and standard accounts, but how?
The code I used
<form action="https://www.sandbox.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="business" value="abhishek.salian.85#gmail.com">
<?php if($_SESSION['cart']) { $total = 0;$i=0;
foreach($_SESSION['cart'] as $product_id => $quantity) {
$product_details = get_product_details($product_id);
$row_cost = $product_details[product_price] * $quantity;
$total = $total + $row_cost; $x=$i+1; ?>
<input type="hidden" name="item_name_<?php echo $x; ?>" value="<?php echo $product_details[product_title]; ?>">
<input type="hidden" name="quantity_<?php echo $x; ?>" value="<?php echo $_SESSION['cart'][product_id]; ?>">
<input type="hidden" name="amount_<?php echo $x; ?>" value="<?php echo $product_details[product_price]; ?>">
<?php $product_id_array = $product_details[product_id]."-".$_SESSION['cart'][product_id].",";$i++; } } ?>
<input type="hidden" name="custom" value="<?php echo $product_id_array; ?>">
<input type="hidden" name="notify_url" value="<?php echo WEBSITE.'paypal/ipn.php'; ?>">
<input type="hidden" name="return" value="<?php echo WEBSITE.'products/thank_you'; ?>">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Retun to Flash Shop">
<input type="hidden" name="cancel_return" value="<?php echo WEBSITE.'products/cancel'; ?>">
<input type="hidden" name="lc" value="AU">
<input type="hidden" name="currency_code" value="AUD">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - it's fast, free and secure!"></form>
I believe that's a setting in your seller account - not something you pass in with the form.
Under Profile, fund the "Website Payment Preferences" link and then look for the "PayPal Account Optional" section. Choose "On" and save your settings.

Categories