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

151 lines
5.1 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;
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
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
2020-10-28 11:10:49 +01:00
private function processSuccessfulPayment(Payment $_payment)
2020-10-22 15:24:18 +02:00
{
if ($this->checkout->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-10-26 14:55:48 +01:00
'amount' => $this->checkout->payment_hash->data->raw_value,
'transaction_reference' => $_payment->reference,
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)]);
}
2020-10-28 11:10:49 +01:00
public function processUnsuccessfulPayment(Payment $_payment)
2020-10-22 15:24:18 +02:00
{
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
);
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
{
$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']);
2020-10-28 11:10:49 +01:00
} catch (Exception $e) {
return $this->processInternallyFailedPayment($this->checkout, $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-10-26 14:40:50 +01:00
'payment_method_id' => $this->checkout->payment_hash->data->payment_method_id,
];
2020-10-27 13:44:16 +01:00
return $this->checkout->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
}