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

143 lines
4.8 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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2020-06-10 17:38:10 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers\CheckoutCom;
use App\Exceptions\PaymentFailed;
2020-10-22 15:24:18 +02:00
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
2020-10-26 14:40:50 +01:00
use App\Models\GatewayType;
2020-10-22 15:24:18 +02:00
use App\Models\PaymentType;
use App\Models\SystemLog;
2020-10-28 11:10:49 +01:00
use Checkout\Models\Payments\Payment;
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' ? $this : $this->checkout;
}
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
2021-01-28 16:13:32 +01:00
private function processSuccessfulPayment(Payment $_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) {
2020-10-26 14:40:50 +01:00
$this->storePaymentMethod($_payment);
2020-10-22 15:24:18 +02:00
}
$data = [
'payment_method' => $_payment->source['id'],
'payment_type' => PaymentType::parseCardType(strtolower($_payment->source['scheme'])),
2020-12-07 14:50:30 +01:00
'amount' => $this->getParent()->payment_hash->data->raw_value,
2020-12-03 13:38:57 +01: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,
2020-12-07 14:50:30 +01:00
$this->getParent()->client
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
}
2021-01-27 15:58:19 +01:00
public function processUnsuccessfulPayment(Payment $_payment, $throw_exception = true)
2020-10-22 15:24:18 +02:00
{
PaymentFailureMailer::dispatch(
2020-12-07 14:50:30 +01:00
$this->getParent()->client,
2020-10-22 15:24:18 +02:00
$_payment,
2020-12-07 14:50:30 +01:00
$this->getParent()->client->company,
$this->getParent()->payment_hash->data->value
2020-10-22 15:24:18 +02:00
);
$message = [
'server_response' => $_payment,
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,
2020-12-07 14:50:30 +01:00
$this->getParent()->client
2020-10-22 15:24:18 +02:00
);
2021-01-27 15:58:19 +01:00
if ($throw_exception) {
throw new PaymentFailed($_payment->status, $_payment->http_code);
}
2020-10-22 15:24:18 +02:00
}
2020-10-28 11:10:49 +01:00
private function processPendingPayment(Payment $_payment)
2020-10-22 15:24:18 +02:00
{
try {
return redirect($_payment->_links['redirect']['href']);
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
2020-12-07 14:50:30 +01:00
return $this->processInternallyFailedPayment($this->getParent(), $e);
2020-10-22 15:24:18 +02:00
}
}
2020-10-28 11:10:49 +01:00
private function storePaymentMethod(Payment $response)
2020-10-26 14:40:50 +01:00
{
try {
2020-10-28 11:10:49 +01:00
$payment_meta = new stdClass;
2020-10-26 14:40:50 +01: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'];
$payment_meta->type = (int) GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
2020-10-27 13:44:16 +01: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
];
2020-12-07 14:50:30 +01:00
return $this->getParent()->storePaymentMethod($data);
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
}