2021-07-22 03:30:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-07-22 03:30:16 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Eway;
|
|
|
|
|
|
|
|
use App\Exceptions\PaymentFailed;
|
|
|
|
use App\Jobs\Util\SystemLogger;
|
|
|
|
use App\Models\GatewayType;
|
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\Models\SystemLog;
|
2024-08-09 01:07:35 +02:00
|
|
|
use App\PaymentDrivers\Common\LivewireMethodInterface;
|
2022-06-21 11:57:17 +02:00
|
|
|
use App\PaymentDrivers\EwayPaymentDriver;
|
2021-08-18 05:27:44 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2021-07-22 03:30:16 +02:00
|
|
|
|
2024-08-09 01:07:35 +02:00
|
|
|
class CreditCard implements LivewireMethodInterface
|
2021-07-22 03:30:16 +02:00
|
|
|
{
|
2021-08-18 05:27:44 +02:00
|
|
|
use MakesHash;
|
2021-07-22 03:30:16 +02:00
|
|
|
|
2021-07-22 08:05:58 +02:00
|
|
|
public $eway_driver;
|
2021-07-22 03:30:16 +02:00
|
|
|
|
2021-07-22 08:05:58 +02:00
|
|
|
public function __construct(EwayPaymentDriver $eway_driver)
|
2021-07-22 03:30:16 +02:00
|
|
|
{
|
2021-07-22 08:05:58 +02:00
|
|
|
$this->eway_driver = $eway_driver;
|
2021-07-22 03:30:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeView($data)
|
|
|
|
{
|
2021-07-22 08:05:58 +02:00
|
|
|
$data['gateway'] = $this->eway_driver;
|
|
|
|
$data['api_key'] = $this->eway_driver->company_gateway->getConfigField('apiKey');
|
2021-07-26 05:33:03 +02:00
|
|
|
$data['public_api_key'] = $this->eway_driver->company_gateway->getConfigField('publicApiKey');
|
2021-07-22 08:05:58 +02:00
|
|
|
|
|
|
|
return render('gateways.eway.authorize', $data);
|
2021-07-22 03:30:16 +02:00
|
|
|
}
|
|
|
|
|
2021-07-26 05:33:03 +02:00
|
|
|
public function authorizeResponse($request)
|
2021-07-22 03:30:16 +02:00
|
|
|
{
|
2021-08-18 10:24:49 +02:00
|
|
|
$token = $this->createEwayToken($request->input('securefieldcode'));
|
|
|
|
|
|
|
|
return redirect()->route('client.payment_methods.index');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createEwayToken($securefieldcode)
|
|
|
|
{
|
2021-07-26 05:53:02 +02:00
|
|
|
$transaction = [
|
|
|
|
'Reference' => $this->eway_driver->client->number,
|
|
|
|
'Title' => '',
|
2021-11-12 01:46:43 +01:00
|
|
|
'FirstName' => $this->eway_driver->client->contacts()->first()->present()->first_name(),
|
|
|
|
'LastName' => $this->eway_driver->client->contacts()->first()->present()->last_name(),
|
2021-07-26 05:53:02 +02:00
|
|
|
'CompanyName' => $this->eway_driver->client->name,
|
|
|
|
'Street1' => $this->eway_driver->client->address1,
|
|
|
|
'Street2' => $this->eway_driver->client->address2,
|
|
|
|
'City' => $this->eway_driver->client->city,
|
|
|
|
'State' => $this->eway_driver->client->state,
|
|
|
|
'PostalCode' => $this->eway_driver->client->postal_code,
|
|
|
|
'Country' => $this->eway_driver->client->country->iso_3166_2,
|
2024-03-25 02:30:08 +01:00
|
|
|
'Phone' => $this->eway_driver->client->phone ?? '',
|
|
|
|
'Email' => $this->eway_driver->client->contacts()->first()->email ?? '',
|
|
|
|
'Url' => $this->eway_driver->client->website ?? '',
|
2021-07-22 08:05:58 +02:00
|
|
|
'Method' => \Eway\Rapid\Enum\PaymentMethod::CREATE_TOKEN_CUSTOMER,
|
2021-08-18 10:24:49 +02:00
|
|
|
'SecuredCardData' => $securefieldcode,
|
2021-07-22 08:05:58 +02:00
|
|
|
];
|
|
|
|
|
2021-07-26 05:33:03 +02:00
|
|
|
$response = $this->eway_driver->init()->eway->createCustomer(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction);
|
2021-07-22 08:05:58 +02:00
|
|
|
|
2023-07-05 08:07:36 +02:00
|
|
|
if($response->getErrors()) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-05 08:07:36 +02:00
|
|
|
$response_status['message'] = \Eway\Rapid::getMessage($response->getErrors()[0]);
|
2023-06-28 01:57:03 +02:00
|
|
|
|
2021-10-17 13:21:56 +02:00
|
|
|
$this->eway_driver->sendFailureMail($response_status['message']);
|
|
|
|
|
2023-06-28 01:57:03 +02:00
|
|
|
$this->logResponse($response);
|
|
|
|
|
2024-06-28 07:16:08 +02:00
|
|
|
throw new PaymentFailed($response_status['message'] ?? 'Unknown response from gateway, please contact you merchant.', 400); //@phpstan-ignore-line
|
2021-10-17 13:21:56 +02:00
|
|
|
}
|
2021-07-26 05:53:02 +02:00
|
|
|
|
|
|
|
//success
|
|
|
|
$cgt = [];
|
2022-12-21 05:56:42 +01:00
|
|
|
$cgt['token'] = strval($response->Customer->TokenCustomerID);
|
2021-07-26 05:53:02 +02:00
|
|
|
$cgt['payment_method_id'] = GatewayType::CREDIT_CARD;
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$payment_meta = new \stdClass();
|
2021-07-27 04:49:13 +02:00
|
|
|
$payment_meta->exp_month = $response->Customer->CardDetails->ExpiryMonth;
|
|
|
|
$payment_meta->exp_year = $response->Customer->CardDetails->ExpiryYear;
|
2021-07-26 05:53:02 +02:00
|
|
|
$payment_meta->brand = 'CC';
|
2022-06-21 11:57:17 +02:00
|
|
|
$payment_meta->last4 = substr($response->Customer->CardDetails->Number, -4);
|
2021-07-26 05:53:02 +02:00
|
|
|
$payment_meta->type = GatewayType::CREDIT_CARD;
|
|
|
|
|
|
|
|
$cgt['payment_meta'] = $payment_meta;
|
|
|
|
|
|
|
|
$token = $this->eway_driver->storeGatewayToken($cgt, []);
|
|
|
|
|
2023-06-28 01:57:03 +02:00
|
|
|
$this->logResponse($response);
|
|
|
|
|
2021-08-18 10:24:49 +02:00
|
|
|
return $token;
|
2021-07-22 03:30:16 +02:00
|
|
|
}
|
|
|
|
|
2024-08-09 01:07:35 +02:00
|
|
|
public function paymentData(array $data): array
|
2021-07-22 03:30:16 +02:00
|
|
|
{
|
2021-07-27 06:03:04 +02:00
|
|
|
$data['gateway'] = $this->eway_driver;
|
|
|
|
$data['public_api_key'] = $this->eway_driver->company_gateway->getConfigField('publicApiKey');
|
|
|
|
|
2024-08-09 01:07:35 +02:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentView($data)
|
|
|
|
{
|
|
|
|
$data = $this->paymentData($data);
|
|
|
|
|
2021-07-27 06:03:04 +02:00
|
|
|
return render('gateways.eway.pay', $data);
|
2021-07-22 03:30:16 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 13:02:12 +02:00
|
|
|
public function paymentResponse($request)
|
2021-07-22 03:30:16 +02:00
|
|
|
{
|
2021-08-18 05:03:38 +02:00
|
|
|
$state = [
|
|
|
|
'server_response' => $request->all(),
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->eway_driver->payment_hash->data = array_merge((array) $this->eway_driver->payment_hash->data, $state);
|
|
|
|
$this->eway_driver->payment_hash->save();
|
2021-08-16 11:02:21 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (boolval($request->input('store_card'))) {
|
2021-08-18 10:24:49 +02:00
|
|
|
$token = $this->createEwayToken($request->input('securefieldcode'));
|
2021-08-18 13:12:13 +02:00
|
|
|
$payment = $this->tokenBilling($token->token, $this->eway_driver->payment_hash);
|
2021-08-18 10:24:49 +02:00
|
|
|
|
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($request->token) {
|
2021-08-18 10:24:49 +02:00
|
|
|
$payment = $this->tokenBilling($request->token, $this->eway_driver->payment_hash);
|
|
|
|
|
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
|
|
|
|
}
|
|
|
|
|
2022-03-08 09:43:48 +01:00
|
|
|
$invoice_numbers = '';
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($this->eway_driver->payment_hash->data) {
|
2024-06-28 07:16:08 +02:00
|
|
|
$invoice_numbers = collect($this->eway_driver->payment_hash->data->invoices)->pluck('invoice_number')->implode(','); //@phpstan-ignore-line
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
|
|
|
|
2022-03-08 09:43:48 +01:00
|
|
|
$amount = array_sum(array_column($this->eway_driver->payment_hash->invoices(), 'amount')) + $this->eway_driver->payment_hash->fee_total;
|
|
|
|
|
2023-06-28 01:57:03 +02:00
|
|
|
// $description = "Invoices: {$invoice_numbers} for {$amount} for client {$this->eway_driver->client->present()->name()}";
|
2022-03-08 09:43:48 +01:00
|
|
|
|
2021-08-16 11:02:21 +02:00
|
|
|
$transaction = [
|
|
|
|
'Payment' => [
|
2021-08-18 05:03:38 +02:00
|
|
|
'TotalAmount' => $this->convertAmountForEway(),
|
2022-03-08 09:43:48 +01:00
|
|
|
'CurrencyCode' => $this->eway_driver->client->currency()->code,
|
|
|
|
'InvoiceNumber' => $invoice_numbers,
|
2022-06-21 11:57:17 +02:00
|
|
|
'InvoiceDescription' => substr($invoice_numbers, 0, 63),
|
2021-08-16 11:02:21 +02:00
|
|
|
],
|
|
|
|
'TransactionType' => \Eway\Rapid\Enum\TransactionType::PURCHASE,
|
2021-08-18 05:03:38 +02:00
|
|
|
'SecuredCardData' => $request->input('securefieldcode'),
|
2021-08-16 11:02:21 +02:00
|
|
|
];
|
|
|
|
|
2021-08-18 05:03:38 +02:00
|
|
|
$response = $this->eway_driver->init()->eway->createTransaction(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction);
|
|
|
|
|
2022-03-25 04:55:39 +01:00
|
|
|
$this->logResponse($response);
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($response->TransactionStatus) {
|
|
|
|
$payment = $this->storePayment($response);
|
|
|
|
} else {
|
|
|
|
$message = 'Error processing payment.';
|
2022-04-19 06:53:19 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (isset($response->ResponseMessage)) {
|
|
|
|
$message .= " Gateway Error Code = {$response->ResponseMessage}";
|
|
|
|
}
|
2022-04-19 06:53:19 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($response->getErrors()) {
|
|
|
|
foreach ($response->getErrors() as $error) {
|
|
|
|
$message = \Eway\Rapid::getMessage($error);
|
2022-04-19 06:53:19 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-18 05:27:44 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->eway_driver->sendFailureMail($message);
|
2021-08-18 05:03:38 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
throw new PaymentFailed($message, 400);
|
|
|
|
}
|
2021-08-18 05:03:38 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
|
2021-08-16 11:02:21 +02:00
|
|
|
}
|
|
|
|
|
2021-08-18 05:27:44 +02:00
|
|
|
private function storePayment($response)
|
|
|
|
{
|
|
|
|
$amount = array_sum(array_column($this->eway_driver->payment_hash->invoices(), 'amount')) + $this->eway_driver->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->TransactionID;
|
|
|
|
|
|
|
|
$payment = $this->eway_driver->createPayment($payment_record);
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
2021-08-16 11:02:21 +02:00
|
|
|
|
2021-08-18 10:24:49 +02:00
|
|
|
private function convertAmountForEway($amount = false)
|
2021-08-16 11:02:21 +02:00
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $amount) {
|
2021-08-18 10:24:49 +02:00
|
|
|
$amount = array_sum(array_column($this->eway_driver->payment_hash->invoices(), 'amount')) + $this->eway_driver->payment_hash->fee_total;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-08-18 05:03:38 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (in_array($this->eway_driver->client->currency()->code, ['VND', 'JPY', 'KRW', 'GNF', 'IDR', 'PYG', 'RWF', 'UGX', 'VUV', 'XAF', 'XPF'])) {
|
2021-08-18 05:03:38 +02:00
|
|
|
return $amount;
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2021-08-18 05:03:38 +02:00
|
|
|
|
|
|
|
return $amount * 100;
|
2021-07-22 03:30:16 +02:00
|
|
|
}
|
2021-08-18 05:03:38 +02:00
|
|
|
|
2021-08-18 05:27:44 +02:00
|
|
|
private function logResponse($response, $success = true)
|
|
|
|
{
|
|
|
|
$logger_message = [
|
|
|
|
'server_response' => $response,
|
|
|
|
];
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$logger_message,
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
$success ? SystemLog::EVENT_GATEWAY_SUCCESS : SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_EWAY,
|
|
|
|
$this->eway_driver->client,
|
|
|
|
$this->eway_driver->client->company,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-08-18 10:24:49 +02:00
|
|
|
public function tokenBilling($token, $payment_hash)
|
|
|
|
{
|
|
|
|
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
|
|
|
|
|
2022-03-17 09:37:40 +01:00
|
|
|
$invoice_numbers = '';
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
if ($this->eway_driver->payment_hash->data) {
|
|
|
|
$invoice_numbers = collect($this->eway_driver->payment_hash->data->invoices)->pluck('invoice_number')->implode(',');
|
|
|
|
}
|
|
|
|
|
2022-03-08 09:43:48 +01:00
|
|
|
$description = "Invoices: {$invoice_numbers} for {$amount} for client {$this->eway_driver->client->present()->name()}";
|
|
|
|
|
2021-08-18 10:24:49 +02:00
|
|
|
$transaction = [
|
|
|
|
'Customer' => [
|
|
|
|
'TokenCustomerID' => $token,
|
|
|
|
],
|
|
|
|
'Payment' => [
|
|
|
|
'TotalAmount' => $this->convertAmountForEway($amount),
|
2022-03-08 09:43:48 +01:00
|
|
|
'CurrencyCode' => $this->eway_driver->client->currency()->code,
|
|
|
|
'InvoiceNumber' => $invoice_numbers,
|
2022-06-21 11:57:17 +02:00
|
|
|
'InvoiceDescription' => substr($invoice_numbers, 0, 63),
|
2021-08-18 10:24:49 +02:00
|
|
|
],
|
|
|
|
'TransactionType' => \Eway\Rapid\Enum\TransactionType::RECURRING,
|
|
|
|
];
|
|
|
|
|
|
|
|
$response = $this->eway_driver->init()->eway->createTransaction(\Eway\Rapid\Enum\ApiMethod::DIRECT, $transaction);
|
|
|
|
|
2023-10-15 08:27:00 +02:00
|
|
|
if ($response->TransactionStatus ?? false) {
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->logResponse($response, true);
|
|
|
|
$payment = $this->storePayment($response);
|
|
|
|
} else {
|
|
|
|
$message = 'Error processing payment.';
|
2021-08-18 10:24:49 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (isset($response->ResponseMessage)) {
|
|
|
|
$message .= " Gateway Error Code = {$response->ResponseMessage}";
|
|
|
|
}
|
2021-10-17 13:21:56 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($response->getErrors()) {
|
|
|
|
foreach ($response->getErrors() as $error) {
|
|
|
|
$message = \Eway\Rapid::getMessage($error);
|
2022-04-19 06:53:19 +02:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
2022-04-19 06:53:19 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->logResponse($response, false);
|
2021-08-18 10:24:49 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
$this->eway_driver->sendFailureMail($message);
|
2021-08-18 10:24:49 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
throw new PaymentFailed($message, 400);
|
|
|
|
}
|
2021-08-18 10:24:49 +02:00
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
2024-08-09 01:07:35 +02:00
|
|
|
public function livewirePaymentView(array $data): string
|
|
|
|
{
|
|
|
|
return 'gateways.eway.pay_livewire';
|
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|