Contact Form 7 and PayPal IPN integration - php

I need add PayPal IPN on my site in a specific contact form.
I have integrate the funcions for IPN (listener and other) but I don't known how to invoke the Paypal from the CF7" and send the email only AFTER the payment confirm.
I think I have to use the wpcf7_before_send_mail function, but I don't know how.
I add this simple wpcf7_before_send_mail function in my functions.php
add_action("wpcf7_before_send_mail", "wpcf7_do_something_else");
function wpcf7_do_something_else($cf7) {
// get the contact form object
$wpcf = WPCF7_ContactForm::get_current();
$form_id = $contact_form->posted_data['_wpcf7'];
if ($form_id == 2969) {
if (/*Check if payment in the PayPal it's ok */) {
//Send mail and thank you page
} else {
$wpcf->skip_mail = true;
}
}
return $wpcf;
}
But I don't known to check the IPN (i think first I have redirect to paypal url and after i need listen the paypal response).
Also, i have insert in fuction only the $wpcf->skip_mail = true; but the mail but the email is sent however.
Can you help me?
Regards,
Marco

If this is a form post that then redirects the user to a PayPal Payments Standard checkout, there will be no completed payment until later (and some portion of payments will go unpaid), so you will not be able to check for payment at the time the form is posted. That comes later.
The variable notify_url to be specified for IPN is documented here.

Related

How to send a HTTP response back to shopify for the payment session creation to be successful?

Am creating a payment app on Shopify but am stuck.. any assistance will be appreciated.
Shopify must receive an HTTP 2xx response with a redirect url for the payment session creation to be successful.
How do you send such when no endpoint is provided or probably am missing something but I have done all the configuration correctly. Am stuck at Your payment can’t be processed for technical reasons - above must be the cause just I don't know how to go about it. The redirect url is required to redirect customer back to partner's payment page for payment processing.
Relevant documentation: https://shopify.dev/apps/payments/processing-a-payment#http-response-example
Here is the endpoint with the redirect URL and status code.
POST https://{shop_domain}/payments_apps/api/{api_version}/graphql.json
mutation PaymentSessionResolve($id: ID!, $authorizationExpiresAt: DateTime) {
paymentSessionResolve(id: $id, authorizationExpiresAt: $authorizationExpiresAt) {
paymentSession {
id
status {
code
}
nextAction {
action
context {
... on PaymentSessionActionsRedirect {
redirectUrl
}
}
}
}
userErrors {
field
message
}
}
}

How to return from PayPal purchase and display a success or failure message?

I can't seem to figure out proper way to handle the return url from a paypal purchase and display an alert message.
Here is how I understand process:
From my site send user to paypal, having set returnurl to be the url corresponding to what they purchased.
Paypal processes, then hits my IPN listener
User sees message in paypal window and button Return to merchant (I am in sandbox testing)
User hits Return to Merchant and is back to the page where they made purchase from
Ok, so in my IPN listener I do this:
if( ! session_id() ) {
session_start();
}
then a bunch of checks, then
$_SESSION['alert-success'] = 'Thank you for your purchase!';
or
$_SESSION['alert-danger'] = 'There was a problem processing your order';
Now in this IPN listener I can set more fine tuned messages, but for now trying to get something basic working.
Then in the product page they are returned to I have this
<div class="flash-message">
<?php
foreach (['danger', 'warning', 'success', 'info'] as $msg) {
$msgtype = 'alert-' . $msg;
$issession=0;
if(isset($_SESSION[$msgtype])) {
$p = "<p class=\"alert " . $msgtype. "\">" . $_SESSION[$msgtype] . "</p>";
echo $p;
$issession=1;
}
}
if ($issession) {
session_destroy();
}
?>
</div>
Now, this isn't doing anything. So it seems whatever session variable is being set in IPN listener is not available when user gets back to the return url.
I have in my paypal form this
<input type="hidden" name="rm" value="2">
in order to have the $_POST data and I confirmed that when user hits Return to Merchant the post data is there with 'payer_status' => string 'VERIFIED'
So what gives? What is the point of having an IPN listener do a bunch of work, if the return url is only coming back with minimal info? It is the status info I get in IPN listener that I want to be able to display in my flash message area, and I thought my using session variables would achieve that but was wrong.
All above said, what is the proper way to do this?
Thanks!
The reason using sessions doesn't work because paypal is the one sending data to the IPN listener. The session wouldn't be stored on the user's browser since they aren't the one sending data to the IPN listener. Can you not store the information gathered from the IPN listener into a database? That's what I did for my paypal purchases. When the payment successfully went through, I stored the payment data gathered from the IPN listener and stored it with their user ID in the database. When they were redirected to a success page, I grabbed the information from the database and displayed it.

how to handle paypal notify url in our site?

I have paypal form which will have ipn notify url. i have setup notify url in my paypal ipn settings. but it doesn't return to my site. after analyzed for more than 2 days. i found that my controller construct itself i check for current user session exist or not? should i remove the session check in construct not to check for session. Please advise on this.
this is my code. i am using codeigniter application.
function __construct() {
parent::__construct();
$this->load->model('user_model');
// if (!isEmployee()) {
// redirect('information');
//
// }
}
function paypal_ipn(){
$txn_id = $_POST['txn_id'];
$data = array("paypal_transaction_id"=>$txn_id);
$this->db->insert("payments",$data);
}
I have commented out now
Notify url is part of the Instant Payment Notification (IPN) mechanism of PayPal. This URL is called by PayPal to notify about a payment. It is not part of the user session at all so do not check for it.
I recommend you to read the documentation and get the example from here:
documentation IPN

Paypal Sandbox IPN "Email does not match seller"

I am trying to create a paypal subscription button with paypal sandbox. I have created the dev account, created sandbox business and test accounts. Logged into my business test account, went to merchant services -> my saved buttons.
Found the subscription button and copied the code.
I have uploaded the form to my codeigniter controller at: http://422clients.com/ftg/paypal/test
I am using this paypal IPN library for codeigniter: https://github.com/orderly/codeigniter-paypal-ipn
I am using unmodified controller code for now to handle the IPN response because it writes it to the database just fine for testing:
function ipn()
{
$this->load->library('PayPal_IPN'); // Load the library
// Try to get the IPN data.
if ($this->paypal_ipn->validateIPN())
{
// Succeeded, now let's extract the order
$this->paypal_ipn->extractOrder();
// And we save the order now (persist and extract are separate because you might only want to persist the order in certain circumstances).
$this->paypal_ipn->saveOrder();
// Now let's check what the payment status is and act accordingly
if ($this->paypal_ipn->orderStatus == PayPal_IPN::PAID)
{
//Enable database subscription...
}
}
else // Just redirect to the root URL
{
$this->load->helper('url');
redirect('/', 'refresh');
}
}
My problem is that when i click the button on the site and pay with my sandbox test account everything looks totally fine and the payment goes through, but when I look in my database it says: Status: ERROR.
Reason Field says: email business_test#422studios.com does not match seller
business_test#422studios.com is the sandbox account that i generated the button from.
I'm totally lost here. Any help is appreciated.
In your config file, make sure you have this:
$config['paypal_ipn_sandbox_settings'] = array(
'email' => 'business_test#422studios.com',
'url' => 'https://www.sandbox.paypal.com/cgi-bin/webscr',
'debug' => TRUE
);
And that you pass this value in your form:
<input type="hidden" name="business" value="business_test#422studios.com">

pay pal returnurl to return users pay pal account email address

I'm using PHP.
Is it possible when paypal returns to my site after taken the payment that it could also pass me the email address (paypal username) so that I may use this to send them an email?
I'm trying to keep my site as simple as possible and I don't want to ask the customer for a load of information up front.
My ideal flow would be:
Take Payment > Add record to database > email customer with username and password
The customer can then log in and fill the rest in at their own leisure.
Is it possible to get this variable?
Thanks in advance.
Yes...see the receiver_email variable that was passed back.
Guide here: https://cms.paypal.com/cms_content/GB/en_GB/files/developer/IPNGuide.pdf
See Pages 42-43
This page shows the things you can get back from PayPal after they process a payment.
IPN and PDT Variables
Here is on example I made in Codeigniter.
/* Receives a form data from paypal that is processed after payment goes through.
* It will update the database entry with a transaction id.
*/
function epayment_notify(){
$this->load->model('pament_notice_model');
echo "Hi<br>";
$postvars = isset($_POST)? $_POST : array("no post");
/* echo "</pre>Post Vars:<br><pre>";
print_r($postvars);
echo "</pre>Refurl:<br><pre>";
Check for the transaction ID and put it in the pament_notice table if possible.
*/
if (isset($postvars['item_number']) && $postvars['item_number'] > 0 && isset($postvars['txn_id'])){
/* make sure that payment_number is empty on this row. */
log_message('info',"PayPal transaction received.");
if ($this->pament_notice_model->make_sure_payment_number_is_empty($postvars['item_number'])){
//writes the message to the local log file in CI
log_message('debug',"PayPal payment number is empty.");
/* set the txn_id in the slot */
$data = array('payment_number' => $postvars['txn_id']);
$this->db->where('entry_id',$postvars['item_number']);
$this->db->update('pament_notice',$data);
log_message('debug',"payment number updated. item:".$postvars['item_number']);
log_message('debug',"payment actual cost. payment_gross:".$postvars['payment_gross']);
} else {
log_message('error',"PayPal transaction attempted on non-empty payment number.");
if (isset($postvars['item_number']))
log_message('error',"failed request item_number :".$postvars['item_number']);
if (isset($postvars['txn_id']))
log_message('error',"failed request txn_id:". $postvars['txn_id']);
}
} else {
log_message('info',"PayPal payment transaction sent with invalid data.");
if (!isset($postvars['item_number']))
log_message('error',"item_number is not set");
if (isset($postvars['item_number']))
log_message('error',"failed request item_number :".$postvars['item_number']);
if (!isset($postvars['txn_id']))
log_message('error',"txn_id is not set\n");
if (isset($postvars['txn_id']))
log_message('error',"failed request txn_id:". $postvars['txn_id']);
}
}
There are a couple of way of handling this.
The easiest method is to collect that data prior to sending the user to paypal.
Store it in the users current session, and have paypal redirect the user back to your page automatically.
The second is to set up the Payment Data Transfer option. I haven't fully implemented this but there is Documentation on paypal's developer site. It basically sends a token back to your site when the user makes payment and you have to perform another HTTP post to retrieve the user data.

Categories