2023-07-21 13:01:22 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @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)
|
2023-07-21 13:01:22 +02:00
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\CheckoutCom;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2023-07-21 13:01:22 +02:00
|
|
|
use App\Libraries\MultiDB;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\CompanyGateway;
|
2023-07-21 13:01:22 +02:00
|
|
|
use App\Models\GatewayType;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\Payment;
|
2023-07-21 13:01:22 +02:00
|
|
|
use App\Models\PaymentHash;
|
|
|
|
use App\Models\PaymentType;
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Models\SystemLog;
|
2023-07-21 13:01:22 +02:00
|
|
|
use App\PaymentDrivers\Stripe\Utilities;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Illuminate\Bus\Queueable;
|
2023-07-21 13:01:22 +02:00
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
2023-10-26 04:57:44 +02:00
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2023-07-21 13:01:22 +02:00
|
|
|
|
|
|
|
class CheckoutWebhook implements ShouldQueue
|
|
|
|
{
|
2024-01-14 05:05:00 +01:00
|
|
|
use Dispatchable;
|
|
|
|
use InteractsWithQueue;
|
|
|
|
use Queueable;
|
|
|
|
use SerializesModels;
|
|
|
|
use Utilities;
|
2023-07-21 13:01:22 +02:00
|
|
|
|
|
|
|
public $tries = 1;
|
|
|
|
|
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
|
|
|
public CompanyGateway $company_gateway;
|
|
|
|
|
|
|
|
public function __construct(public array $webhook_array, public string $company_key, public int $company_gateway_id)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handle()
|
|
|
|
{
|
2024-06-16 11:16:42 +02:00
|
|
|
// nlog("Checkout Webhook");
|
2023-07-21 13:01:22 +02:00
|
|
|
|
|
|
|
MultiDB::findAndSetDbByCompanyKey($this->company_key);
|
|
|
|
|
|
|
|
$this->company_gateway = CompanyGateway::withTrashed()->find($this->company_gateway_id);
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if(!isset($this->webhook_array['type'])) {
|
2023-07-21 13:01:22 +02:00
|
|
|
nlog("Checkout Webhook type not set");
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2023-07-21 13:01:22 +02:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
match($this->webhook_array['type']) {
|
2023-07-21 13:01:22 +02:00
|
|
|
'payment_approved' => $this->paymentApproved(),
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {
|
|
|
|
* "id":"evt_dli6ty4qo5vuxle5wklf5gwbwy","type":"payment_approved","version":"1.0.33","created_on":"2023-07-21T10:03:07.1555904Z",
|
|
|
|
* "data":{"id":"pay_oqwbsd22kvpuvd35y5fhbdawxa","action_id":"act_buviezur7zsurnsorcgfn63e44","reference":"0014","amount":584168,"auth_code":"113059","currency":"USD","customer":{"id":"cus_6n4yt4q5kf4unn36o5qpbevxhe","email":"cypress@example.com"},
|
|
|
|
* "metadata":{"udf1":"Invoice Ninja","udf2":"ofhgiGjyQXbsbUwygURfYFT2C3E7iY7U"},"payment_type":"Regular","processed_on":"2023-07-21T10:02:57.4678165Z","processing":{"acquirer_transaction_id":"645272142084717830381","retrieval_reference_number":"183042259107"},"response_code":"10000","response_summary":"Approved","risk":{"flagged":false,"score":0},"3ds":{"version":"2.2.0","challenged":true,"challenge_indicator":"no_preference","exemption":"none","eci":"05","cavv":"AAABAVIREQAAAAAAAAAAAAAAAAA=","xid":"74afa3ac-25d3-4d95-b815-cefbdd7c8270","downgraded":false,"enrolled":"Y","authentication_response":"Y","flow_type":"challenged"},"scheme_id":"114455763095262",
|
2023-10-26 04:57:44 +02:00
|
|
|
* "source":{"id":"src_ghavmefpetjellmteqwj5jjcli","type":"card","billing_address":{},"expiry_month":10,"expiry_year":2025,"scheme":"VISA","last_4":"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"},"balances":{"total_authorized":584168,"total_voided":0,"available_to_void":584168,"total_captured":0,"available_to_capture":584168,"total_refunded":0,"available_to_refund":0},"event_links":{"payment":"https://api.sandbox.checkout.com/payments/pay_oqwbsd22kvpuvd35y5fhbdawxa","payment_actions":"https://api.sandbox.checkout.com/payments/pay_oqwbsd22kvpuvd35y5fhbdawxa/actions","capture":"https://api.sandbox.checkout.com/payments/pay_oqwbsd22kvpuvd35y5fhbdawxa/captures","void":"https://api.sandbox.checkout.com/payments/pay_oqwbsd22kvpuvd35y5fhbdawxa/voids"}},"_links":{"self":{"href":"https://api.sandbox.checkout.com/workflows/events/evt_dli6ty4qo5vuxle5wklf5gwbwy"},"subject":{"href":"https://api.sandbox.checkout.com/workflows/events/subject/pay_oqwbsd22kvpuvd35y5fhbdawxa"},"payment":{"href":"https://api.sandbox.checkout.com/payments/pay_oqwbsd22kvpuvd35y5fhbdawxa"},"payment_actions":{"href":"https://api.sandbox.checkout.com/payments/pay_oqwbsd22kvpuvd35y5fhbdawxa/actions"},"capture":{"href":"https://api.sandbox.checkout.com/payments/pay_oqwbsd22kvpuvd35y5fhbdawxa/captures"},"void":{"href":"https://api.sandbox.checkout.com/payments/pay_oqwbsd22kvpuvd35y5fhbdawxa/voids"}}}
|
2023-07-21 13:01:22 +02:00
|
|
|
*/
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-21 13:01:22 +02:00
|
|
|
private function paymentApproved()
|
|
|
|
{
|
|
|
|
$payment_object = $this->webhook_array['data'];
|
|
|
|
|
2023-08-15 14:47:54 +02:00
|
|
|
$payment = Payment::query()->withTrashed()->where('transaction_reference', $payment_object['id'])->first();
|
2023-07-21 13:01:22 +02:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($payment && $payment->status_id == Payment::STATUS_COMPLETED) {
|
2023-07-21 13:01:22 +02:00
|
|
|
return;
|
2023-10-26 04:57:44 +02:00
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
if($payment) {
|
2023-07-21 13:01:22 +02:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
|
|
|
$payment->save();
|
|
|
|
return;
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-21 13:01:22 +02:00
|
|
|
if(isset($this->webhook_array['metadata'])) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-07-21 13:01:22 +02:00
|
|
|
$metadata = $this->webhook_array['metadata'];
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-08-15 14:47:54 +02:00
|
|
|
$payment_hash = PaymentHash::query()->where('hash', $metadata['udf2'])->first();
|
2023-07-21 13:01:22 +02:00
|
|
|
|
|
|
|
$driver = $this->company_gateway->driver($payment_hash->fee_invoice->client)->init()->setPaymentMethod();
|
|
|
|
|
2023-08-15 16:23:18 +02:00
|
|
|
$payment_hash->data = array_merge((array) $payment_hash->data, $this->webhook_array); // @phpstan-ignore-line
|
2023-10-26 04:57:44 +02:00
|
|
|
$payment_hash->save();
|
|
|
|
$driver->setPaymentHash($payment_hash);
|
2023-07-21 13:01:22 +02:00
|
|
|
|
2023-08-15 16:23:18 +02:00
|
|
|
// @phpstan-ignore-line
|
2023-07-21 13:01:22 +02:00
|
|
|
$data = [
|
|
|
|
'payment_method' => isset($this->webhook_array['source']['id']) ? $this->webhook_array['source']['id'] : '',
|
|
|
|
'payment_type' => PaymentType::CREDIT_CARD_OTHER,
|
2023-08-15 16:23:18 +02:00
|
|
|
'amount' => $payment_hash->data->raw_value, // @phpstan-ignore-line
|
2023-07-21 13:01:22 +02:00
|
|
|
'transaction_reference' => $payment_object['id'],
|
|
|
|
'gateway_type_id' => GatewayType::CREDIT_CARD,
|
|
|
|
];
|
|
|
|
|
|
|
|
$payment = $driver->createPayment($data, \App\Models\Payment::STATUS_COMPLETED);
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $this->webhook_array, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_CHECKOUT,
|
|
|
|
$payment_hash->fee_invoice->client,
|
|
|
|
$this->company_gateway->company,
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|