Square upgrades

This commit is contained in:
David Bomba 2023-08-19 10:14:46 +10:00
parent c0acc7a15e
commit cfde7414d7
2 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,59 @@
<?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\Square;
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 Illuminate\Queue\SerializesModels;
use App\PaymentDrivers\Stripe\Utilities;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SquareWebhook implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Utilities;
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()
{
nlog("Square Webhook");
MultiDB::findAndSetDbByCompanyKey($this->company_key);
$this->company_gateway = CompanyGateway::withTrashed()->find($this->company_gateway_id);
// if(!isset($this->webhook_array['type']))
// nlog("Checkout Webhook type not set");
// match($this->webhook_array['type']){
// 'payment_approved' => $this->paymentApproved(),
// };
}
}

View File

@ -177,8 +177,46 @@ class SquarePaymentDriver extends BaseDriver
return false; return false;
} }
public function createWebhooks()
{
$this->init();
$event_types = ['payment.created', 'payment.updated'];
$subscription = new \Square\Models\WebhookSubscription();
$subscription->setName('Invoice Ninja Webhook Subscription');
$subscription->setEventTypes($event_types);
$subscription->setNotificationUrl($this->company_gateway->webhookUrl());
// $subscription->setApiVersion('2021-12-15');
$body = new \Square\Models\CreateWebhookSubscriptionRequest($subscription);
$body->setIdempotencyKey(\Illuminate\Support\Str::uuid());
$api_response = $this->square->getWebhookSubscriptionsApi()->createWebhookSubscription($body);
if ($api_response->isSuccess()) {
$result = $api_response->getResult();
} else {
$errors = $api_response->getErrors();
}
}
public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null) public function processWebhookRequest(PaymentWebhookRequest $request, Payment $payment = null)
{ {
// 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);
// } else {
// 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 convertAmount($amount) public function convertAmount($amount)