1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/PaymentDrivers/CheckoutComPaymentDriver.php

231 lines
6.9 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-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;
use App\PaymentDrivers\BaseDriver;
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;
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
2020-10-22 15:24:18 +02:00
/**
* @var \Checkout\CheckoutApi;
*/
2020-06-10 17:38:10 +02:00
public $gateway;
public $payment_method; //the gateway type id
/**
* @var \App\Models\PaymentHash
*/
public $payment_hash;
2020-07-03 02:56:36 +02:00
public static $methods = [
2020-10-22 15:24:18 +02:00
GatewayType::CREDIT_CARD => \App\PaymentDrivers\CheckoutCom\CreditCard::class,
2020-07-03 02:56:36 +02:00
];
/**
* Returns the default gateway type.
*/
public function gatewayTypes()
{
return [
GatewayType::CREDIT_CARD,
];
}
2020-09-10 03:05:42 +02:00
/**
* Since with Checkout.com we handle only credit cards, this method should be empty.
* @param $string payment_method string
*/
public function setPaymentMethod($payment_method = null)
2020-06-10 17:38:10 +02:00
{
2020-10-22 15:24:18 +02:00
// At the moment Checkout.com payment
// 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 = [
'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-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
* @param int $gateway_type_id The gateway type
* @return string The view string
*/
2020-06-10 17:38:10 +02:00
public function viewForType($gateway_type_id)
{
2020-10-22 15:24:18 +02:00
// At the moment Checkout.com payment
// driver only supports payments using credit card.
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
*
* @param array $data Payment data array
* @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
*
* @param Request $request The payment request
* @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-06-12 16:23:46 +02:00
public function saveCard($state)
{
//some cards just can't be tokenized....
2020-10-22 15:24:18 +02:00
if (!$state['payment_response']->source['id'])
return;
2020-10-22 15:24:18 +02:00
// [id] => src_hck5nsv3fljehbam2cvdm7fioa
// [type] => card
// [expiry_month] => 10
// [expiry_year] => 2022
// [scheme] => Visa
// [last4] => 4242
// [fingerprint] => 688192847DB9AE8A26C53776D036D5B8AD2CEAF1D5A8F5475F542B021041EFA1
// [bin] => 424242
// [card_type] => Credit
// [card_category] => Consumer
// [issuer] => JPMORGAN CHASE BANK NA
// [issuer_country] => US
// [product_id] => A
// [product_type] => Visa Traditional
// [avs_check] => S
// [cvv_check] => Y
// [payouts] => 1
// [fast_funds] => d
$payment_meta = new \stdClass;
$payment_meta->exp_month = (string)$state['payment_response']->source['expiry_month'];
$payment_meta->exp_year = (string)$state['payment_response']->source['expiry_year'];
$payment_meta->brand = (string)$state['payment_response']->source['scheme'];
$payment_meta->last4 = (string)$state['payment_response']->source['last4'];
$payment_meta->type = $this->payment_method;
2020-06-12 16:23:46 +02:00
$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 = $payment_meta;
2020-06-12 16:23:46 +02:00
$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
}