From 6b0b4b32dc7de7fe584dab06e130e85fb6e126bd Mon Sep 17 00:00:00 2001 From: devansh bawari Date: Mon, 8 Mar 2021 11:34:28 +0530 Subject: [PATCH] IPN Issue Fixed --- packages/Webkul/Paypal/src/Helpers/Ipn.php | 42 +++++++++++-------- .../Http/Controllers/StandardController.php | 10 ++--- packages/Webkul/Paypal/src/Http/routes.php | 2 +- .../Webkul/Paypal/src/Payment/Standard.php | 23 +++++++--- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/packages/Webkul/Paypal/src/Helpers/Ipn.php b/packages/Webkul/Paypal/src/Helpers/Ipn.php index 855e0d479..b2706d05c 100755 --- a/packages/Webkul/Paypal/src/Helpers/Ipn.php +++ b/packages/Webkul/Paypal/src/Helpers/Ipn.php @@ -2,34 +2,42 @@ namespace Webkul\Paypal\Helpers; +use Webkul\Paypal\Payment\Standard; use Webkul\Sales\Repositories\OrderRepository; use Webkul\Sales\Repositories\InvoiceRepository; class Ipn { /** - * Ipn post data + * IPN post data. * * @var array */ protected $post; /** - * Order object + * Standard $paypalStandard + * + * @var \Webkul\Paypal\Payment\Standard + */ + protected $paypalStandard; + + /** + * Order $order * * @var \Webkul\Sales\Contracts\Order */ protected $order; /** - * OrderRepository object + * OrderRepository $orderRepository * * @var \Webkul\Sales\Repositories\OrderRepository */ protected $orderRepository; /** - * InvoiceRepository object + * InvoiceRepository $invoiceRepository * * @var \Webkul\Sales\Repositories\InvoiceRepository */ @@ -40,20 +48,24 @@ class Ipn * * @param \Webkul\Sales\Repositories\OrderRepository $orderRepository * @param \Webkul\Sales\Repositories\InvoiceRepository $invoiceRepository + * @param \Webkul\Paypal\Payment\Standard $paypalStandard * @return void */ public function __construct( + Standard $paypalStandard, OrderRepository $orderRepository, InvoiceRepository $invoiceRepository ) { + $this->paypalStandard = $paypalStandard; + $this->orderRepository = $orderRepository; $this->invoiceRepository = $invoiceRepository; } /** - * This function process the ipn sent from paypal end + * This function process the IPN sent from paypal end. * * @param array $post * @return null|void|\Exception @@ -80,7 +92,7 @@ class Ipn } /** - * Load order via ipn invoice id + * Load order via IPN invoice id. * * @return void */ @@ -92,7 +104,7 @@ class Ipn } /** - * Process order and create invoice + * Process order and create invoice. * * @return void */ @@ -112,13 +124,13 @@ class Ipn } /** - * Prepares order's invoice data for creation + * Prepares order's invoice data for creation. * * @return array */ protected function prepareInvoiceData() { - $invoiceData = ["order_id" => $this->order->id,]; + $invoiceData = ['order_id' => $this->order->id]; foreach ($this->order->items as $item) { $invoiceData['invoice']['items'][$item->id] = $item->qty_to_invoice; @@ -128,24 +140,20 @@ class Ipn } /** - * Post back to PayPal to check whether this request is a valid one + * Post back to PayPal to check whether this request is a valid one. * * @return bool */ protected function postBack() { - if (array_key_exists('test_ipn', $this->post) && 1 === (int) $this->post['test_ipn']) { - $url = 'https://www.sandbox.paypal.com/cgi-bin/webscr'; - } else { - $url = 'https://www.paypal.com/cgi-bin/webscr'; - } + $url = $this->paypalStandard->getIPNUrl(); $request = curl_init(); curl_setopt_array($request, [ CURLOPT_URL => $url, CURLOPT_POST => TRUE, - CURLOPT_POSTFIELDS => http_build_query(array('cmd' => '_notify-validate') + $this->post), + CURLOPT_POSTFIELDS => http_build_query(['cmd' => '_notify-validate'] + $this->post), CURLOPT_RETURNTRANSFER => TRUE, CURLOPT_HEADER => FALSE, ]); @@ -161,4 +169,4 @@ class Ipn return false; } -} \ No newline at end of file +} diff --git a/packages/Webkul/Paypal/src/Http/Controllers/StandardController.php b/packages/Webkul/Paypal/src/Http/Controllers/StandardController.php index 8d1e2f750..1174a1efe 100755 --- a/packages/Webkul/Paypal/src/Http/Controllers/StandardController.php +++ b/packages/Webkul/Paypal/src/Http/Controllers/StandardController.php @@ -2,21 +2,21 @@ namespace Webkul\Paypal\Http\Controllers; +use Webkul\Paypal\Helpers\Ipn; use Webkul\Checkout\Facades\Cart; use Webkul\Sales\Repositories\OrderRepository; -use Webkul\Paypal\Helpers\Ipn; class StandardController extends Controller { /** - * OrderRepository object + * OrderRepository $orderRepository * * @var \Webkul\Sales\Repositories\OrderRepository */ protected $orderRepository; /** - * Ipn object + * IPN $ipnHelper * * @var \Webkul\Paypal\Helpers\Ipn */ @@ -62,7 +62,7 @@ class StandardController extends Controller } /** - * Success payment + * Success payment. * * @return \Illuminate\Http\Response */ @@ -78,7 +78,7 @@ class StandardController extends Controller } /** - * Paypal Ipn listener + * Paypal IPN listener. * * @return \Illuminate\Http\Response */ diff --git a/packages/Webkul/Paypal/src/Http/routes.php b/packages/Webkul/Paypal/src/Http/routes.php index 5f0b97ab9..03b529121 100755 --- a/packages/Webkul/Paypal/src/Http/routes.php +++ b/packages/Webkul/Paypal/src/Http/routes.php @@ -16,4 +16,4 @@ Route::group(['middleware' => ['web']], function () { }); }); -Route::get('paypal/standard/ipn', 'Webkul\Paypal\Http\Controllers\StandardController@ipn')->name('paypal.standard.ipn'); +Route::post('paypal/standard/ipn', 'Webkul\Paypal\Http\Controllers\StandardController@ipn')->name('paypal.standard.ipn'); diff --git a/packages/Webkul/Paypal/src/Payment/Standard.php b/packages/Webkul/Paypal/src/Payment/Standard.php index a41f81f12..d1b88db94 100755 --- a/packages/Webkul/Paypal/src/Payment/Standard.php +++ b/packages/Webkul/Paypal/src/Payment/Standard.php @@ -5,14 +5,14 @@ namespace Webkul\Paypal\Payment; class Standard extends Paypal { /** - * Payment method code + * Payment method code. * * @var string */ protected $code = 'paypal_standard'; /** - * Line items fields mapping + * Line items fields mapping. * * @var array */ @@ -24,7 +24,7 @@ class Standard extends Paypal ]; /** - * Return paypal redirect url + * Return paypal redirect url. * * @return string */ @@ -34,7 +34,20 @@ class Standard extends Paypal } /** - * Return form field array + * Return paypal IPN url. + * + * @return string + */ + public function getIPNUrl() + { + return $this->getConfigData('sandbox') + ? 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr' + : 'https://ipnpb.paypal.com/cgi-bin/webscr'; + } + + + /** + * Return form field array. * * @return array */ @@ -89,7 +102,7 @@ class Standard extends Paypal } /** - * Add shipping as item + * Add shipping as item. * * @param array $fields * @param int $i