Add checkout webhooks on create/update

This commit is contained in:
David Bomba 2023-07-21 21:58:04 +10:00
parent 2ed80ee678
commit 3e657a9886
3 changed files with 152 additions and 188 deletions

View File

@ -24,6 +24,7 @@ use App\Http\Requests\CompanyGateway\UpdateCompanyGatewayRequest;
use App\Jobs\Util\ApplePayDomain;
use App\Models\Client;
use App\Models\CompanyGateway;
use App\PaymentDrivers\CheckoutCom\CheckoutSetupWebhook;
use App\PaymentDrivers\Stripe\Jobs\StripeWebhook;
use App\Repositories\CompanyRepository;
use App\Transformers\CompanyGatewayTransformer;
@ -49,6 +50,8 @@ class CompanyGatewayController extends BaseController
private array $stripe_keys = ['d14dd26a47cecc30fdd65700bfb67b34', 'd14dd26a37cecc30fdd65700bfb55b23'];
private string $checkout_key = '3758e7f7c6f4cecf0f4f348b9a00f456';
/**
* CompanyGatewayController constructor.
* @param CompanyRepository $company_repo
@ -211,6 +214,9 @@ class CompanyGatewayController extends BaseController
if (in_array($company_gateway->gateway_key, $this->stripe_keys)) {
StripeWebhook::dispatch($company_gateway->company->company_key, $company_gateway->id);
}
elseif($company_gateway->gateway_key == $this->checkout_key) {
CheckoutSetupWebhook::dispatch($company_gateway->company->company_key, $company_gateway->id);
}
return $this->itemResponse($company_gateway);
}
@ -382,8 +388,10 @@ class CompanyGatewayController extends BaseController
$company_gateway->save();
// ApplePayDomain::dispatch($company_gateway, $company_gateway->company->db);
if($company_gateway->gateway_key == $this->checkout_key) {
CheckoutSetupWebhook::dispatch($company_gateway->company->company_key, $company_gateway->fresh()->id);
}
return $this->itemResponse($company_gateway);
}

View File

@ -0,0 +1,122 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\CheckoutCom;
use App\Models\Payment;
use App\Models\SystemLog;
use App\Libraries\MultiDB;
use App\Models\GatewayType;
use App\Models\PaymentHash;
use App\Models\PaymentType;
use Illuminate\Bus\Queueable;
use App\Models\CompanyGateway;
use App\Jobs\Util\SystemLogger;
use Checkout\CheckoutApiException;
use Illuminate\Queue\SerializesModels;
use App\PaymentDrivers\Stripe\Utilities;
use Illuminate\Queue\InteractsWithQueue;
use App\PaymentDrivers\CheckoutCom\Webhook;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Checkout\CheckoutAuthorizationException;
use Checkout\Workflows\CreateWorkflowRequest;
use App\PaymentDrivers\CheckoutComPaymentDriver;
use Checkout\Workflows\Actions\WebhookSignature;
use Checkout\Workflows\Actions\WebhookWorkflowActionRequest;
use Checkout\Workflows\Conditions\EventWorkflowConditionRequest;
class CheckoutSetupWebhook implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Utilities;
public $tries = 1;
public $deleteWhenMissingModels = true;
private string $authentication_webhook_name = 'Invoice_Ninja_3DS_Workflow';
public CheckoutComPaymentDriver $checkout;
public function __construct(private string $company_key, private int $company_gateway_id)
{
}
public function handle()
{
MultiDB::findAndSetDbByCompanyKey($this->company_key);
$company_gateway = CompanyGateway::find($this->company_gateway_id);
$this->checkout = $company_gateway->driver()->init();
$webhook = new Webhook($this->checkout);
$workflows = $webhook->getWorkFlows();
$wf = collect($workflows['data'])->first(function ($workflow) {
return $workflow['name'] == $this->authentication_webhook_name;
});
if($wf)
return;
$this->createAuthenticationWorkflow();
}
/**
* Creates an authentication workflow for 3DS
* and also a registration mechanism for payments that have been approved.
*
* @return void
*/
public function createAuthenticationWorkflow()
{
$signature = new WebhookSignature();
$signature->key = $this->checkout->company_gateway->company->company_key;
$signature->method = "HMACSHA256";
$actionRequest = new WebhookWorkflowActionRequest();
$actionRequest->url = $this->checkout->company_gateway->webhookUrl();
$actionRequest->signature = $signature;
$eventWorkflowConditionRequest = new EventWorkflowConditionRequest();
$eventWorkflowConditionRequest->events = [
"gateway" => ["payment_approved"],
"issuing" => ["authorization_approved","authorization_declined"],
];
$request = new CreateWorkflowRequest();
$request->actions = [$actionRequest];
$request->conditions = [$eventWorkflowConditionRequest];
$request->name = $this->authentication_webhook_name;
$request->active = true;
try {
$response = $this->checkout->gateway->getWorkflowsClient()->createWorkflow($request);
} catch (CheckoutApiException $e) {
// API error
$error_details = $e->error_details;
$http_status_code = isset($e->http_metadata) ? $e->http_metadata->getStatusCode() : null;
nlog("Checkout WEBHOOK creation error");
nlog($error_details);
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
}
}
}

View File

@ -12,197 +12,32 @@
namespace App\PaymentDrivers\CheckoutCom;
use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\Models\SystemLog;
use App\PaymentDrivers\CheckoutComPaymentDriver;
use App\PaymentDrivers\Common\MethodInterface;
use App\Utils\Traits\MakesHash;
use Checkout\CheckoutApiException;
use Checkout\CheckoutArgumentException;
use Checkout\CheckoutAuthorizationException;
use Checkout\Workflows\CreateWorkflowRequest;
use App\PaymentDrivers\Common\MethodInterface;
use App\PaymentDrivers\CheckoutComPaymentDriver;
use Checkout\Workflows\Actions\WebhookSignature;
use Checkout\Workflows\Actions\WebhookWorkflowActionRequest;
use Checkout\Workflows\Conditions\EventWorkflowConditionRequest;
use Checkout\Workflows\Conditions\EntityWorkflowConditionRequest;
use Checkout\Workflows\Conditions\ProcessingChannelWorkflowConditionRequest;
use Checkout\Payments\Four\Request\PaymentRequest;
use Checkout\Payments\Four\Request\Source\RequestTokenSource;
use Checkout\Payments\PaymentRequest as PaymentsPaymentRequest;
use Checkout\Payments\Source\RequestTokenSource as SourceRequestTokenSource;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\View\View;
class Webhook
{
/**
* @var CheckoutComPaymentDriver
*/
public $checkout;
private string $authentication_webhook_name = 'Invoice_Ninja_3DS_Workflow';
public function __construct(CheckoutComPaymentDriver $checkout)
public function __construct(public CheckoutComPaymentDriver $checkout)
{
$this->checkout = $checkout;
$this->checkout->init();
}
/*
'id' => 'pay_qw7rslcvacvubcvn6o5v7jp3ie',
'requested_on' => '2023-07-21T05:49:29.0799437Z',
'source' =>
array (
'id' => 'src_epivptv65yxungkhyqt6nayiai',
'type' => 'card',
'phone' =>
array (
),
'expiry_month' => 10,
'expiry_year' => 2025,
'scheme' => 'Visa',
'last4' => '4242',
'fingerprint' => 'BD864B08D0B098DD83052A038FD2BA967DF2D48E375AAEEF54E37BC36B385E9A',
'bin' => '424242',
'card_type' => 'CREDIT',
'card_category' => 'CONSUMER',
'issuer_country' => 'GB',
'product_id' => 'F',
'product_type' => 'Visa Classic',
'avs_check' => 'G',
'cvv_check' => 'Y',
'payment_account_reference' => 'V001726431013874807',
),
'expires_on' => '2023-08-20T05:50:08.7570835Z',
'items' =>
array (
),
'amount' => 44520,
'currency' => 'USD',
'payment_type' => 'Regular',
'reference' => '0024',
'status' => 'Captured',
'approved' => true,
'3ds' =>
array (
'downgraded' => false,
'enrolled' => 'Y',
'authentication_response' => 'Y',
'cryptogram' => 'AAABAVIREQAAAAAAAAAAAAAAAAA=',
'xid' => 'e1331818-b517-439e-b186-e22bf4efbf4b',
'version' => '2.2.0',
'exemption' => 'none',
'challenged' => true,
'exemption_applied' => 'none',
),
'balances' =>
array (
'total_authorized' => 44520,
'total_voided' => 0,
'available_to_void' => 0,
'total_captured' => 44520,
'available_to_capture' => 0,
'total_refunded' => 0,
'available_to_refund' => 44520,
),
'risk' =>
array (
'flagged' => false,
'score' => 0.0,
),
'customer' =>
array (
'id' => 'cus_aarus35jqd5uddkcxqfd5gwiii',
'email' => 'user@example.com',
'name' => 'GBP',
),
'metadata' =>
array (
'udf1' => 'Invoice Ninja',
'udf2' => 'JUdUiwMNTV1qfSstvC0ZvUJSQVJ65DDC',
),
'processing' =>
array (
'acquirer_transaction_id' => '767665093700479870728',
'retrieval_reference_number' => '787770870837',
'merchant_category_code' => '5815',
'scheme_merchant_id' => '55500',
'aft' => false,
'cko_network_token_available' => false,
),
'eci' => '05',
'scheme_id' => '420920321590206',
'actions' =>
array (
0 =>
array (
'id' => 'act_c3suhqtbmpjejltr6krvuknikm',
'type' => 'Capture',
'response_code' => '10000',
'response_summary' => 'Approved',
),
1 =>
array (
'id' => 'act_q4tjzvgsr2yu3cfzgrfn342wei',
'type' => 'Authorization',
'response_code' => '10000',
'response_summary' => 'Approved',
),
),
'_links' =>
array (
'self' =>
array (
'href' => 'https://api.sandbox.checkout.com/payments/pay_qw7rslcvacvubcvn6o5v7jp3ie',
),
'actions' =>
array (
'href' => 'https://api.sandbox.checkout.com/payments/pay_qw7rslcvacvubcvn6o5v7jp3ie/actions',
),
'refund' =>
array (
'href' => 'https://api.sandbox.checkout.com/payments/pay_qw7rslcvacvubcvn6o5v7jp3ie/refunds',
),
),
)
*/
/**
* Creates an authentication workflow for 3DS
* and also a registration mechanism for payments that have been approved.
*
* @return void
*/
public function createAuthenticationWorkflow()
{
$signature = new WebhookSignature();
$signature->key = $this->checkout->company_gateway->company->company_key;
$signature->method = "HMACSHA256";
$actionRequest = new WebhookWorkflowActionRequest();
$actionRequest->url = $this->checkout->company_gateway->webhookUrl();
$actionRequest->signature = $signature;
$eventWorkflowConditionRequest = new EventWorkflowConditionRequest();
$eventWorkflowConditionRequest->events = [
"gateway" => ["payment_approved"],
"issuing" => ["authorization_approved","authorization_declined"],
];
$request = new CreateWorkflowRequest();
$request->actions = [$actionRequest];
$request->conditions = [$eventWorkflowConditionRequest];
$request->name = $this->authentication_webhook_name;
$request->active = true;
try {
$response = $this->checkout->gateway->getWorkflowsClient()->createWorkflow($request);
nlog($response);
} catch (CheckoutApiException $e) {
// API error
$error_details = $e->error_details;
$http_status_code = isset($e->http_metadata) ? $e->http_metadata->getStatusCode() : null;
nlog($error_details);
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
}
}
/**
@ -215,7 +50,7 @@ class Webhook
try {
$response = $this->checkout->gateway->getWorkflowsClient()->getEventTypes();
nlog($response);
return $response;
} catch (CheckoutApiException $e) {
// API error
@ -238,9 +73,10 @@ class Webhook
{
try {
$response = $this->checkout->gateway->getWorkflowsClient()->getWorkflows();
nlog($response);
return $response;
} catch (CheckoutApiException $e) {
// API error
@ -250,8 +86,6 @@ class Webhook
// Bad Invalid authorization
}
}
}