1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 15:42:51 +01:00
invoiceninja/app/PaymentDrivers/CheckoutCom/Utilities.php

155 lines
5.4 KiB
PHP
Raw Normal View History

2020-06-10 17:38:10 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
2020-06-10 17:38:10 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2020-06-10 17:38:10 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-06-10 17:38:10 +02:00
*/
namespace App\PaymentDrivers\CheckoutCom;
2023-07-21 13:01:22 +02:00
use App\Exceptions\PaymentFailed;
2023-10-26 04:57:44 +02:00
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;
use App\Models\SystemLog;
use Exception;
use stdClass;
2020-10-22 15:24:18 +02:00
2020-06-10 17:38:10 +02:00
trait Utilities
{
public function getPublishableKey()
{
2020-06-15 12:59:02 +02:00
return $this->company_gateway->getConfigField('publicApiKey');
2020-06-10 17:38:10 +02:00
}
2020-06-11 15:13:35 +02:00
2020-12-07 14:50:30 +01:00
public function getParent()
{
return static::class == \App\PaymentDrivers\CheckoutComPaymentDriver::class ? $this : $this->checkout;
2020-12-07 14:50:30 +01:00
}
2020-06-11 15:13:35 +02:00
public function convertToCheckoutAmount($amount, $currency)
{
$cases = [
'option_1' => ['BIF', 'DJF', 'GNF', 'ISK', 'KMF', 'XAF', 'CLF', 'XPF', 'JPY', 'PYG', 'RWF', 'KRW', 'VUV', 'VND', 'XOF'],
'option_2' => ['BHD', 'IQD', 'JOD', 'KWD', 'LYD', 'OMR', 'TND'],
];
// https://docs.checkout.com/resources/calculating-the-value#Calculatingthevalue-Option1:Thefullvaluefullvalue
if (in_array($currency, $cases['option_1'])) {
return round($amount);
}
// https://docs.checkout.com/resources/calculating-the-value#Calculatingthevalue-Option2:Thevaluedividedby1000valuediv1000
if (in_array($currency, $cases['option_2'])) {
return round($amount * 1000);
}
// https://docs.checkout.com/resources/calculating-the-value#Calculatingthevalue-Option3:Thevaluedividedby100valuediv100
return round($amount * 100);
}
2020-10-22 15:24:18 +02:00
2022-06-16 02:01:24 +02:00
private function processSuccessfulPayment($_payment)
2020-10-22 15:24:18 +02:00
{
2020-12-07 14:50:30 +01:00
if ($this->getParent()->payment_hash->data->store_card) {
2021-04-22 15:32:34 +02:00
$this->storeLocalPaymentMethod($_payment);
2020-10-22 15:24:18 +02:00
}
$data = [
2022-06-16 02:01:24 +02:00
'payment_method' => $_payment['source']['id'],
2023-07-21 13:01:22 +02:00
'payment_type' => \App\Models\PaymentType::CREDIT_CARD_OTHER,
2020-12-07 14:50:30 +01:00
'amount' => $this->getParent()->payment_hash->data->raw_value,
2022-06-16 02:01:24 +02:00
'transaction_reference' => $_payment['id'],
2021-02-03 12:36:10 +01:00
'gateway_type_id' => GatewayType::CREDIT_CARD,
2020-10-22 15:24:18 +02:00
];
2020-12-07 14:50:30 +01:00
$payment = $this->getParent()->createPayment($data, \App\Models\Payment::STATUS_COMPLETED);
2020-10-22 15:24:18 +02:00
SystemLogger::dispatch(
['response' => $_payment, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_CHECKOUT,
$this->getParent()->client,
$this->getParent()->client->company
2020-10-22 15:24:18 +02:00
);
2020-12-07 14:50:30 +01:00
return redirect()->route('client.payments.show', ['payment' => $this->getParent()->encodePrimaryKey($payment->id)]);
2020-10-22 15:24:18 +02:00
}
2022-06-16 02:01:24 +02:00
public function processUnsuccessfulPayment($_payment, $throw_exception = true)
2020-10-22 15:24:18 +02:00
{
2021-12-21 03:45:00 +01:00
$error_message = '';
nlog("checkout failure");
nlog($_payment);
2023-01-15 21:54:49 +01:00
if (is_array($_payment) && array_key_exists('status', $_payment)) {
2022-06-16 02:01:24 +02:00
$error_message = $_payment['status'];
2023-02-16 02:36:09 +01:00
} else {
2022-11-11 23:00:54 +01:00
$error_message = 'Error processing payment.';
}
2021-12-21 03:45:00 +01:00
2023-09-12 02:27:30 +02:00
if(isset($_payment['actions'][0]['response_summary']) ?? false) {
$error_message = $_payment['actions'][0]['response_summary'];
}
2023-09-24 21:54:39 +02:00
//checkout does not return a integer status code as an alias for a http status code.
$error_code = 400;
2023-09-12 02:27:30 +02:00
2021-12-21 03:45:00 +01:00
$this->getParent()->sendFailureMail($error_message);
2020-10-22 15:24:18 +02:00
$message = [
2022-11-11 23:00:54 +01:00
'server_response' => $_payment ?: 'Server did not return any response. Most likely failed before payment was created.',
2020-12-07 14:50:30 +01:00
'data' => $this->getParent()->payment_hash->data,
2020-10-22 15:24:18 +02:00
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_CHECKOUT,
$this->getParent()->client,
$this->getParent()->client->company,
2020-10-22 15:24:18 +02:00
);
2021-01-27 15:58:19 +01:00
if ($throw_exception) {
2023-09-12 02:27:30 +02:00
throw new PaymentFailed($error_message, $error_code);
2021-01-27 15:58:19 +01:00
}
2020-10-22 15:24:18 +02:00
}
2022-06-16 02:01:24 +02:00
private function processPendingPayment($_payment)
2020-10-22 15:24:18 +02:00
{
try {
2022-06-16 03:21:10 +02:00
return redirect($_payment['_links']['redirect']['href']);
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2022-06-16 03:21:10 +02:00
return $this->getParent()->processInternallyFailedPayment($this->getParent(), $e);
2020-10-22 15:24:18 +02:00
}
}
2022-06-16 02:01:24 +02:00
private function storeLocalPaymentMethod($response)
2020-10-26 14:40:50 +01:00
{
try {
2020-10-28 11:10:49 +01:00
$payment_meta = new stdClass;
2022-06-16 03:21:10 +02:00
$payment_meta->exp_month = (string) $response['source']['expiry_month'];
$payment_meta->exp_year = (string) $response['source']['expiry_year'];
$payment_meta->brand = (string) $response['source']['scheme'];
$payment_meta->last4 = (string) $response['source']['last4'];
2020-10-26 14:40:50 +01:00
$payment_meta->type = (int) GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
2022-06-16 03:21:10 +02:00
'token' => $response['source']['id'],
2020-12-07 14:50:30 +01:00
'payment_method_id' => $this->getParent()->payment_hash->data->payment_method_id,
2020-10-26 14:40:50 +01:00
];
2022-06-16 02:01:24 +02:00
return $this->getParent()->storePaymentMethod($data, ['gateway_customer_reference' => $response['customer']['id']]);
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2020-10-26 14:40:50 +01:00
session()->flash('message', ctrans('texts.payment_method_saving_failed'));
}
}
2020-06-10 17:38:10 +02:00
}