1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/PaymentDrivers/CheckoutCom/Utilities.php

157 lines
5.0 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) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers\CheckoutCom;
2020-10-22 15:24:18 +02:00
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\PaymentType;
use App\Models\SystemLog;
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
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
private function processSuccessfulPayment(\Checkout\Models\Payments\Payment $_payment)
{
if ($this->checkout->payment_hash->data->store_card) {
2020-10-22 15:24:18 +02:00
// $this->saveCreditCard();
}
$data = [
'payment_method' => $_payment->source['id'],
'payment_type' => PaymentType::parseCardType(strtolower($_payment->source['scheme'])),
'amount' => $this->checkout->payment_hash->data->value,
2020-10-22 15:24:18 +02:00
];
$payment = $this->checkout->createPayment($data, \App\Models\Payment::STATUS_COMPLETED);
SystemLogger::dispatch(
['response' => $_payment, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client
);
return redirect()->route('client.payments.show', ['payment' => $this->checkout->encodePrimaryKey($payment->id)]);
}
public function processUnsuccessfulPayment(\Checkout\Models\Payments\Payment $_payment)
{
PaymentFailureMailer::dispatch(
$this->checkout->client,
$_payment,
$this->checkout->client->company,
$this->checkout->payment_hash->data->value
2020-10-22 15:24:18 +02:00
);
$message = [
'server_response' => $_payment,
'data' => $this->checkout->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->checkout->client
);
return render('gateways.unsuccessful', [
'code' => $_payment->http_code,
'message' => $_payment->status,
]);
}
private function processPendingPayment(\Checkout\Models\Payments\Payment $_payment)
{
$data = [
'payment_method' => $_payment->source['id'],
'payment_type' => PaymentType::parseCardType(strtolower($_payment->source['scheme'])),
'amount' => $this->checkout->payment_hash->data->value,
2020-10-22 15:24:18 +02:00
];
$payment = $this->checkout->createPayment($data, \App\Models\Payment::STATUS_PENDING);
SystemLogger::dispatch(
['response' => $_payment, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client
);
try {
return redirect($_payment->_links['redirect']['href']);
} catch (\Exception $e) {
return $this->processInternallyFailedPayment($e);
}
}
private function processInternallyFailedPayment($e)
{
if ($e instanceof \Checkout\Library\Exceptions\CheckoutHttpException) {
$error = $e->getBody();
}
if ($e instanceof \Exception) {
$error = $e->getMessage();
}
PaymentFailureMailer::dispatch(
$this->checkout->client,
$error,
$this->checkout->client->company,
$this->checkout->payment_hash->data->value
2020-10-22 15:24:18 +02:00
);
SystemLogger::dispatch(
$this->checkout->payment_hash,
2020-10-22 15:24:18 +02:00
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client,
);
return render('gateways.unsuccessful', [
'error' => $e->getCode(),
'message' => $error,
]);
}
2020-06-10 17:38:10 +02:00
}