Post to database and to paypal using one button - php

I'm actually stuck, sending a form post request to multiple action receiver.
What am I trying to do?
I'm trying to send an post request to my database and to paypal at the same time using one button.
After the user submitted the form, I want my PHP script to post those informations to the database before redirecting to paypal. To work with those sent informations.
My actual code:
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="item_name" id="item_name" value="Helpful.ninja coins"/>
<input type="hidden" name="business" value="pr0bpayment#gmail.com">
<input type="hidden" name="return" value="http://helpful.ninja/?username=payment_complete=1">
<input type="hidden" name="image_url" value="http://helpful.ninja/assets/images/logo_dark.png">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="amount" class="amountToPay" value="">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<button type="submit" name="submit" alt="PayPal - The safer, easier way to pay online!" class="btn bg-danger-400">Purchase now <i class="icon-paypal position-right"></i></button>
</form>
<?php
require_once('../handling/database.php');
require_once('../handling/user.class.php');
require_once('../handling/purchase.class.php');
require_once('../handling/config.php');
$user = new user($_SESSION['Username']);
$config = new config();
if(!empty($_POST)){
$purchase = new purchase($user->username, $user->coins, $_POST['coinValue'], $config->coin_price);
$purchase->insert_purchase($user->username, $user->email, $_SERVER['REMOTE_ADDR'], $purchase->ordernumber, $purchase->purchase_amount, $purchase->price);
}
?>
<script type="text/javascript">
$(".coinValue").bind("change paste keyup", function() {
$(".amountToPay").val($(this).val() * <?php echo $config->coin_price; ?>);
});
</script>
So I'm trying to post the following code:
if(!empty($_POST)){
$purchase = new purchase($user->username, $user->coins, $_POST['coinValue'], $config->coin_price);
$purchase->insert_purchase($user->username, $user->email, $_SERVER['REMOTE_ADDR'], $purchase->ordernumber, $purchase->purchase_amount, $purchase->price);
}
to the databse before the user gets redirected to paypal.
Does anyone have an idea how to manage this?

The best option is probably to use one of the other PayPal apis that is designed for this type of thing, but sticking with the simple paypal button api, you can use the fact that the paypal api endpoint will accept the parameters in a get request as well as a post.
So you can post the form to your php handler then perform a header redirect to paypal with the data:
<?php
//php must be at top of file, before ANY output, in order to use header redirect
require_once('../handling/database.php');
require_once('../handling/user.class.php');
require_once('../handling/purchase.class.php');
require_once('../handling/config.php');
$user = new user($_SESSION['Username']);
$config = new config();
if(!empty($_POST)){
$purchase = new purchase($user->username, $user->coins, $_POST['coinValue'], $config->coin_price);
$purchase->insert_purchase($user->username, $user->email, $_SERVER['REMOTE_ADDR'], $purchase->ordernumber, $purchase->purchase_amount, $purchase->price);
//redirect to paypal
header('Location: https://www.paypal.com/cgi-bin/webscr?' . http_build_query($_POST));
die();
}
?>
<!-- post form to itsself, not paypal-->
<form action="" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="item_name" id="item_name" value="Helpful.ninja coins"/>
<input type="hidden" name="business" value="pr0bpayment#gmail.com">
<input type="hidden" name="return" value="http://helpful.ninja/?username=payment_complete=1">
<input type="hidden" name="image_url" value="http://helpful.ninja/assets/images/logo_dark.png">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="amount" class="amountToPay" value="">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<button type="submit" name="submit" alt="PayPal - The safer, easier way to pay online!" class="btn bg-danger-400">Purchase now <i class="icon-paypal position-right"></i></button>
</form>

Related

Paypal not returning CUSTOM field variable as post data

I have a paypal form where I have a custom and CatDescription field. But
From
<form name="paypalForm" action="paypal.php" method="post" id="paypalform">
<input type="hidden" name="business" value="shubhamjha900#gmail.com">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="id" value="">
<input type="hidden" name="CatDescription">
<input type="hidden" name="custom">
<input type="hidden" name="key" value="<?php echo md5(date("Y-m-d:").rand()); ?>">
<input type="hidden" name="payment" required>
<input type="submit" value="Pay Now">
</form>
The problem is that on my IPN page when the post data are sent back they send all the data but not the custom and CatDescription field data. Please tell me what can be the issue and how can I solve it. Can I use any other field by which I can send data? Whatever be the solution please help me.

Paypal Not working on localhost

I am working a codeigniter project on localhost and In which I am using paypal sandbox payment method for payment.
In which I am facing a problem, The return url is not redirect automatically and also when I click on return merchant button then It is not returning any parameter to my url.
I have set all setting of paypal, I have set the auto return on from paypal account and also set the return url but still i am not getting any response.I am usging this form and parameters.
$paypal_url='https://www.sandbox.paypal.com/cgi-bin/webscr';
<form action="<?php echo $paypal_url; ?>" method="post" name="frmPayPal1">
<input type="hidden" name="business" value="<?php echo $paypal_id; ?>">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="item_name" value="PHPGang Payment">
<input type="hidden" name="item_number" value="1">
<input type="hidden" name="userid" value="1">
<input type="hidden" name="credits" value="510">
<input type="hidden" name="amount" value="20">
<input type="hidden" name="cpp_header_image" value="http://112.196.5.114/psychics/component/images/logo.png">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="notify_url" value="http://112.196.5.114/psychics/dashboard/profile/return_url">
<input type="hidden" name="cancel_return" value="http://112.196.5.114/psychics/dashboard/profile/cancel_return">
<input type="hidden" name="return" value="http://112.196.5.114/psychics/dashboard/profile/return_url">
<button type="submit" class="btn btn-success"> Add Funds</button>
</form>
I did not get where is my fault.
Your code is fine, just add below line in your code, clear your browser's cache-cookies and try again:
< input type='hidden' name='rm' value='2'>
NOTE 1: After adding above code, PayPal will return response in POST method.
NOTE 2: Recheck and confirm that auto return must be enable from your PayPal business verified account.
You can check response by adding below line in your return page within PHP tag:
print_r($_REQUEST); OR print_r($_POST);
For more help about PayPal button parameters: check documentation

Paypal dynamic return url not working

I am trying to make a paypal payment and I have a problem when returning to my page after the payment is completed.
I want to do it dynamically (so I do not want to set it in the paypal's account option, as I may use this account for other things). This is the form I send to paypal:
<form id="paypalForm" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="POST">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="notify_url" value="http://zopomobile.es/pruebas/modules/rmaforsat/ipn_paypal.php">
<input type="hidden" name="item_name_1" value="Pago de su orden a Zopomobile.">
<input type="hidden" name="item_number_1" value="1">
<input type="hidden" name="amount_1" value="259.12">
<input type="hidden" name="currency_code" value="EUR">
<input type="hidden" name="custom" value="152">
<input type="hidden" name="business" value="admin-facilitator#zopomovil.es">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="return" value="http://zopomobile.es/pruebas">
<input type="hidden" name="cancel" value="http://zopomobile.es/pruebas/NO">
<input type="hidden" name="rm" value="2">
</form>
I have tried changing the "return" variable to "return_url", still not working.
If you want to automatically re-direct to "http://www.zopomobile.es/pruebas/"
then you need to turn on "Auto Return" on your PayPal profile.
Right now, Auto Return is "OFF" in your profile.
The return parameter is return
https://www.sandbox.paypal.com/us/cgi-bin/webscr?cmd=_profile-website-payments
Check above link to turn on the "Auto Return" feature.

paypal notify_url not working and tx ID variable not pass to thank you page

Here is my paypal buy now html code:
<form name="_xclick" action="https://www.paypal.com/ca/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="alex#xumanii.com">
<input type="hidden" name="item_name" value="Bewolf Shooping Cart">
<input type="hidden" name="amount" value="<? echo $grandtotal2;?>">
<input type="hidden" name="add" value="1">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="currency_code" value="CAD">
<input type="hidden" name="member_id" value="<? echo $info['member_id'];?>">
<input type="hidden" name="return" value="http://www.bewolfclothing.com/thankyou.php">
<input type="hidden" name="notify_url" value="http://www.bewolfclothing.com/notify_paypal2.php">
<input type="hidden" name="cancel_return" value="http://www.bewolfclothing.com/mycart.php">
<input type="hidden" name="undefined_quantity" value="0">
<div class="submit-container"><input class="submit-button" type="submit" name="submit" value="CHECK OUT" /></div>
</form>
In my paypal account, under profile, my selling tools, I have set the AUTO RETURN to ON and I put the thankyou page in there.
After the purchase, it goes to the thankyou page, but the $_REQUEST['tx'] for the paypal transaction ID is not working and the notify_url NEVER gets called.
I searched online and saw that lots of people seem to have trouble with that, but I couldn't find a solution.
notify_url is used to Instant Payment Notification and the url should always be a live URL.
To get the tx ID, you need to
-Auto Return is set to ON
-Valid Return URL
-Payment Data Transfer is set to ON
and in your "http://www.bewolfclothing.com/thankyou.php"
$tx_id = $_GET['tx']; //gives you tx id

Echoing textbox content into POST request not working

I am trying to take the textbox content from "mcname" ($custom) and POST that into "item_name" however it doesn't POST anything? I've looked around, but I still cannot find my solution?
At the top of my script before any HTML:
<?php
$custom = $_post['mcname'];
?>
The textbox, form post, and button in the middle of the script.
<input type="text" name="mcname" id="mcname" placeholder="Enter your username hereā€¦" />
<br/>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" id="formval">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="__#gmail.com">
<input type="hidden" name="item_name" value="<?php echo $custom; ?>">
<input type="hidden" name="amount" value="$10.00">
<input type="hidden" name="no_shipping" value="1">
<input type="hidden" name="no_note" value="1">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="bn" value="PP-BuyNowBF">
<input type="hidden" name="return" value="http://__.com/upgrade" />
<input type="hidden" name="cancel_return" value="http://__.com/upgrade" />
<input type="hidden" name="notify_url" value="http://__.com/ipn/ipn.php">
<input type="hidden" name="custom" value="Iron">
<button type="submit" class="btn btn-primary" href="#" rel="tooltip" data-placement="bottom" title="Click here to continue to PayPal." ><i class="icon-shopping- cart"></i>Checkout</a>
</form>
POST is case sensitive, try:
$custom = $_POST['mcname'];
Well, your code is fine, the problem is:
$custom = $_post['mcname'];
change it to:
$custom = $_POST['mcname'];
POST is a super global and must be capitalized:
$custom = $_POST['mcname'];
Also, your textarea element is outside of your form, it needs to be inside the form tags.

Categories