Merge pull request #9605 from Nisaba/v5-develop

BTCPay updates
This commit is contained in:
David Bomba 2024-06-08 08:21:46 +10:00 committed by GitHub
commit 7cce901bc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 25 deletions

View File

@ -12,14 +12,13 @@
namespace App\PaymentDrivers\BTCPay; namespace App\PaymentDrivers\BTCPay;
use App\Models\GatewayType;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentType;
use App\PaymentDrivers\BTCPayPaymentDriver; use App\PaymentDrivers\BTCPayPaymentDriver;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\PaymentDrivers\Common\MethodInterface; use App\PaymentDrivers\Common\MethodInterface;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Exceptions\PaymentFailed; use App\Exceptions\PaymentFailed;
use App\Jobs\Mail\PaymentFailureMailer;
use Illuminate\Mail\Mailables\Address; use Illuminate\Mail\Mailables\Address;
use App\Services\Email\EmailObject; use App\Services\Email\EmailObject;
use App\Services\Email\Email; use App\Services\Email\Email;
@ -83,15 +82,6 @@ class BTCPay implements MethodInterface
$_invoice = collect($drv->payment_hash->data->invoices)->first(); $_invoice = collect($drv->payment_hash->data->invoices)->first();
$cli = $drv->client; $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 = [ $metaData = [
'buyerName' => $cli->name, 'buyerName' => $cli->name,
'buyerAddress1' => $cli->address1, 'buyerAddress1' => $cli->address1,
@ -102,11 +92,11 @@ class BTCPay implements MethodInterface
'buyerCountry' => $cli->country_id, 'buyerCountry' => $cli->country_id,
'buyerPhone' => $cli->phone, 'buyerPhone' => $cli->phone,
'itemDesc' => "From InvoiceNinja", '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 = new \BTCPayServer\Client\InvoiceCheckoutOptions();
$checkoutOptions->setRedirectURL($urlRedirect); $checkoutOptions->setRedirectURL($urlRedirect);
@ -120,11 +110,12 @@ class BTCPay implements MethodInterface
$metaData, $metaData,
$checkoutOptions $checkoutOptions
); );
$payment->transaction_reference = $rep->getId(); //$payment->transaction_reference = $rep->getId();
$payment->save(); // $payment->save();
return redirect($rep->getCheckoutLink()); return redirect($rep->getCheckoutLink());
} catch (\Throwable $e) { } 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()); throw new PaymentFailed('Error during BTCPay payment : ' . $e->getMessage());
} }
} }

View File

@ -13,13 +13,17 @@
namespace App\PaymentDrivers; namespace App\PaymentDrivers;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Models\PaymentHash;
use App\Models\GatewayType; use App\Models\GatewayType;
use App\PaymentDrivers\BTCPay\BTCPay; use App\PaymentDrivers\BTCPay\BTCPay;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Models\Payment; use App\Models\Payment;
use App\Models\Client;
use App\Exceptions\PaymentFailed; use App\Exceptions\PaymentFailed;
use App\Models\PaymentType;
use BTCPayServer\Client\Webhook; use BTCPayServer\Client\Webhook;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Models\Invoice;
class BTCPayPaymentDriver extends BaseDriver class BTCPayPaymentDriver extends BaseDriver
{ {
@ -87,8 +91,7 @@ class BTCPayPaymentDriver extends BaseDriver
public function processWebhookRequest() public function processWebhookRequest()
{ {
$webhook_payload = file_get_contents('php://input'); $webhook_payload = file_get_contents('php://input');
$sig = false;
/** @var \stdClass $btcpayRep */
$btcpayRep = json_decode($webhook_payload); $btcpayRep = json_decode($webhook_payload);
if ($btcpayRep == null) { if ($btcpayRep == null) {
throw new PaymentFailed('Empty data'); throw new PaymentFailed('Empty data');
@ -98,7 +101,11 @@ class BTCPayPaymentDriver extends BaseDriver
'Invalid BTCPayServer payment notification- did not receive invoice ID.' '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; return;
} }
@ -118,21 +125,41 @@ class BTCPayPaymentDriver extends BaseDriver
); );
} }
/** @var \App\Models\Payment $payment **/ $this->setPaymentMethod(GatewayType::CRYPTO);
$payment = Payment::find($btcpayRep->metafata->paymentID); $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) { switch ($btcpayRep->type) {
case "InvoiceExpired": case "InvoiceExpired":
$payment->status_id = Payment::STATUS_CANCELLED; $StatusId = Payment::STATUS_CANCELLED;
break; break;
case "InvoiceInvalid": case "InvoiceInvalid":
$payment->status_id = Payment::STATUS_FAILED; $StatusId = Payment::STATUS_FAILED;
break; break;
case "InvoiceSettled": case "InvoiceSettled":
$payment->status_id = Payment::STATUS_COMPLETED; $StatusId = Payment::STATUS_COMPLETED;
break; break;
} }
if ($payment->status_id != $StatusId) {
$payment->status_id = $StatusId;
$payment->save(); $payment->save();
} }
}
public function refund(Payment $payment, $amount, $return_client_response = false) public function refund(Payment $payment, $amount, $return_client_response = false)