1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 06:32:40 +01:00
invoiceninja/app/PaymentDrivers/Eway/CreditCard.php

276 lines
8.8 KiB
PHP
Raw Normal View History

2021-07-22 03:30:16 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Eway;
use App\Exceptions\PaymentFailed;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\EwayPaymentDriver;
2021-07-27 04:49:13 +02:00
use App\PaymentDrivers\Eway\ErrorCode;
2021-08-18 05:27:44 +02:00
use App\Utils\Traits\MakesHash;
2021-07-22 03:30:16 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
class CreditCard
{
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:53:02 +02:00
/*
Eway\Rapid\Model\Response\CreateCustomerResponse {#2374 ▼
#fillable: array:16 [▶]
#errors: []
#attributes: array:11 [▼
"AuthorisationCode" => null
"ResponseCode" => "00"
"ResponseMessage" => "A2000"
"TransactionID" => null
"TransactionStatus" => false
"TransactionType" => "MOTO"
"BeagleScore" => null
"Verification" => Eway\Rapid\Model\Verification {#2553 ▼
#fillable: array:5 [▶]
#attributes: array:5 [▶]
}
"Customer" => Eway\Rapid\Model\Customer {#2504 ▼
#fillable: array:38 [▶]
#attributes: array:20 [▼
"CardDetails" => Eway\Rapid\Model\CardDetails {#2455 ▼
#fillable: array:8 [▶]
#attributes: array:7 [▼
"Number" => "411111XXXXXX1111"
"Name" => "Joey Diaz"
"ExpiryMonth" => "10"
"ExpiryYear" => "23"
"StartMonth" => null
"StartYear" => null
"IssueNumber" => null
]
}
"TokenCustomerID" => 917047257342
"Reference" => "A12345"
"Title" => "Mr."
"FirstName" => "John"
"LastName" => "Smith"
"CompanyName" => "Demo Shop 123"
"JobDescription" => "PHP Developer"
"Street1" => "Level 5"
"Street2" => "369 Queen Street"
"City" => "Sydney"
"State" => "NSW"
"PostalCode" => "2000"
"Country" => "au"
"Email" => "demo@example.org"
"Phone" => "09 889 0986"
"Mobile" => "09 889 6542"
"Comments" => ""
"Fax" => ""
"Url" => "http://www.ewaypayments.com"
]
}
"Payment" => Eway\Rapid\Model\Payment {#2564 ▼
#fillable: array:5 [▶]
#attributes: array:5 [▼
"TotalAmount" => 0
"InvoiceNumber" => ""
"InvoiceDescription" => ""
"InvoiceReference" => ""
"CurrencyCode" => "AUD"
]
}
"Errors" => null
]
}
*/
2021-07-26 05:33:03 +02:00
public function authorizeResponse($request)
2021-07-22 03:30:16 +02:00
{
2021-07-26 05:53:02 +02:00
$transaction = [
'Reference' => $this->eway_driver->client->number,
'Title' => '',
2021-07-27 04:49:13 +02:00
'FirstName' => $this->eway_driver->client->contacts()->first()->present()->last_name(),
'LastName' => $this->eway_driver->client->contacts()->first()->present()->first_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,
'Phone' => $this->eway_driver->client->phone,
2021-07-27 04:49:13 +02:00
'Email' => $this->eway_driver->client->contacts()->first()->email,
2021-07-26 05:53:02 +02:00
"Url" => $this->eway_driver->client->website,
2021-07-27 04:49:13 +02:00
// 'Payment' => [
// 'TotalAmount' => 0,
// ],
// 'TransactionType' => \Eway\Rapid\Enum\TransactionType::PURCHASE,
2021-07-22 08:05:58 +02:00
'Method' => \Eway\Rapid\Enum\PaymentMethod::CREATE_TOKEN_CUSTOMER,
2021-08-12 13:02:12 +02:00
'SecuredCardData' => $request->input('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
2021-07-27 04:49:13 +02:00
$response_status = ErrorCode::getStatus($response->ResponseMessage);
if(!$response_status['success'])
throw new PaymentFailed($response_status['message'], 400);
2021-07-26 05:53:02 +02:00
//success
$cgt = [];
2021-07-27 04:49:13 +02:00
$cgt['token'] = $response->Customer->TokenCustomerID;
2021-07-26 05:53:02 +02:00
$cgt['payment_method_id'] = GatewayType::CREDIT_CARD;
$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';
2021-07-27 06:03:04 +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, []);
return redirect()->route('client.payment_methods.index');
2021-07-22 03:30:16 +02:00
}
public function paymentView($data)
{
2021-07-27 06:03:04 +02:00
$data['gateway'] = $this->eway_driver;
$data['public_api_key'] = $this->eway_driver->company_gateway->getConfigField('publicApiKey');
return render('gateways.eway.pay', $data);
2021-07-22 03:30:16 +02:00
}
2021-08-16 11:02:21 +02:00
/*
array:9 [
"_token" => "RpkUNg0gHYfzLKPCJG3EtmshGiwDUeytRG53b1Or"
"gateway_response" => null
"store_card" => true
"payment_hash" => "1wC7J7Jo1jagV5oBaWSxz7b2lSLsvVMp"
"company_gateway_id" => "6"
"payment_method_id" => "1"
"token" => null
"securefieldcode" => "F9802rbHYa0St-w3QpBXvNaiNFMNhmY7OmZimH-HROUzS1K0niXOlqXUzugz4mnTqJVqK"
"q" => "/client/payments/process/response"
]
*/
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
$transaction = [
'Payment' => [
2021-08-18 05:03:38 +02:00
'TotalAmount' => $this->convertAmountForEway(),
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);
$response_status = ErrorCode::getStatus($response->ResponseMessage);
2021-08-18 05:27:44 +02:00
if(!$response_status['success']){
$this->logResponse($response, false);
throw new PaymentFailed($response_status['message'], 400);
}
$this->logResponse($response, true);
$payment = $this->storePayment($response);
2021-08-18 05:03:38 +02:00
2021-08-18 05:27:44 +02:00
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
2021-08-18 05:03:38 +02:00
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 05:03:38 +02:00
private function convertAmountForEway()
2021-08-16 11:02:21 +02:00
{
2021-08-18 05:03:38 +02:00
2021-08-18 05:27:44 +02:00
$amount = array_sum(array_column($this->eway_driver->payment_hash->invoices(), 'amount')) + $this->eway_driver->payment_hash->fee_total;
2021-08-18 05:03:38 +02:00
if(in_array($this->eway_driver->client->currency()->code, ['VND', 'JPY', 'KRW', 'GNF', 'IDR', 'PYG', 'RWF', 'UGX', 'VUV', 'XAF', 'XPF']))
return $amount;
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-07-22 03:30:16 +02:00
}