From d7277b7c6db032200c582922cbd2af0ae9f8aa6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Wed, 27 Jan 2021 15:58:19 +0100 Subject: [PATCH] wip --- app/PaymentDrivers/BaseDriver.php | 2 +- app/PaymentDrivers/CheckoutCom/Utilities.php | 12 +++- .../CheckoutComPaymentDriver.php | 65 +++++++++++++++++-- 3 files changed, 69 insertions(+), 10 deletions(-) diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 16f2c3442cb5..5a6e4b751744 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -230,7 +230,7 @@ class BaseDriver extends AbstractPaymentDriver $payment->service()->updateInvoicePayment($this->payment_hash); - if ($this->client->getSetting('client_online_payment_notification')) + if ($this->client->getSetting('client_online_payment_notification')) $payment->service()->sendEmail(); event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); diff --git a/app/PaymentDrivers/CheckoutCom/Utilities.php b/app/PaymentDrivers/CheckoutCom/Utilities.php index cc7427f60267..2ac8e27b8ce4 100644 --- a/app/PaymentDrivers/CheckoutCom/Utilities.php +++ b/app/PaymentDrivers/CheckoutCom/Utilities.php @@ -55,7 +55,7 @@ trait Utilities return round($amount * 100); } - private function processSuccessfulPayment(Payment $_payment) + private function processSuccessfulPayment(Payment $_payment, $return_payment = false) { if ($this->getParent()->payment_hash->data->store_card) { $this->storePaymentMethod($_payment); @@ -78,10 +78,14 @@ trait Utilities $this->getParent()->client ); + if ($return_payment) { + return $payment; + } + return redirect()->route('client.payments.show', ['payment' => $this->getParent()->encodePrimaryKey($payment->id)]); } - public function processUnsuccessfulPayment(Payment $_payment) + public function processUnsuccessfulPayment(Payment $_payment, $throw_exception = true) { PaymentFailureMailer::dispatch( $this->getParent()->client, @@ -103,7 +107,9 @@ trait Utilities $this->getParent()->client ); - throw new PaymentFailed($_payment->status, $_payment->http_code); + if ($throw_exception) { + throw new PaymentFailed($_payment->status, $_payment->http_code); + } } private function processPendingPayment(Payment $_payment) diff --git a/app/PaymentDrivers/CheckoutComPaymentDriver.php b/app/PaymentDrivers/CheckoutComPaymentDriver.php index 1a58002f58b8..0e51959dd395 100644 --- a/app/PaymentDrivers/CheckoutComPaymentDriver.php +++ b/app/PaymentDrivers/CheckoutComPaymentDriver.php @@ -12,18 +12,24 @@ namespace App\PaymentDrivers; +use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\Payments\PaymentWebhookRequest; +use App\Jobs\Mail\PaymentFailureMailer; +use App\Jobs\Util\SystemLogger; use App\Models\ClientGatewayToken; use App\Models\Company; use App\Models\GatewayType; +use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; +use App\Models\PaymentType; use App\Models\SystemLog; use App\PaymentDrivers\CheckoutCom\CreditCard; use App\PaymentDrivers\CheckoutCom\Utilities; use App\Utils\Traits\SystemLogTrait; use Checkout\CheckoutApi; use Checkout\Library\Exceptions\CheckoutHttpException; +use Checkout\Models\Payments\IdSource; use Checkout\Models\Payments\Refund; use Exception; @@ -96,8 +102,8 @@ class CheckoutComPaymentDriver extends BaseDriver public function init() { $config = [ - 'secret' => $this->company_gateway->getConfigField('secretApiKey'), - 'public' => $this->company_gateway->getConfigField('publicApiKey'), + 'secret' => $this->company_gateway->getConfigField('secretApiKey'), + 'public' => $this->company_gateway->getConfigField('publicApiKey'), 'sandbox' => $this->company_gateway->getConfigField('testMode'), ]; @@ -108,7 +114,7 @@ class CheckoutComPaymentDriver extends BaseDriver /** * Process different view depending on payment type - * @param int $gateway_type_id The gateway type + * @param int $gateway_type_id The gateway type * @return string The view string */ public function viewForType($gateway_type_id) @@ -132,7 +138,7 @@ class CheckoutComPaymentDriver extends BaseDriver /** * Payment View * - * @param array $data Payment data array + * @param array $data Payment data array * @return view The payment view */ public function processPaymentView(array $data) @@ -143,7 +149,7 @@ class CheckoutComPaymentDriver extends BaseDriver /** * Process the payment response * - * @param Request $request The payment request + * @param Request $request The payment request * @return view The payment response view */ public function processPaymentResponse($request) @@ -188,7 +194,54 @@ class CheckoutComPaymentDriver extends BaseDriver public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) { - // .. + $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total; + $invoice = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->first(); + + if ($invoice) { + $description = "Invoice {$invoice->number} for {$amount} for client {$this->client->present()->name()}"; + } else { + $description = "Payment with no invoice for amount {$amount} for client {$this->client->present()->name()}"; + } + + $this->init(); + + $method = new IdSource($cgt->token); + + $payment = new \Checkout\Models\Payments\Payment($method, $this->client->getCurrencyCode()); + $payment->amount = $this->convertToCheckoutAmount($amount, $this->client->getCurrencyCode()); + $payment->reference = $cgt->meta->last4 . '-' . now(); + + $request = new PaymentResponseRequest(); + $request->setMethod('POST'); + $request->request->add(['payment_hash' => $payment_hash]); + + try { + $response = $this->gateway->payments()->request($payment); + + if ($response->status == 'Authorized') { + $this->confirmGatewayFee($request); + + return $this->processSuccessfulPayment($response, true); + } + + if ($response->status == 'Declined') { + $this->unWindGatewayFees($payment_hash); + + PaymentFailureMailer::dispatch( + $this->client, $response->response_summary, + $this->client->company, + $this->payment_hash->data->value + ); + + $this->processUnsuccessfulPayment($response, false); + + return false; + } + } catch (\Exception | CheckoutHttpException $e) { + $this->unWindGatewayFees($payment_hash); + + // .. + } } public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null)