2020-06-10 17:38:10 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-06-10 17:38:10 +02:00
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @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;
|
|
|
|
|
2021-01-27 15:58:19 +01:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
2020-12-07 14:49:56 +01:00
|
|
|
use App\Http\Requests\Payments\PaymentWebhookRequest;
|
2021-01-27 15:58:19 +01:00
|
|
|
use App\Jobs\Mail\PaymentFailureMailer;
|
|
|
|
use App\Jobs\Util\SystemLogger;
|
2020-06-12 16:23:46 +02:00
|
|
|
use App\Models\ClientGatewayToken;
|
2020-12-07 14:49:56 +01:00
|
|
|
use App\Models\Company;
|
2020-06-10 17:38:10 +02:00
|
|
|
use App\Models\GatewayType;
|
2021-01-27 15:58:19 +01:00
|
|
|
use App\Models\Invoice;
|
2020-06-15 13:04:05 +02:00
|
|
|
use App\Models\Payment;
|
2020-09-03 23:23:34 +02:00
|
|
|
use App\Models\PaymentHash;
|
2021-01-27 15:58:19 +01:00
|
|
|
use App\Models\PaymentType;
|
2020-10-27 12:53:35 +01:00
|
|
|
use App\Models\SystemLog;
|
2020-10-28 11:10:49 +01:00
|
|
|
use App\PaymentDrivers\CheckoutCom\CreditCard;
|
2020-06-10 17:38:10 +02:00
|
|
|
use App\PaymentDrivers\CheckoutCom\Utilities;
|
|
|
|
use App\Utils\Traits\SystemLogTrait;
|
2020-06-11 15:13:35 +02:00
|
|
|
use Checkout\CheckoutApi;
|
|
|
|
use Checkout\Library\Exceptions\CheckoutHttpException;
|
2021-01-27 15:58:19 +01:00
|
|
|
use Checkout\Models\Payments\IdSource;
|
2020-10-28 11:10:49 +01:00
|
|
|
use Checkout\Models\Payments\Refund;
|
2020-12-07 14:49:56 +01:00
|
|
|
use Exception;
|
2020-06-10 17:38:10 +02:00
|
|
|
|
2020-09-09 02:58:35 +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 */
|
2020-09-09 02:58:35 +02:00
|
|
|
public $invitation;
|
2020-06-10 17:38:10 +02:00
|
|
|
|
|
|
|
/* Gateway capabilities */
|
2020-09-09 02:58:35 +02:00
|
|
|
public $refundable = true;
|
2020-06-10 17:38:10 +02:00
|
|
|
|
|
|
|
/* Token billing */
|
2020-09-09 02:58:35 +02:00
|
|
|
public $token_billing = true;
|
2020-06-10 17:38:10 +02:00
|
|
|
|
|
|
|
/* Authorise payment methods */
|
2020-09-09 02:58:35 +02:00
|
|
|
public $can_authorise_credit_card = true;
|
2020-06-10 17:38:10 +02:00
|
|
|
|
2020-10-22 15:24:18 +02:00
|
|
|
/**
|
2020-10-28 11:10:49 +01:00
|
|
|
* @var CheckoutApi;
|
2020-10-22 15:24:18 +02:00
|
|
|
*/
|
2020-06-10 17:38:10 +02:00
|
|
|
public $gateway;
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
2020-10-09 08:55:03 +02:00
|
|
|
public $payment_method; //the gateway type id
|
|
|
|
|
2020-07-03 02:56:36 +02:00
|
|
|
public static $methods = [
|
2020-10-28 11:10:49 +01:00
|
|
|
GatewayType::CREDIT_CARD => CreditCard::class,
|
2020-07-03 02:56:36 +02:00
|
|
|
];
|
|
|
|
|
2020-10-27 12:53:35 +01:00
|
|
|
const SYSTEM_LOG_TYPE = SystemLog::TYPE_CHECKOUT;
|
|
|
|
|
2020-09-09 02:58:35 +02:00
|
|
|
/**
|
|
|
|
* Returns the default gateway type.
|
|
|
|
*/
|
|
|
|
public function gatewayTypes()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
GatewayType::CREDIT_CARD,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
/**
|
2020-09-10 03:05:42 +02:00
|
|
|
* Since with Checkout.com we handle only credit cards, this method should be empty.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param int|null $payment_method
|
|
|
|
* @return CheckoutComPaymentDriver
|
2020-09-10 03:05:42 +02:00
|
|
|
*/
|
2020-10-28 11:10:49 +01:00
|
|
|
public function setPaymentMethod($payment_method = null): CheckoutComPaymentDriver
|
2020-06-10 17:38:10 +02:00
|
|
|
{
|
2020-10-28 11:10:49 +01:00
|
|
|
// At the moment Checkout.com payment
|
2020-10-22 15:24:18 +02:00
|
|
|
// driver only supports payments using credit card.
|
|
|
|
|
|
|
|
$class = self::$methods[GatewayType::CREDIT_CARD];
|
|
|
|
|
|
|
|
$this->payment_method = new $class($this);
|
|
|
|
|
2020-06-10 17:38:10 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2020-09-10 03:05:42 +02:00
|
|
|
/**
|
|
|
|
* Initialize the checkout payment driver
|
|
|
|
* @return $this
|
|
|
|
*/
|
2020-10-22 15:24:18 +02:00
|
|
|
public function init()
|
2020-06-10 17:38:10 +02:00
|
|
|
{
|
2020-06-15 13:02:44 +02:00
|
|
|
$config = [
|
2021-01-27 15:58:19 +01:00
|
|
|
'secret' => $this->company_gateway->getConfigField('secretApiKey'),
|
|
|
|
'public' => $this->company_gateway->getConfigField('publicApiKey'),
|
2020-06-15 13:02:44 +02:00
|
|
|
'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-09-10 03:05:42 +02:00
|
|
|
|
|
|
|
return $this;
|
2020-06-10 17:38:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 03:05:42 +02:00
|
|
|
/**
|
|
|
|
* Process different view depending on payment type
|
2021-01-27 15:58:19 +01:00
|
|
|
* @param int $gateway_type_id The gateway type
|
2020-09-10 03:05:42 +02:00
|
|
|
* @return string The view string
|
|
|
|
*/
|
2020-06-10 17:38:10 +02:00
|
|
|
public function viewForType($gateway_type_id)
|
|
|
|
{
|
2020-10-28 11:10:49 +01:00
|
|
|
// At the moment Checkout.com payment
|
2020-10-22 15:24:18 +02:00
|
|
|
// driver only supports payments using credit card.
|
2020-10-09 08:55:03 +02:00
|
|
|
|
2020-10-22 15:24:18 +02:00
|
|
|
return 'gateways.checkout.credit_card.pay';
|
2020-06-10 17:38:10 +02:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:39:29 +02:00
|
|
|
public function authorizeView($data)
|
|
|
|
{
|
2020-10-22 15:24:18 +02:00
|
|
|
return $this->payment_method->authorizeView($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeResponse($data)
|
|
|
|
{
|
|
|
|
return $this->payment_method->authorizeResponse($data);
|
2020-07-03 14:39:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 03:05:42 +02:00
|
|
|
/**
|
|
|
|
* Payment View
|
2020-10-28 11:10:49 +01:00
|
|
|
*
|
2021-01-27 15:58:19 +01:00
|
|
|
* @param array $data Payment data array
|
2020-09-10 03:05:42 +02:00
|
|
|
* @return view The payment view
|
|
|
|
*/
|
2020-06-10 17:38:10 +02:00
|
|
|
public function processPaymentView(array $data)
|
|
|
|
{
|
2020-10-22 15:24:18 +02:00
|
|
|
return $this->payment_method->paymentView($data);
|
2020-06-10 17:38:10 +02:00
|
|
|
}
|
|
|
|
|
2020-09-10 03:05:42 +02:00
|
|
|
/**
|
|
|
|
* Process the payment response
|
2020-10-28 11:10:49 +01:00
|
|
|
*
|
2021-01-27 15:58:19 +01:00
|
|
|
* @param Request $request The payment request
|
2020-09-10 03:05:42 +02:00
|
|
|
* @return view The payment response view
|
|
|
|
*/
|
2020-06-10 17:38:10 +02:00
|
|
|
public function processPaymentResponse($request)
|
|
|
|
{
|
2020-10-22 15:24:18 +02:00
|
|
|
return $this->payment_method->paymentResponse($request);
|
2020-06-11 15:13:35 +02:00
|
|
|
}
|
|
|
|
|
2020-10-26 14:40:50 +01:00
|
|
|
public function storePaymentMethod(array $data)
|
2020-06-12 16:23:46 +02:00
|
|
|
{
|
2020-10-26 14:40:50 +01:00
|
|
|
return $this->storeGatewayToken($data);
|
2020-06-12 16:23:46 +02:00
|
|
|
}
|
2020-06-24 16:29:01 +02:00
|
|
|
|
2020-09-09 02:58:35 +02:00
|
|
|
public function refund(Payment $payment, $amount, $return_client_response = false)
|
2020-06-24 16:29:01 +02:00
|
|
|
{
|
2020-06-25 17:08:15 +02:00
|
|
|
$this->init();
|
|
|
|
|
2020-10-28 11:10:49 +01:00
|
|
|
$checkout_payment = new Refund($payment->transaction_reference);
|
2020-06-24 16:29:01 +02:00
|
|
|
|
|
|
|
try {
|
2020-06-25 17:08:15 +02:00
|
|
|
$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) {
|
2020-06-25 17:08:15 +02:00
|
|
|
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
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
|
|
|
|
{
|
2021-01-27 15:58:19 +01:00
|
|
|
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
|
|
|
|
$invoice = Invoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->first();
|
|
|
|
|
|
|
|
if ($invoice) {
|
|
|
|
$description = "Invoice {$invoice->number} for {$amount} for client {$this->client->present()->name()}";
|
|
|
|
} else {
|
|
|
|
$description = "Payment with no invoice for amount {$amount} for client {$this->client->present()->name()}";
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
$method = new IdSource($cgt->token);
|
|
|
|
|
|
|
|
$payment = new \Checkout\Models\Payments\Payment($method, $this->client->getCurrencyCode());
|
|
|
|
$payment->amount = $this->convertToCheckoutAmount($amount, $this->client->getCurrencyCode());
|
2021-04-12 13:56:08 +02:00
|
|
|
//$payment->reference = $cgt->meta->last4 . '-' . now();
|
|
|
|
$payment->reference = $invoice->number . '-' . now();
|
2021-01-27 15:58:19 +01:00
|
|
|
|
|
|
|
$request = new PaymentResponseRequest();
|
|
|
|
$request->setMethod('POST');
|
2021-01-28 16:13:32 +01:00
|
|
|
$request->request->add(['payment_hash' => $payment_hash->hash]);
|
|
|
|
|
2021-01-29 12:00:30 +01:00
|
|
|
//$this->setPaymentHash($payment_hash);
|
2021-01-27 15:58:19 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
$response = $this->gateway->payments()->request($payment);
|
|
|
|
|
|
|
|
if ($response->status == 'Authorized') {
|
|
|
|
$this->confirmGatewayFee($request);
|
|
|
|
|
2021-01-28 16:13:32 +01:00
|
|
|
$data = [
|
|
|
|
'payment_method' => $response->source['id'],
|
|
|
|
'payment_type' => PaymentType::parseCardType(strtolower($response->source['scheme'])),
|
|
|
|
'amount' => $amount,
|
|
|
|
'transaction_reference' => $response->id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$payment = $this->createPayment($data, \App\Models\Payment::STATUS_COMPLETED);
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $response, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_CHECKOUT,
|
|
|
|
$this->client
|
|
|
|
);
|
|
|
|
|
|
|
|
return $payment;
|
2021-01-27 15:58:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($response->status == 'Declined') {
|
|
|
|
$this->unWindGatewayFees($payment_hash);
|
|
|
|
|
|
|
|
PaymentFailureMailer::dispatch(
|
|
|
|
$this->client, $response->response_summary,
|
|
|
|
$this->client->company,
|
2021-01-28 16:13:32 +01:00
|
|
|
$amount
|
2021-01-27 15:58:19 +01:00
|
|
|
);
|
|
|
|
|
2021-01-28 16:13:32 +01:00
|
|
|
$message = [
|
|
|
|
'server_response' => $response,
|
|
|
|
'data' => $payment_hash->data,
|
|
|
|
];
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$message,
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_CHECKOUT,
|
|
|
|
$this->client
|
|
|
|
);
|
2021-01-27 15:58:19 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (\Exception | CheckoutHttpException $e) {
|
|
|
|
$this->unWindGatewayFees($payment_hash);
|
2021-01-28 16:13:32 +01:00
|
|
|
$message = $e instanceof CheckoutHttpException
|
|
|
|
? $e->getBody()
|
|
|
|
: $e->getMessage();
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'status' => '',
|
|
|
|
'error_type' => '',
|
|
|
|
'error_code' => $e->getCode(),
|
|
|
|
'param' => '',
|
|
|
|
'message' => $message,
|
|
|
|
];
|
2021-01-27 15:58:19 +01:00
|
|
|
|
2021-01-28 16:13:32 +01:00
|
|
|
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_CHECKOUT, $this->client);
|
2021-01-27 15:58:19 +01:00
|
|
|
}
|
2020-12-07 14:49:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null)
|
|
|
|
{
|
|
|
|
$this->init();
|
|
|
|
$this->setPaymentHash($request->getPaymentHash());
|
|
|
|
|
|
|
|
try {
|
|
|
|
$payment = $this->gateway->payments()->details($request->query('cko-session-id'));
|
|
|
|
|
|
|
|
if ($payment->approved) {
|
|
|
|
return $this->processSuccessfulPayment($payment);
|
|
|
|
} else {
|
|
|
|
return $this->processUnsuccessfulPayment($payment);
|
|
|
|
}
|
|
|
|
} catch (CheckoutHttpException | Exception $e) {
|
|
|
|
return $this->processInternallyFailedPayment($this, $e);
|
|
|
|
}
|
2020-09-06 11:38:10 +02:00
|
|
|
}
|
2021-01-20 16:19:01 +01:00
|
|
|
|
|
|
|
public function detach(ClientGatewayToken $clientGatewayToken)
|
|
|
|
{
|
|
|
|
// Gateway doesn't support this feature.
|
|
|
|
}
|
2020-06-10 17:38:10 +02:00
|
|
|
}
|