1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/PaymentDrivers/CheckoutComPaymentDriver.php

628 lines
20 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2020-06-10 17:38:10 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-06-10 17:38:10 +02:00
*/
namespace App\PaymentDrivers;
2024-03-18 03:50:12 +01:00
use Exception;
use App\Models\Company;
2021-01-27 15:58:19 +01:00
use App\Models\Invoice;
2020-06-15 13:04:05 +02:00
use App\Models\Payment;
2024-03-18 03:50:12 +01:00
use App\Models\SystemLog;
use Checkout\CheckoutSdk;
use Checkout\Environment;
use Checkout\Common\Phone;
use App\Models\GatewayType;
2020-09-03 23:23:34 +02:00
use App\Models\PaymentHash;
2021-01-27 15:58:19 +01:00
use App\Models\PaymentType;
2024-03-18 03:50:12 +01:00
use Illuminate\Support\Carbon;
use App\Jobs\Util\SystemLogger;
use App\Exceptions\PaymentFailed;
use App\Models\ClientGatewayToken;
2022-06-16 03:21:10 +02:00
use Checkout\CheckoutApiException;
2024-03-18 03:50:12 +01:00
use App\Utils\Traits\SystemLogTrait;
use Checkout\Payments\RefundRequest;
use Illuminate\Support\Facades\Auth;
2022-06-16 03:21:10 +02:00
use Checkout\CheckoutArgumentException;
use Checkout\Customers\CustomerRequest;
2024-03-18 03:50:12 +01:00
use Checkout\CheckoutAuthorizationException;
use App\PaymentDrivers\CheckoutCom\Utilities;
2023-09-12 02:27:30 +02:00
use Checkout\Payments\Request\PaymentRequest;
2024-03-18 03:50:12 +01:00
use App\PaymentDrivers\CheckoutCom\CreditCard;
use App\PaymentDrivers\CheckoutCom\CheckoutWebhook;
use App\Http\Requests\Payments\PaymentWebhookRequest;
2023-09-12 02:27:30 +02:00
use Checkout\Payments\Request\Source\RequestIdSource;
2024-03-18 03:50:12 +01:00
use App\Http\Requests\Gateways\Checkout3ds\Checkout3dsRequest;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use Checkout\Payments\Previous\PaymentRequest as PreviousPaymentRequest;
use Checkout\Payments\Previous\Source\RequestIdSource as SourceRequestIdSource;
2020-06-10 17:38:10 +02:00
class CheckoutComPaymentDriver extends BaseDriver
2020-06-10 17:38:10 +02:00
{
2024-01-14 05:05:00 +01:00
use SystemLogTrait;
use Utilities;
2020-06-10 17:38:10 +02:00
/* 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
2022-06-16 02:01:24 +02:00
public $is_four_api = false;
2020-10-22 15:24:18 +02:00
/**
2023-09-12 02:27:30 +02:00
* @var CheckoutSdk;
2020-10-22 15:24:18 +02:00
*/
2020-06-10 17:38:10 +02:00
public $gateway;
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
];
2024-01-14 05:05:00 +01:00
public const SYSTEM_LOG_TYPE = SystemLog::TYPE_CHECKOUT;
/**
* Returns the default gateway type.
*/
public function gatewayTypes(): array
{
$types = [];
2021-06-30 13:21:46 +02:00
$types[] = GatewayType::CREDIT_CARD;
return $types;
}
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
*/
public function setPaymentMethod($payment_method = null): self
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
{
2023-09-12 02:27:30 +02:00
if (str_contains($this->company_gateway->getConfigField('secretApiKey'), '-')) {
2024-01-14 05:05:00 +01:00
2023-09-12 02:27:30 +02:00
$this->is_four_api = true; //was four api, now known as previous.
2023-09-26 15:04:31 +02:00
/** @phpstan-ignore-next-line **/
2023-09-12 02:27:30 +02:00
$builder = CheckoutSdk::builder()
->previous()
->staticKeys()
->environment($this->company_gateway->getConfigField('testMode') ? Environment::sandbox() : Environment::production()) /** phpstan-ignore-line **/
2023-09-12 02:27:30 +02:00
->publicKey($this->company_gateway->getConfigField('publicApiKey'))
->secretKey($this->company_gateway->getConfigField('secretApiKey'));
2022-06-15 14:47:25 +02:00
$this->gateway = $builder->build();
2024-01-14 05:05:00 +01:00
} else {
2023-09-26 15:04:31 +02:00
/** @phpstan-ignore-next-line **/
$builder = CheckoutSdk::builder()
->staticKeys()
->environment($this->company_gateway->getConfigField('testMode') ? Environment::sandbox() : Environment::production()) /** phpstan-ignore-line **/
2023-09-12 02:27:30 +02:00
->publicKey($this->company_gateway->getConfigField('publicApiKey'))
2023-09-26 15:04:31 +02:00
->secretKey($this->company_gateway->getConfigField('secretApiKey'));
2023-09-12 02:27:30 +02:00
2022-06-15 14:47:25 +02:00
$this->gateway = $builder->build();
2020-09-10 03:05:42 +02:00
2023-09-12 02:27:30 +02:00
}
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
2023-10-26 04:57:44 +02:00
*
2021-01-27 15:58:19 +01:00
* @param int $gateway_type_id The gateway type
2023-09-12 02:27:30 +02:00
* @return string The view string
2020-09-10 03:05:42 +02:00
*/
2020-06-10 17:38:10 +02:00
public function viewForType($gateway_type_id)
{
2020-10-22 15:24:18 +02:00
return 'gateways.checkout.credit_card.pay';
2020-06-10 17:38:10 +02:00
}
2024-01-14 05:05:00 +01:00
2023-09-12 02:27:30 +02:00
/**
* Authorize View
*
* @param array $data
* @return \Illuminate\View\View
*/
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);
}
2024-01-14 05:05:00 +01:00
2023-09-12 02:27:30 +02:00
/**
* Authorize Response
*
* @param array $data
* @return \Illuminate\View\View
*/
2020-10-22 15:24:18 +02:00
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
2023-09-12 02:27:30 +02:00
* @return \Illuminate\View\View
2020-09-10 03:05:42 +02:00
*/
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
*
2023-08-06 04:20:18 +02:00
* @param \Illuminate\Http\Request $request The payment request
2023-09-12 02:27:30 +02:00
* @return \Illuminate\View\View
2020-09-10 03:05:42 +02:00
*/
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
}
2024-01-14 05:05:00 +01:00
2023-09-12 02:27:30 +02:00
/**
* Store PaymentMethod
*
* @param array $data
* @return ?ClientGatewayToken $token
*/
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
public function refund(Payment $payment, $amount, $return_client_response = false)
2020-06-24 16:29:01 +02:00
{
$this->init();
2024-02-13 05:25:18 +01:00
if($this->company_gateway->update_details) {
$this->updateCustomer();
2024-02-13 05:25:18 +01:00
}
2022-06-16 03:21:10 +02:00
$request = new RefundRequest();
$request->reference = "{$payment->transaction_reference} ".now();
2022-06-16 03:21:10 +02:00
$request->amount = $this->convertToCheckoutAmount($amount, $this->client->getCurrencyCode());
2020-06-24 16:29:01 +02:00
try {
2023-09-12 02:27:30 +02:00
2022-06-16 03:21:10 +02:00
$response = $this->gateway->getPaymentsClient()->refundPayment($payment->transaction_reference, $request);
SystemLogger::dispatch(
array_merge(['message' => "Gateway Refund"], $response),
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_CHECKOUT,
$payment->client,
$payment->company,
);
return [
2022-06-16 03:21:10 +02:00
'transaction_reference' => $response['action_id'],
'transaction_response' => json_encode($response),
2022-06-16 03:21:10 +02:00
'success' => true,
'description' => $response['reference'],
'code' => 202,
];
2022-06-16 03:21:10 +02:00
} catch (CheckoutApiException $e) {
// API error
2022-09-27 12:01:58 +02:00
throw new PaymentFailed($e->getMessage(), $e->getCode());
2022-06-16 03:21:10 +02:00
} catch (CheckoutArgumentException $e) {
// Bad arguments
SystemLogger::dispatch(
$e->getMessage(),
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_CHECKOUT,
$payment->client,
$payment->company,
);
2022-09-27 12:01:58 +02:00
return [
'transaction_reference' => null,
'transaction_response' => json_encode($e->getMessage()),
'success' => false,
'description' => $e->getMessage(),
'code' => $e->getCode(),
];
2022-06-16 03:21:10 +02:00
} catch (CheckoutAuthorizationException $e) {
SystemLogger::dispatch(
$e->getMessage(),
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_CHECKOUT,
$payment->client,
$payment->company,
);
2022-09-27 12:01:58 +02:00
return [
'transaction_reference' => null,
'transaction_response' => json_encode($e->getMessage()),
'success' => false,
'description' => $e->getMessage(),
'code' => $e->getCode(),
];
}
2022-06-16 03:21:10 +02:00
}
public function getCustomer()
{
try {
$response = $this->gateway->getCustomersClient()->get($this->client->present()->email());
2022-06-16 03:21:10 +02:00
return $response;
} catch (\Exception $e) {
2023-09-12 02:27:30 +02:00
$request = new CustomerRequest();
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
$phone = new Phone();
$phone->number = substr(str_pad($this->client->present()->phone(), 6, "0", STR_PAD_RIGHT), 0, 24);
$request->email = $this->client->present()->email();
$request->name = $this->client->present()->name();
$request->phone = $phone;
try {
$response = $this->gateway->getCustomersClient()->create($request);
} catch (CheckoutApiException $e) {
// API error
$error_details = $e->error_details;
2023-09-12 02:27:30 +02:00
if (isset($error_details['error_codes']) ?? false) {
2023-02-16 02:36:09 +01:00
$error_details = end($e->error_details['error_codes']);
2023-09-12 02:27:30 +02:00
} else {
$error_details = $e->getMessage();
2023-02-16 02:36:09 +01:00
}
2023-09-12 02:27:30 +02:00
throw new PaymentFailed($error_details, 400);
2023-02-16 02:36:09 +01:00
} catch (CheckoutArgumentException $e) {
2023-09-12 02:27:30 +02:00
throw new PaymentFailed($e->getMessage(), $e->getCode());
2023-02-16 02:36:09 +01:00
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
2023-09-12 02:27:30 +02:00
throw new PaymentFailed("Checkout Gateway credentials are invalid", 400);
2023-02-16 02:36:09 +01:00
}
return $response;
2022-06-16 03:21:10 +02:00
}
}
2024-01-14 05:05:00 +01:00
public function updateCustomer($customer_id = null)
2023-09-26 15:04:31 +02:00
{
2024-02-13 05:25:18 +01:00
if(!$customer_id) {
return;
2024-02-13 05:25:18 +01:00
}
2024-01-27 05:43:37 +01:00
try {
2024-02-13 05:25:18 +01:00
$request = new CustomerRequest();
2023-09-26 15:04:31 +02:00
$phone = new Phone();
$phone->number = substr(str_pad($this->client->present()->phone(), 6, "0", STR_PAD_RIGHT), 0, 24);
$request->email = $this->client->present()->email();
$request->name = $this->client->present()->name();
$request->phone = $phone;
2023-09-26 15:04:31 +02:00
$response = $this->gateway->getCustomersClient()->update($customer_id, $request);
2023-09-26 15:04:31 +02:00
} catch (CheckoutApiException $e) {
2024-01-27 05:43:37 +01:00
nlog($e->getMessage());
2023-09-26 15:04:31 +02:00
} catch (CheckoutAuthorizationException $e) {
2024-01-27 05:43:37 +01:00
nlog($e->getMessage());
2023-09-26 15:04:31 +02:00
}
}
2023-09-12 02:27:30 +02:00
/**
* Boots a request for a token payment
*
* @param string $token
* @return PreviousPaymentRequest | PaymentRequest
*/
2022-06-16 03:21:10 +02:00
public function bootTokenRequest($token)
{
if ($this->is_four_api) {
2022-06-16 03:21:10 +02:00
$token_source = new SourceRequestIdSource();
$token_source->id = $token;
2023-09-12 02:27:30 +02:00
$request = new PreviousPaymentRequest();
2022-06-16 03:21:10 +02:00
$request->source = $token_source;
} else {
2022-06-16 03:21:10 +02:00
$token_source = new RequestIdSource();
$token_source->id = $token;
2023-09-12 02:27:30 +02:00
$request = new PaymentRequest();
2022-06-16 03:21:10 +02:00
$request->source = $token_source;
}
return $request;
2020-06-24 16:29:01 +02:00
}
2020-07-08 04:20:44 +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;
2023-08-06 09:03:12 +02:00
$invoice = Invoice::query()->whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->withTrashed()->first();
2023-09-12 02:27:30 +02:00
$this->client = $invoice->client;
$this->payment_hash = $payment_hash;
2021-01-27 15:58:19 +01:00
$this->init();
2024-02-13 05:25:18 +01:00
2022-06-16 03:21:10 +02:00
$paymentRequest = $this->bootTokenRequest($cgt->token);
$paymentRequest->amount = $this->convertToCheckoutAmount($amount, $this->client->getCurrencyCode());
$paymentRequest->reference = '#'.$invoice->number.' - '.now();
2022-06-16 03:21:10 +02:00
$paymentRequest->customer = $this->getCustomer();
2023-07-21 07:48:10 +02:00
$paymentRequest->metadata = ['udf1' => 'Invoice Ninja', 'udf2' => $payment_hash->hash];
2022-06-16 03:21:10 +02:00
$paymentRequest->currency = $this->client->getCurrencyCode();
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-27 15:58:19 +01:00
try {
2022-06-16 03:21:10 +02:00
$response = $this->gateway->getPaymentsClient()->requestPayment($paymentRequest);
2021-01-27 15:58:19 +01:00
2022-06-16 03:21:10 +02:00
if ($response['status'] == 'Authorized') {
2024-08-13 07:14:41 +02:00
2021-01-28 16:13:32 +01:00
$data = [
2022-06-16 03:21:10 +02:00
'payment_method' => $response['source']['id'],
'payment_type' => PaymentType::parseCardType(strtolower($response['source']['scheme'])),
2021-01-28 16:13:32 +01:00
'amount' => $amount,
2022-06-16 03:21:10 +02:00
'transaction_reference' => $response['id'],
2024-08-13 07:14:41 +02:00
'gateway_type_id' => GatewayType::CREDIT_CARD,
2021-01-28 16:13:32 +01:00
];
2024-08-13 07:14:41 +02:00
$this->confirmGatewayFee($data);
$payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
2021-01-28 16:13:32 +01:00
SystemLogger::dispatch(
['response' => $response, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_CHECKOUT,
2023-11-24 07:32:10 +01:00
$this->client,
$this->client->company,
2021-01-28 16:13:32 +01:00
);
return $payment;
2021-01-27 15:58:19 +01:00
}
2022-06-16 03:21:10 +02:00
if ($response['status'] == 'Declined') {
2021-01-27 15:58:19 +01:00
$this->unWindGatewayFees($payment_hash);
$this->sendFailureMail($response['status'].' '.$response['response_summary']);
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,
2023-11-24 07:32:10 +01:00
$this->client,
$this->client->company
2021-01-28 16:13:32 +01:00
);
2021-01-27 15:58:19 +01:00
return false;
}
2023-09-12 02:27:30 +02:00
} catch (CheckoutApiException $e) {
2021-01-27 15:58:19 +01:00
$this->unWindGatewayFees($payment_hash);
2021-01-28 16:13:32 +01:00
2023-09-12 02:27:30 +02:00
$error_details = $e->error_details;
2023-09-12 02:27:30 +02:00
if (isset($error_details['error_codes']) ?? false) {
$error_details = end($e->error_details['error_codes']);
} else {
$error_details = $e->getMessage();
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2021-01-28 16:13:32 +01:00
$data = [
'status' => $e->error_details,
2021-01-28 16:13:32 +01:00
'error_type' => '',
'error_code' => $e->getCode(),
'param' => '',
2023-09-12 02:27:30 +02:00
'message' => $e->getMessage(),
2021-01-28 16:13:32 +01:00
];
2021-01-27 15:58:19 +01:00
2023-09-12 02:27:30 +02:00
$this->sendFailureMail($e->getMessage());
2021-10-17 12:40:40 +02:00
SystemLogger::dispatch(
$data,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_CHECKOUT,
$this->client,
2021-10-17 12:40:40 +02:00
$this->client->company
);
2021-01-27 15:58:19 +01:00
}
}
2021-08-04 16:24:44 +02:00
public function processWebhookRequest(PaymentWebhookRequest $request)
{
2023-07-21 13:01:22 +02:00
header('Content-Type: text/plain');
$webhook_payload = file_get_contents('php://input');
if($request->header('cko-signature') == hash_hmac('sha256', $webhook_payload, $this->company_gateway->company->company_key)) {
CheckoutWebhook::dispatch($request->all(), $request->company_key, $this->company_gateway->id)->delay(10);
2023-09-12 02:27:30 +02:00
} else {
2023-07-21 13:01:22 +02:00
nlog("Hash Mismatch = {$request->header('cko-signature')} ".hash_hmac('sha256', $webhook_payload, $this->company_gateway->company->company_key));
nlog($request->all());
}
return response()->json(['success' => true]);
}
public function process3dsConfirmation(Checkout3dsRequest $request)
{
$this->init();
$this->setPaymentHash($request->getPaymentHash());
//11-08-2022 check the user is authenticated
2022-08-12 03:25:10 +02:00
if (!Auth::guard('contact')->check()) {
$client = $request->getClient();
$this->client = $client;
2022-08-12 03:25:10 +02:00
auth()->guard('contact')->loginUsingId($client->contacts()->first()->id, true);
}
try {
2022-06-16 03:21:10 +02:00
$payment = $this->gateway->getPaymentsClient()->getPaymentDetails(
$request->query('cko-session-id')
);
2023-07-18 03:42:36 +02:00
nlog("checkout3ds");
nlog($payment);
if (isset($payment['approved']) && $payment['approved']) {
return $this->processSuccessfulPayment($payment);
} else {
return $this->processUnsuccessfulPayment($payment);
}
2022-06-16 03:21:10 +02:00
} catch (CheckoutApiException | Exception $e) {
nlog("checkout");
nlog($e->getMessage());
return $this->processInternallyFailedPayment($this, $e);
}
}
public function detach(ClientGatewayToken $clientGatewayToken)
{
// Gateway doesn't support this feature.
}
2024-03-15 04:21:42 +01:00
public function auth(): bool
{
2024-06-14 09:09:44 +02:00
try {
2024-03-15 04:21:42 +01:00
$this->init()->gateway->getCustomersClient('x');
return true;
2024-06-14 09:09:44 +02:00
} catch(\Exception $e) {
2024-03-15 04:21:42 +01:00
}
return false;
}
2024-03-18 03:50:12 +01:00
private function getToken(string $token, $gateway_customer_reference)
{
return ClientGatewayToken::query()
->where('company_id', $this->company_gateway->company_id)
->where('gateway_customer_reference', $gateway_customer_reference)
->where('token', $token)
->first();
}
2024-06-14 09:09:44 +02:00
2024-03-18 03:51:21 +01:00
/**
* ImportCustomers
*
2024-06-14 09:09:44 +02:00
* Only their methods because checkout.com
2024-03-18 03:51:21 +01:00
* does not have a list route for customers
2024-06-14 09:09:44 +02:00
*
2024-03-18 03:51:21 +01:00
* @return void
*/
2024-03-18 03:50:12 +01:00
public function importCustomers()
{
$this->init();
2024-06-14 09:09:44 +02:00
2024-03-18 03:50:12 +01:00
$this->company_gateway
->company
->clients()
->cursor()
2024-06-14 09:09:44 +02:00
->each(function ($client) {
if(!str_contains($client->present()->email(), "@")) {
return;
}
try {
$customer = $this->gateway->getCustomersClient()->get($client->present()->email());
} catch(\Exception $e) {
nlog("Checkout: Customer not found");
return;
}
$this->client = $client;
nlog($customer['instruments']);
foreach($customer['instruments'] as $card) {
if(
$card['type'] != 'card' ||
2024-07-14 13:28:54 +02:00
Carbon::createFromDate($card['expiry_year'], $card['expiry_month'], '1')->lt(now()) || //@phpstan-ignore-line
2024-06-14 09:09:44 +02:00
$this->getToken($card['id'], $customer['id'])
) {
continue;
}
$payment_meta = new \stdClass();
$payment_meta->exp_month = (string) $card['expiry_month'];
$payment_meta->exp_year = (string) $card['expiry_year'];
$payment_meta->brand = (string) $card['scheme'];
$payment_meta->last4 = (string) $card['last4'];
$payment_meta->type = (int) GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
'token' => $card['id'],
'payment_method_id' => GatewayType::CREDIT_CARD,
];
$this->storeGatewayToken($data, ['gateway_customer_reference' => $customer['id']]);
}
2024-03-18 03:50:12 +01:00
});
}
2024-09-04 09:30:53 +02:00
public function livewirePaymentView(array $data): string
{
return $this->payment_method->livewirePaymentView($data);
}
2020-06-10 17:38:10 +02:00
}