From 665e1eabfa9aceac822afeae608fb7eb51475580 Mon Sep 17 00:00:00 2001 From: Nisaba <34550856+Nisaba@users.noreply.github.com> Date: Fri, 7 Jun 2024 11:30:11 +0000 Subject: [PATCH] BTCPay updates Signed-off-by: Nisaba <34550856+Nisaba@users.noreply.github.com> --- app/PaymentDrivers/BTCPay/BTCPay.php | 21 +++------- app/PaymentDrivers/BTCPayPaymentDriver.php | 47 +++++++++++++++++----- 2 files changed, 43 insertions(+), 25 deletions(-) diff --git a/app/PaymentDrivers/BTCPay/BTCPay.php b/app/PaymentDrivers/BTCPay/BTCPay.php index 5989bb21296d..8b5b3a07aff8 100644 --- a/app/PaymentDrivers/BTCPay/BTCPay.php +++ b/app/PaymentDrivers/BTCPay/BTCPay.php @@ -12,14 +12,13 @@ namespace App\PaymentDrivers\BTCPay; -use App\Models\GatewayType; use App\Models\Payment; -use App\Models\PaymentType; use App\PaymentDrivers\BTCPayPaymentDriver; use App\Utils\Traits\MakesHash; use App\PaymentDrivers\Common\MethodInterface; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Exceptions\PaymentFailed; +use App\Jobs\Mail\PaymentFailureMailer; use Illuminate\Mail\Mailables\Address; use App\Services\Email\EmailObject; use App\Services\Email\Email; @@ -83,15 +82,6 @@ class BTCPay implements MethodInterface $_invoice = collect($drv->payment_hash->data->invoices)->first(); $cli = $drv->client; - $dataPayment = [ - 'payment_method' => $drv->payment_method, - 'payment_type' => PaymentType::CRYPTO, - 'amount' => $request->amount, - 'gateway_type_id' => GatewayType::CRYPTO, - 'transaction_reference' => 'xxx' - ]; - $payment = $drv->createPayment($dataPayment, \App\Models\Payment::STATUS_PENDING); - $metaData = [ 'buyerName' => $cli->name, 'buyerAddress1' => $cli->address1, @@ -102,11 +92,11 @@ class BTCPay implements MethodInterface 'buyerCountry' => $cli->country_id, 'buyerPhone' => $cli->phone, 'itemDesc' => "From InvoiceNinja", - 'paymentID' => $payment->id + 'InvoiceNinjaPaymentHash' => $drv->payment_hash->hash ]; - $urlRedirect = redirect()->route('client.payments.show', ['payment' => $payment->hashed_id])->getTargetUrl(); + $urlRedirect = redirect()->route('client.invoice.show', ['invoice' => $_invoice->invoice_id])->getTargetUrl(); $checkoutOptions = new \BTCPayServer\Client\InvoiceCheckoutOptions(); $checkoutOptions->setRedirectURL($urlRedirect); @@ -120,11 +110,12 @@ class BTCPay implements MethodInterface $metaData, $checkoutOptions ); - $payment->transaction_reference = $rep->getId(); - $payment->save(); + //$payment->transaction_reference = $rep->getId(); + // $payment->save(); return redirect($rep->getCheckoutLink()); } catch (\Throwable $e) { + PaymentFailureMailer::dispatch($drv->client, $drv->payment_hash->data, $drv->client->company, $request->amount); throw new PaymentFailed('Error during BTCPay payment : ' . $e->getMessage()); } } diff --git a/app/PaymentDrivers/BTCPayPaymentDriver.php b/app/PaymentDrivers/BTCPayPaymentDriver.php index e7560cec2784..755116526703 100644 --- a/app/PaymentDrivers/BTCPayPaymentDriver.php +++ b/app/PaymentDrivers/BTCPayPaymentDriver.php @@ -13,13 +13,17 @@ namespace App\PaymentDrivers; use App\Utils\Traits\MakesHash; +use App\Models\PaymentHash; use App\Models\GatewayType; use App\PaymentDrivers\BTCPay\BTCPay; use App\Models\SystemLog; use App\Models\Payment; +use App\Models\Client; use App\Exceptions\PaymentFailed; - +use App\Models\PaymentType; use BTCPayServer\Client\Webhook; +use App\Http\Requests\Payments\PaymentWebhookRequest; +use App\Models\Invoice; class BTCPayPaymentDriver extends BaseDriver { @@ -87,8 +91,7 @@ class BTCPayPaymentDriver extends BaseDriver public function processWebhookRequest() { $webhook_payload = file_get_contents('php://input'); - $sig = false; - /** @var \stdClass $btcpayRep */ + $btcpayRep = json_decode($webhook_payload); if ($btcpayRep == null) { throw new PaymentFailed('Empty data'); @@ -98,7 +101,11 @@ class BTCPayPaymentDriver extends BaseDriver 'Invalid BTCPayServer payment notification- did not receive invoice ID.' ); } - if (str_starts_with($btcpayRep->invoiceId, "__test__") || $btcpayRep->type == "InvoiceCreated") { + if ( + str_starts_with($btcpayRep->invoiceId, "__test__") + || $btcpayRep->type == "InvoiceProcessing" + || $btcpayRep->type == "InvoiceCreated" + ) { return; } @@ -118,20 +125,40 @@ class BTCPayPaymentDriver extends BaseDriver ); } - /** @var \App\Models\Payment $payment **/ - $payment = Payment::find($btcpayRep->metafata->paymentID); + $this->setPaymentMethod(GatewayType::CRYPTO); + $this->payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$btcpayRep->metadata->InvoiceNinjaPaymentHash])->firstOrFail(); + $StatusId = Payment::STATUS_PENDING; + if ($this->payment_hash->payment_id == null) { + //$_invoice = collect($this->payment_hash->data->invoices)->first(); + $_invoice = Invoice::query()->where('number', $btcpayRep->metadata->orderId)->first(); + $this->client = Client::find($_invoice->client_id); + $dataPayment = [ + 'payment_method' => $this->payment_method, + 'payment_type' => PaymentType::CRYPTO, + 'amount' => $_invoice->amount, + 'gateway_type_id' => GatewayType::CRYPTO, + 'transaction_reference' => $btcpayRep->invoiceId + ]; + $payment = $this->createPayment($dataPayment, $StatusId); + } else { + $payment = Payment::find($this->payment_hash->payment_id); + $StatusId = $payment->status_id; + } switch ($btcpayRep->type) { case "InvoiceExpired": - $payment->status_id = Payment::STATUS_CANCELLED; + $StatusId = Payment::STATUS_CANCELLED; break; case "InvoiceInvalid": - $payment->status_id = Payment::STATUS_FAILED; + $StatusId = Payment::STATUS_FAILED; break; case "InvoiceSettled": - $payment->status_id = Payment::STATUS_COMPLETED; + $StatusId = Payment::STATUS_COMPLETED; break; } - $payment->save(); + if ($payment->status_id != $StatusId) { + $payment->status_id = $StatusId; + $payment->save(); + } }