From 3e2a19fe5328442c6134c83acb05838d652b194f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 5 Jul 2021 21:57:59 +1000 Subject: [PATCH] Fixes for payfast --- app/Console/Kernel.php | 2 +- app/PaymentDrivers/PayFast/CreditCard.php | 140 +++++++++++++++++++- app/PaymentDrivers/PayFastPaymentDriver.php | 4 +- 3 files changed, 143 insertions(+), 3 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 7c777677086e..a97b90b1730c 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -64,7 +64,7 @@ class Kernel extends ConsoleKernel $schedule->job(new AutoBillCron)->dailyAt('00:30')->withoutOverlapping(); - $schedule->job(new SchedulerCheck)->everyFiveMinutes(); + $schedule->job(new SchedulerCheck)->daily()->withoutOverlapping(); /* Run hosted specific jobs */ if (Ninja::isHosted()) { diff --git a/app/PaymentDrivers/PayFast/CreditCard.php b/app/PaymentDrivers/PayFast/CreditCard.php index 44e9f2329ad5..33bbbb9e3ac8 100644 --- a/app/PaymentDrivers/PayFast/CreditCard.php +++ b/app/PaymentDrivers/PayFast/CreditCard.php @@ -185,9 +185,147 @@ class CreditCard } + /* + [2021-07-05 11:21:24] local.INFO: array ( + 'm_payment_id' => 'B7G9Q2vPhqkLEoMwwY1paXvPGuFxpbDe', + 'pf_payment_id' => '1410364', + 'payment_status' => 'COMPLETE', + 'item_name' => 'purchase', + 'item_description' => 'Invoices: ["0001"]', + 'amount_gross' => '100.00', + 'amount_fee' => '-2.30', + 'amount_net' => '97.70', + 'custom_str1' => NULL, + 'custom_str2' => NULL, + 'custom_str3' => NULL, + 'custom_str4' => NULL, + 'custom_str5' => NULL, + 'custom_int1' => NULL, + 'custom_int2' => NULL, + 'custom_int3' => NULL, + 'custom_int4' => NULL, + 'custom_int5' => NULL, + 'name_first' => NULL, + 'name_last' => NULL, + 'email_address' => NULL, + 'merchant_id' => '10023100', + 'signature' => '3ed27638479fd65cdffb0f4910679d10', + 'q' => '/payment_notification_webhook/EhbnVYyzJZyccY85hcHIkIzNPI2rtHzznAyyyG73oSxZidAdN9gf8BvAKDomqeHp/4openRe7Az/WPe99p3eLy', + ) + + */ public function paymentResponse(Request $request) { - dd($request->all()); + $response_array = $request->all(); + + $state = [ + 'server_response' => $request->all(), + 'payment_hash' => $request->input('m_payment_id'), + ]; + + $this->payfast->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, $state); + $this->payfast->payment_hash->save(); + + if($response_array['payment_status'] == 'COMPLETE') { + + $this->payfast->logSuccessfulGatewayResponse(['response' => json_decode($response_array), 'data' => $this->payfast->payment_hash], SystemLog::TYPE_PAYFAST); + + return $this->processSuccessfulPayment(); + } + else { + $this->processUnsuccessfulPayment($response_array); + } } } + + +private function processSuccessfulPayment() + { + $stripe_method = $this->stripe->getStripePaymentMethod($this->stripe->payment_hash->data->server_response->payment_method); + + $data = [ + 'payment_method' => $this->stripe->payment_hash->data->server_response->payment_method, + 'payment_type' => PaymentType::parseCardType(strtolower($stripe_method->card->brand)), + 'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->server_response->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()), + 'transaction_reference' => optional($this->stripe->payment_hash->data->payment_intent->charges->data[0])->id, + 'gateway_type_id' => GatewayType::CREDIT_CARD, + ]; + + $this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['amount' => $data['amount']]); + $this->stripe->payment_hash->save(); + + if ($this->stripe->payment_hash->data->store_card) { + $customer = new \stdClass; + $customer->id = $this->stripe->payment_hash->data->customer; + + $this->stripe->attach($this->stripe->payment_hash->data->server_response->payment_method, $customer); + + $stripe_method = $this->stripe->getStripePaymentMethod($this->stripe->payment_hash->data->server_response->payment_method); + + $this->storePaymentMethod($stripe_method, $this->stripe->payment_hash->data->payment_method_id, $customer); + } + + $payment = $this->stripe->createPayment($data, Payment::STATUS_COMPLETED); + + SystemLogger::dispatch( + ['response' => $this->stripe->payment_hash->data->server_response, 'data' => $data], + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_SUCCESS, + SystemLog::TYPE_STRIPE, + $this->stripe->client, + $this->stripe->client->company, + ); + + return redirect()->route('client.payments.show', ['payment' => $this->stripe->encodePrimaryKey($payment->id)]); + } + + private function processUnsuccessfulPayment($server_response) + { + PaymentFailureMailer::dispatch($this->stripe->client, $server_response->cancellation_reason, $this->stripe->client->company, $server_response->amount); + + PaymentFailureMailer::dispatch( + $this->stripe->client, + $server_response, + $this->stripe->client->company, + $server_response->amount + ); + + $message = [ + 'server_response' => $server_response, + 'data' => $this->stripe->payment_hash->data, + ]; + + SystemLogger::dispatch( + $message, + SystemLog::CATEGORY_GATEWAY_RESPONSE, + SystemLog::EVENT_GATEWAY_FAILURE, + SystemLog::TYPE_STRIPE, + $this->stripe->client, + $this->stripe->client->company, + ); + + throw new PaymentFailed('Failed to process the payment.', 500); + } + + private function storePaymentMethod(PaymentMethod $method, $payment_method_id, $customer) + { + try { + $payment_meta = new \stdClass; + $payment_meta->exp_month = (string) $method->card->exp_month; + $payment_meta->exp_year = (string) $method->card->exp_year; + $payment_meta->brand = (string) $method->card->brand; + $payment_meta->last4 = (string) $method->card->last4; + $payment_meta->type = GatewayType::CREDIT_CARD; + + $data = [ + 'payment_meta' => $payment_meta, + 'token' => $method->id, + 'payment_method_id' => $payment_method_id, + ]; + + $this->stripe->storeGatewayToken($data, ['gateway_customer_reference' => $customer->id]); + } catch (\Exception $e) { + return $this->stripe->processInternallyFailedPayment($this->stripe, $e); + } + } diff --git a/app/PaymentDrivers/PayFastPaymentDriver.php b/app/PaymentDrivers/PayFastPaymentDriver.php index 54affcae8845..058d4113291b 100644 --- a/app/PaymentDrivers/PayFastPaymentDriver.php +++ b/app/PaymentDrivers/PayFastPaymentDriver.php @@ -203,8 +203,10 @@ class PayFastPaymentDriver extends BaseDriver break; default: - + + $payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$hash])->first(); return $this->setPaymentMethod(GatewayType::CREDIT_CARD) + ->setPaymentHash($payment_hash) ->processPaymentResponse($request); break; }