1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/PaymentDrivers/EwayPaymentDriver.php

197 lines
7.2 KiB
PHP
Raw Normal View History

2020-11-25 22:07:09 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2020-11-25 22:07:09 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-11-25 22:07:09 +01:00
*/
namespace App\PaymentDrivers;
2021-06-24 11:45:45 +02:00
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\SystemLog;
2021-07-22 03:30:16 +02:00
use App\PaymentDrivers\Eway\CreditCard;
2021-07-27 07:14:29 +02:00
use App\PaymentDrivers\Eway\Token;
2020-11-25 22:07:09 +01:00
use App\Utils\Traits\MakesHash;
2021-07-22 03:30:16 +02:00
class EwayPaymentDriver extends BaseDriver
2020-11-25 22:07:09 +01:00
{
use MakesHash;
public $refundable = true; //does this gateway support refunds?
public $token_billing = true; //does this gateway support token billing?
public $can_authorise_credit_card = true; //does this gateway support authorizations?
2021-07-22 03:30:16 +02:00
public $eway; //initialized gateway
2020-11-25 22:07:09 +01:00
public $payment_method; //initialized payment method
public static $methods = [
GatewayType::CREDIT_CARD => CreditCard::class, //maps GatewayType => Implementation class
];
2021-07-22 08:05:58 +02:00
const SYSTEM_LOG_TYPE = SystemLog::TYPE_EWAY; //define a constant for your gateway ie TYPE_YOUR_CUSTOM_GATEWAY - set the const in the SystemLog model
2020-11-25 22:07:09 +01:00
2021-06-24 07:51:30 +02:00
public function init()
{
2021-07-22 03:30:16 +02:00
$apiKey = $this->company_gateway->getConfigField('apiKey');
2021-07-26 05:33:03 +02:00
$apiPassword = $this->company_gateway->getConfigField('password');
2021-07-22 03:30:16 +02:00
$apiEndpoint = $this->company_gateway->getConfigField('testMode') ? \Eway\Rapid\Client::MODE_SANDBOX : \Eway\Rapid\Client::MODE_PRODUCTION;
$this->eway = \Eway\Rapid::createClient($apiKey, $apiPassword, $apiEndpoint);
return $this;
2021-06-24 07:51:30 +02:00
}
/* Returns an array of gateway types for the payment gateway */
public function gatewayTypes(): array
{
$types = [];
$types[] = GatewayType::CREDIT_CARD;
2021-06-24 07:51:30 +02:00
return $types;
}
/* Sets the payment method initialized */
2020-11-25 22:07:09 +01:00
public function setPaymentMethod($payment_method_id)
{
$class = self::$methods[$payment_method_id];
$this->payment_method = new $class($this);
2020-11-25 22:07:09 +01:00
return $this;
}
public function authorizeView(array $data)
{
2021-07-26 05:33:03 +02:00
return $this->payment_method->authorizeView($data);
2020-11-25 22:07:09 +01:00
}
public function authorizeResponse($request)
{
2021-07-26 05:33:03 +02:00
return $this->payment_method->authorizeResponse($request);
2020-11-25 22:07:09 +01:00
}
public function processPaymentView(array $data)
{
return $this->payment_method->paymentView($data); //this is your custom implementation from here
}
2020-11-27 12:08:42 +01:00
public function processPaymentResponse($request)
2020-11-25 22:07:09 +01:00
{
return $this->payment_method->paymentResponse($request); //this is your custom implementation from here
}
2021-08-18 13:12:13 +02:00
/* We need PCI compliance prior to enabling this */
2020-11-25 22:07:09 +01:00
public function refund(Payment $payment, $amount, $return_client_response = false)
{
2021-07-27 07:41:31 +02:00
$refund = [
'Refund' => [
'TransactionID' => $payment->transaction_reference,
'TotalAmount' => $this->convertAmount($amount),
2021-07-27 07:41:31 +02:00
],
];
2021-08-18 13:12:13 +02:00
$response = $this->init()->eway->refund($refund);
2021-07-27 07:41:31 +02:00
$transaction_reference = '';
$refund_status = true;
$refund_message = '';
2021-07-27 07:41:31 +02:00
if ($response->TransactionStatus) {
$transaction_reference = $response->TransactionID;
} else {
if ($response->getErrors()) {
foreach ($response->getErrors() as $error) {
$refund_status = false;
$refund_message = \Eway\Rapid::getMessage($error);
}
} else {
$refund_status = false;
2021-08-09 07:44:28 +02:00
$refund_message = 'Sorry, your refund failed';
2021-07-27 07:41:31 +02:00
}
}
2021-08-18 13:12:13 +02:00
2021-07-27 07:41:31 +02:00
return [
'transaction_reference' => $response->TransactionID,
'transaction_response' => json_encode($response),
'success' => $refund_status,
'description' => $refund_message,
'code' => '',
];
2020-11-25 22:07:09 +01:00
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
2021-07-27 07:14:29 +02:00
return (new Token($this))->tokenBilling($cgt, $payment_hash);
2020-11-25 22:07:09 +01:00
}
2021-06-24 11:45:45 +02:00
public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null)
{
}
2021-07-27 04:49:13 +02:00
2021-07-27 07:14:29 +02:00
public function convertAmount($amount)
{
$precision = $this->client->currency()->precision;
if ($precision == 0) {
2021-07-27 07:14:29 +02:00
return $amount;
}
2021-07-27 07:14:29 +02:00
if ($precision == 1) {
return $amount * 10;
}
2021-07-27 07:14:29 +02:00
if ($precision == 2) {
return $amount * 100;
}
2021-07-27 07:14:29 +02:00
return $amount;
}
2021-07-27 04:49:13 +02:00
public function getClientRequiredFields(): array
{
$fields = [];
$fields[] = ['name' => 'contact_first_name', 'label' => ctrans('texts.first_name'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'contact_last_name', 'label' => ctrans('texts.last_name'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'contact_email', 'label' => ctrans('texts.email'), 'type' => 'text', 'validation' => 'required,email:rfc'];
$fields[] = ['name' => 'client_country_id', 'label' => ctrans('texts.country'), 'type' => 'text', 'validation' => 'required'];
if ($this->company_gateway->require_client_name) {
$fields[] = ['name' => 'client_name', 'label' => ctrans('texts.client_name'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_client_phone) {
$fields[] = ['name' => 'client_phone', 'label' => ctrans('texts.client_phone'), 'type' => 'tel', 'validation' => 'required'];
}
if ($this->company_gateway->require_billing_address) {
$fields[] = ['name' => 'client_address_line_1', 'label' => ctrans('texts.address1'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_city', 'label' => ctrans('texts.city'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_state', 'label' => ctrans('texts.state'), 'type' => 'text', 'validation' => 'required'];
}
if ($this->company_gateway->require_postal_code) {
2021-07-27 04:49:13 +02:00
$fields[] = ['name' => 'client_postal_code', 'label' => ctrans('texts.postal_code'), 'type' => 'text', 'validation' => 'required'];
}
2021-07-27 04:49:13 +02:00
if ($this->company_gateway->require_shipping_address) {
$fields[] = ['name' => 'client_shipping_address_line_1', 'label' => ctrans('texts.shipping_address1'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_city', 'label' => ctrans('texts.shipping_city'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_state', 'label' => ctrans('texts.shipping_state'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_postal_code', 'label' => ctrans('texts.shipping_postal_code'), 'type' => 'text', 'validation' => 'required'];
$fields[] = ['name' => 'client_shipping_country_id', 'label' => ctrans('texts.shipping_country'), 'type' => 'text', 'validation' => 'required'];
}
return $fields;
}
2020-11-25 22:07:09 +01:00
}