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

337 lines
12 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;
2020-06-11 15:13:35 +02:00
use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
2020-06-12 16:23:46 +02:00
use App\Models\ClientGatewayToken;
2020-06-10 17:38:10 +02:00
use App\Models\GatewayType;
2020-06-15 13:04:05 +02:00
use App\Models\Payment;
2020-09-03 23:23:34 +02:00
use App\Models\PaymentHash;
2020-06-11 15:13:35 +02:00
use App\Models\PaymentType;
use App\Models\SystemLog;
use App\PaymentDrivers\BaseDriver;
2020-06-10 17:38:10 +02:00
use App\PaymentDrivers\CheckoutCom\Utilities;
2020-07-08 14:02:16 +02:00
use App\Utils\Ninja;
2020-06-10 17:38:10 +02:00
use App\Utils\Traits\SystemLogTrait;
2020-06-11 15:13:35 +02:00
use Checkout\CheckoutApi;
use Checkout\Library\Exceptions\CheckoutHttpException;
2020-06-15 14:18:44 +02:00
use Checkout\Models\Payments\IdSource;
2020-06-15 13:04:05 +02:00
use Checkout\Models\Payments\Payment as CheckoutPayment;
2020-06-11 15:13:35 +02:00
use Checkout\Models\Payments\TokenSource;
2020-06-10 17:38:10 +02:00
class CheckoutComPaymentDriver extends BaseDriver
2020-06-10 17:38:10 +02:00
{
use SystemLogTrait, Utilities;
/* The company gateway instance*/
public $company_gateway;
2020-06-11 15:13:35 +02:00
2020-06-10 17:38:10 +02:00
/* The Invitation */
public $invitation;
2020-06-10 17:38:10 +02:00
/* Gateway capabilities */
public $refundable = true;
2020-06-10 17:38:10 +02:00
/* Token billing */
public $token_billing = true;
2020-06-10 17:38:10 +02:00
/* Authorise payment methods */
public $can_authorise_credit_card = true;
2020-06-10 17:38:10 +02:00
/** Instance of \Checkout\CheckoutApi */
public $gateway;
2020-07-03 02:56:36 +02:00
public static $methods = [
GatewayType::CREDIT_CARD => '',
];
/**
* Returns the default gateway type.
*/
public function gatewayTypes()
{
return [
GatewayType::CREDIT_CARD,
];
}
2020-06-10 17:38:10 +02:00
/** Since with Checkout.com we handle only credit cards, this method should be empty. */
public function setPaymentMethod($string = null)
{
return $this;
}
public function init()
{
2020-06-15 13:02:44 +02:00
$config = [
'secret' => $this->company_gateway->getConfigField('secretApiKey'),
'public' => $this->company_gateway->getConfigField('publicApiKey'),
'sandbox' => $this->company_gateway->getConfigField('testMode'),
];
2020-06-15 13:27:14 +02:00
2020-06-15 13:02:44 +02:00
$this->gateway = new CheckoutApi($config['secret'], $config['sandbox'], $config['public']);
2020-06-10 17:38:10 +02:00
}
public function viewForType($gateway_type_id)
{
if ($gateway_type_id == GatewayType::CREDIT_CARD) {
return 'gateways.checkout.credit_card';
}
if ($gateway_type_id == GatewayType::TOKEN) {
return 'gateways.checkout.credit_card';
}
}
2020-07-03 14:39:29 +02:00
public function authorizeView($data)
{
return render('gateways.checkout.authorize');
}
2020-06-10 17:38:10 +02:00
public function processPaymentView(array $data)
{
$data['gateway'] = $this;
$data['company_gateway'] = $this->company_gateway;
2020-06-10 17:38:10 +02:00
$data['client'] = $this->client;
2020-06-12 16:32:00 +02:00
$data['currency'] = $this->client->getCurrencyCode();
2020-06-11 15:13:35 +02:00
$data['value'] = $this->convertToCheckoutAmount($data['amount_with_fee'], $this->client->getCurrencyCode());
$data['raw_value'] = $data['amount_with_fee'];
2020-06-10 17:38:10 +02:00
$data['customer_email'] = $this->client->present()->email;
return render($this->viewForType($data['payment_method_id']), $data);
}
public function processPaymentResponse($request)
{
2020-06-11 15:13:35 +02:00
$this->init();
$state = [
'server_response' => json_decode($request->gateway_response),
'value' => $request->value,
'raw_value' => $request->raw_value,
'currency' => $request->currency,
2020-09-03 23:23:34 +02:00
'payment_hash' =>$request->payment_hash,
2020-06-11 15:13:35 +02:00
];
$state = array_merge($state, $request->all());
2020-06-15 14:18:44 +02:00
$state['store_card'] = boolval($state['store_card']);
if ($request->has('token') && ! is_null($request->token)) {
2020-06-15 14:18:44 +02:00
$method = new IdSource($state['token']);
$payment = new CheckoutPayment($method, $state['currency']);
$payment->amount = $state['value'];
} else {
$method = new TokenSource($state['server_response']->cardToken);
$payment = new CheckoutPayment($method, $state['currency']);
$payment->amount = $state['value'];
if ($this->client->currency()->code === 'EUR') {
$payment->{'3ds'} = ['enabled' => true];
2020-06-15 14:18:44 +02:00
}
2020-06-12 14:48:54 +02:00
}
2020-06-11 15:13:35 +02:00
try {
$response = $this->gateway->payments()->request($payment);
$state['payment_response'] = $response;
if ($response->status === 'Authorized') {
return $this->processSuccessfulPayment($state);
}
if ($response->status === 'Pending') {
return $this->processPendingPayment($state);
}
if ($response->status === 'Declined') {
return $this->processUnsuccessfulPayment($state);
}
} catch (CheckoutHttpException $e) {
return $this->processInternallyFailedPayment($e, $state);
}
}
public function processSuccessfulPayment($state)
{
$state['charge_id'] = $state['payment_response']->id;
2020-06-15 14:23:46 +02:00
if (isset($state['store_card']) && $state['store_card']) {
2020-06-12 16:23:46 +02:00
$this->saveCard($state);
2020-06-11 15:13:35 +02:00
}
$data = [
'payment_method' => $state['charge_id'],
2020-06-15 14:18:44 +02:00
'payment_type' => PaymentType::parseCardType($state['payment_response']->source['scheme']),
2020-06-11 15:13:35 +02:00
'amount' => $state['raw_value'],
];
2020-06-15 13:04:05 +02:00
$payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
$payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$state['payment_hash']])->firstOrFail();
2020-09-03 23:23:34 +02:00
$this->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash);
2020-06-11 15:13:35 +02:00
2020-07-08 14:02:16 +02:00
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
2020-06-11 15:13:35 +02:00
$logger_message = [
'server_response' => $state['payment_response'],
'data' => $data,
2020-06-11 15:13:35 +02:00
];
SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_CHECKOUT, $this->client);
2020-06-12 14:48:54 +02:00
2020-06-11 15:13:35 +02:00
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]);
}
public function processPendingPayment($state)
{
2020-06-12 14:48:54 +02:00
$state['charge_id'] = $state['payment_response']->id;
2020-06-15 13:27:14 +02:00
2020-06-15 14:23:46 +02:00
if (isset($state['store_card']) && $state['store_card']) {
2020-06-12 16:23:46 +02:00
$this->saveCard($state);
2020-06-12 14:48:54 +02:00
}
$data = [
'payment_method' => $state['charge_id'],
2020-06-15 14:18:44 +02:00
'payment_type' => PaymentType::parseCardType($state['payment_response']->source['scheme']),
2020-06-12 14:48:54 +02:00
'amount' => $state['raw_value'],
];
2020-06-15 13:04:05 +02:00
$payment = $this->createPayment($data, Payment::STATUS_PENDING);
2020-06-12 14:48:54 +02:00
$this->attachInvoices($payment, $state['hashed_ids']);
$payment->service()->updateInvoicePayment();
2020-07-08 14:02:16 +02:00
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
2020-06-12 14:48:54 +02:00
$logger_message = [
'server_response' => $state['payment_response'],
'data' => $data,
2020-06-12 14:48:54 +02:00
];
SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_CHECKOUT, $this->client);
try {
return redirect($state['payment_response']->_links['redirect']['href']);
} catch (\Exception $e) {
SystemLogger::dispatch($logger_message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_CHECKOUT, $this->client);
throw new \Exception('Failed to process the payment.', 1);
}
2020-06-11 15:13:35 +02:00
}
public function processUnsuccessfulPayment($state)
{
2020-06-12 14:48:54 +02:00
PaymentFailureMailer::dispatch($this->client, $state['payment_response']->response_summary, $this->client->company, $state['payment_response']->amount);
$message = [
'server_response' => $state['server_response'],
'data' => $state,
];
SystemLogger::dispatch($message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_CHECKOUT, $this->client);
// throw new \Exception('Failed to process the payment: ' . $state['payment_response']->response_summary, 1);
return render('gateways.unsuccessful', [
'code' => $state['payment_response']->response_code,
]);
2020-06-11 15:13:35 +02:00
}
public function processInternallyFailedPayment($e, $state)
{
$message = json_decode($e->getBody());
PaymentFailureMailer::dispatch($this->client, $message->error_type, $this->client->company, $state['value']);
$message = [
'server_response' => $state['server_response'],
'data' => $message,
];
SystemLogger::dispatch($message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_CHECKOUT, $this->client);
throw new \Exception('Failed to process the payment.', 1);
}
2020-06-15 13:04:05 +02:00
public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment
2020-06-11 15:13:35 +02:00
{
$payment = parent::createPayment($data, $status);
$client_contact = $this->getContact();
$client_contact_id = $client_contact ? $client_contact->id : null;
$payment->amount = $data['amount'];
$payment->type_id = $data['payment_type'];
$payment->transaction_reference = $data['payment_method'];
$payment->client_contact_id = $client_contact_id;
$payment->save();
return $payment;
2020-06-10 17:38:10 +02:00
}
2020-06-12 16:23:46 +02:00
public function saveCard($state)
{
$company_gateway_token = new ClientGatewayToken();
$company_gateway_token->company_id = $this->client->company->id;
$company_gateway_token->client_id = $this->client->id;
$company_gateway_token->token = $state['payment_response']->source['id'];
$company_gateway_token->company_gateway_id = $this->company_gateway->id;
$company_gateway_token->gateway_type_id = $state['payment_method_id'];
$company_gateway_token->meta = $state['payment_response']->source;
$company_gateway_token->save();
if ($this->client->gateway_tokens->count() == 1) {
$this->client->gateway_tokens()->update(['is_default' => 0]);
$company_gateway_token->is_default = 1;
$company_gateway_token->save();
}
}
2020-06-24 16:29:01 +02:00
public function refund(Payment $payment, $amount, $return_client_response = false)
2020-06-24 16:29:01 +02:00
{
$this->init();
$checkout_payment = new \Checkout\Models\Payments\Refund($payment->transaction_reference);
2020-06-24 16:29:01 +02:00
try {
$refund = $this->gateway->payments()->refund($checkout_payment);
$checkout_payment = $this->gateway->payments()->details($refund->id);
$response = ['refund_response' => $refund, 'checkout_payment_fetch' => $checkout_payment];
return [
'transaction_reference' => $refund->action_id,
'transaction_response' => json_encode($response),
'success' => $checkout_payment->status == 'Refunded',
'description' => $checkout_payment->status,
'code' => $checkout_payment->http_code,
];
2020-06-24 16:29:01 +02:00
} catch (CheckoutHttpException $e) {
return [
'transaction_reference' => null,
'transaction_response' => json_encode($e->getMessage()),
'success' => false,
'description' => $e->getMessage(),
'code' => $e->getCode(),
];
2020-06-24 16:29:01 +02:00
}
}
2020-07-08 04:20:44 +02:00
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
}
2020-06-10 17:38:10 +02:00
}