diff --git a/app/PaymentDrivers/Eway/Token.php b/app/PaymentDrivers/Eway/Token.php new file mode 100644 index 000000000000..1bdbc2ed18a0 --- /dev/null +++ b/app/PaymentDrivers/Eway/Token.php @@ -0,0 +1,111 @@ +eway_driver = $eway_driver; + } + + public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) + { + + $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total; + + $transaction = [ + 'Customer' => [ + 'TokenCustomerID' => $cgt->token, + ], + 'Payment' => [ + 'TotalAmount' => $this->eway_driver->convertAmount($amount), + ], + 'TransactionType' => \Eway\Rapid\Enum\TransactionType::RECURRING, + ]; + + $response = $this->eway_driver->init()->eway->createTransaction(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction); + + $response_status = ErrorCode::getStatus($response->ResponseMessage); + + if(!$response_status['success']) + return $this->processUnsuccessfulPayment($response); + + $payment = $this->processSuccessfulPayment($response); + + return $payment; + + } + + + private function processSuccessfulPayment($response) + { + $amount = array_sum(array_column($this->eway_driver->payment_hash->invoices(), 'amount')) + $this->eway_driver->payment_hash->fee_total; + + $data = [ + 'gateway_type_id' => $cgt->gateway_type_id, + 'payment_type' => GatewayType::CREDIT_CARD_OTHER, + 'transaction_reference' => $response->Customer->Reference, + 'amount' => $amount, + ]; + + $payment = $this->eway_driver->createPayment($data); + $payment->meta = $cgt->meta; + $payment->save(); + + $payment_hash->payment_id = $payment->id; + $payment_hash->save(); + + return $payment; + + } + + private function processUnsuccessfulPayment($response) + { + + $response_status = ErrorCode::getStatus($response->ResponseMessage); + + $error = $response_status['message'] + $error_code = $response->ResponseMessage; + + $data = [ + 'response' => $response, + 'error' => $error, + 'error_code' => $error_code, + ]; + + return $this->driver_class->processUnsuccessfulTransaction($data); + + } + +} \ No newline at end of file diff --git a/app/PaymentDrivers/EwayPaymentDriver.php b/app/PaymentDrivers/EwayPaymentDriver.php index 260723172bff..5a6154fda388 100644 --- a/app/PaymentDrivers/EwayPaymentDriver.php +++ b/app/PaymentDrivers/EwayPaymentDriver.php @@ -18,6 +18,7 @@ use App\Models\Payment; use App\Models\PaymentHash; use App\Models\SystemLog; use App\PaymentDrivers\Eway\CreditCard; +use App\PaymentDrivers\Eway\Token; use App\Utils\Traits\MakesHash; class EwayPaymentDriver extends BaseDriver @@ -95,13 +96,30 @@ class EwayPaymentDriver extends BaseDriver public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) { - return $this->payment_method->yourTokenBillingImplmentation(); //this is your custom implementation from here + return (new Token($this))->tokenBilling($cgt, $payment_hash); } public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null) { } + public function convertAmount($amount) + { + $precision = $this->client->currency()->precision; + + if($precision == 0) + return $amount; + + if($precision == 1) + return $amount*10; + + if$precision == 2) + return $amount*100; + + + return $amount; + } + public function getClientRequiredFields(): array { $fields = []; diff --git a/app/PaymentDrivers/Sample/CreditCard.php b/app/PaymentDrivers/Sample/CreditCard.php index 12e4d872bfcd..44c404ae2611 100644 --- a/app/PaymentDrivers/Sample/CreditCard.php +++ b/app/PaymentDrivers/Sample/CreditCard.php @@ -21,12 +21,14 @@ use App\Models\Payment; use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\SystemLog; +use App\Utils\Traits\MakesHash; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; class CreditCard { + use MakesHash; public $driver_class; @@ -55,6 +57,43 @@ class CreditCard } + /* This method is stubbed ready to go - you just need to harvest the equivalent 'transaction_reference' */ + private function processSuccessfulPayment($response) + { + $amount = array_sum(array_column($this->driver_class->payment_hash->invoices(), 'amount')) + $this->driver_class->payment_hash->fee_total; + + $payment_record = []; + $payment_record['amount'] = $amount; + $payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER; + $payment_record['gateway_type_id'] = GatewayType::CREDIT_CARD; + // $payment_record['transaction_reference'] = $response->transaction_id; + + $payment = $this->driver_class->createPayment($payment_record, Payment::STATUS_COMPLETED); + + return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); + + } + + private function processUnsuccessfulPayment($response) + { + /*Harvest your own errors here*/ + // $error = $response->status_message; + + // if(property_exists($response, 'approval_message') && $response->approval_message) + // $error .= " - {$response->approval_message}"; + + // $error_code = property_exists($response, 'approval_message') ? $response->approval_message : 'Undefined code'; + + $data = [ + 'response' => $response, + 'error' => $error, + 'error_code' => $error_code, + ]; + + return $this->driver_class->processUnsuccessfulTransaction($data); + + } + /* Helpers */ @@ -69,4 +108,6 @@ class CreditCard */ + + } \ No newline at end of file