2024-05-13 18:33:15 +02:00
|
|
|
<?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\BTCPay;
|
|
|
|
|
|
|
|
use App\Models\Payment;
|
|
|
|
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;
|
2024-06-07 13:30:11 +02:00
|
|
|
use App\Jobs\Mail\PaymentFailureMailer;
|
2024-05-30 15:34:43 +02:00
|
|
|
use Illuminate\Mail\Mailables\Address;
|
|
|
|
use App\Services\Email\EmailObject;
|
|
|
|
use App\Services\Email\Email;
|
|
|
|
use Illuminate\Support\Facades\App;
|
2024-05-13 18:33:15 +02:00
|
|
|
|
|
|
|
class BTCPay implements MethodInterface
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
|
|
|
public $driver_class;
|
|
|
|
|
|
|
|
public function __construct(BTCPayPaymentDriver $driver_class)
|
|
|
|
{
|
|
|
|
$this->driver_class = $driver_class;
|
|
|
|
$this->driver_class->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeView($data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeRequest($request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
public function authorizeResponse($request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentView($data)
|
|
|
|
{
|
|
|
|
$data['gateway'] = $this->driver_class;
|
|
|
|
$data['amount'] = $data['total']['amount_with_fee'];
|
|
|
|
$data['currency'] = $this->driver_class->client->getCurrencyCode();
|
|
|
|
|
|
|
|
return render('gateways.btcpay.pay', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
|
|
{
|
|
|
|
|
|
|
|
$request->validate([
|
|
|
|
'payment_hash' => ['required'],
|
|
|
|
'amount' => ['required'],
|
|
|
|
'currency' => ['required'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$drv = $this->driver_class;
|
|
|
|
if (
|
|
|
|
strlen($drv->btcpay_url) < 1
|
|
|
|
|| strlen($drv->api_key) < 1
|
|
|
|
|| strlen($drv->store_id) < 1
|
|
|
|
|| strlen($drv->webhook_secret) < 1
|
|
|
|
) {
|
|
|
|
throw new PaymentFailed('BTCPay is not well configured');
|
|
|
|
}
|
|
|
|
if (!filter_var($this->driver_class->btcpay_url, FILTER_VALIDATE_URL)) {
|
|
|
|
throw new PaymentFailed('Wrong format for BTCPay Url');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$_invoice = collect($drv->payment_hash->data->invoices)->first();
|
|
|
|
$cli = $drv->client;
|
|
|
|
|
|
|
|
$metaData = [
|
|
|
|
'buyerName' => $cli->name,
|
|
|
|
'buyerAddress1' => $cli->address1,
|
|
|
|
'buyerAddress2' => $cli->address2,
|
|
|
|
'buyerCity' => $cli->city,
|
|
|
|
'buyerState' => $cli->state,
|
|
|
|
'buyerZip' => $cli->postal_code,
|
|
|
|
'buyerCountry' => $cli->country_id,
|
|
|
|
'buyerPhone' => $cli->phone,
|
|
|
|
'itemDesc' => "From InvoiceNinja",
|
2024-06-07 13:30:11 +02:00
|
|
|
'InvoiceNinjaPaymentHash' => $drv->payment_hash->hash
|
2024-05-13 18:33:15 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
|
2024-06-07 13:30:11 +02:00
|
|
|
$urlRedirect = redirect()->route('client.invoice.show', ['invoice' => $_invoice->invoice_id])->getTargetUrl();
|
2024-05-13 18:33:15 +02:00
|
|
|
$checkoutOptions = new \BTCPayServer\Client\InvoiceCheckoutOptions();
|
|
|
|
$checkoutOptions->setRedirectURL($urlRedirect);
|
|
|
|
|
|
|
|
$client = new \BTCPayServer\Client\Invoice($drv->btcpay_url, $drv->api_key);
|
|
|
|
$rep = $client->createInvoice(
|
|
|
|
$drv->store_id,
|
|
|
|
$request->currency,
|
|
|
|
\BTCPayServer\Util\PreciseNumber::parseString($request->amount),
|
|
|
|
$_invoice->invoice_number,
|
|
|
|
$cli->present()->email(),
|
|
|
|
$metaData,
|
|
|
|
$checkoutOptions
|
|
|
|
);
|
2024-06-07 13:30:11 +02:00
|
|
|
//$payment->transaction_reference = $rep->getId();
|
|
|
|
// $payment->save();
|
2024-05-13 18:33:15 +02:00
|
|
|
|
|
|
|
return redirect($rep->getCheckoutLink());
|
|
|
|
} catch (\Throwable $e) {
|
2024-06-07 13:30:11 +02:00
|
|
|
PaymentFailureMailer::dispatch($drv->client, $drv->payment_hash->data, $drv->client->company, $request->amount);
|
2024-05-13 18:33:15 +02:00
|
|
|
throw new PaymentFailed('Error during BTCPay payment : ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function refund(Payment $payment, $amount)
|
|
|
|
{
|
|
|
|
try {
|
2024-05-30 15:34:43 +02:00
|
|
|
if ($amount == $payment->amount) {
|
|
|
|
$refundVariant = "Fiat";
|
|
|
|
$refundPaymentMethod = "BTC";
|
|
|
|
$refundDescription = "Full refund";
|
|
|
|
$refundCustomCurrency = null;
|
|
|
|
$refundCustomAmount = null;
|
|
|
|
} else {
|
|
|
|
$refundVariant = "Custom";
|
|
|
|
$refundPaymentMethod = "";
|
|
|
|
$refundDescription = "Partial refund";
|
|
|
|
$refundCustomCurrency = $payment->currency;
|
|
|
|
$refundCustomAmount = $amount;
|
|
|
|
}
|
2024-05-13 18:33:15 +02:00
|
|
|
|
|
|
|
$client = new \BTCPayServer\Client\Invoice($this->driver_class->btcpay_url, $this->driver_class->api_key);
|
2024-05-30 15:34:43 +02:00
|
|
|
$refund = $client->refundInvoice(
|
|
|
|
$this->driver_class->store_id,
|
|
|
|
$payment->transaction_reference,
|
|
|
|
$refundVariant,
|
|
|
|
$refundPaymentMethod,
|
|
|
|
null,
|
|
|
|
$refundDescription,
|
|
|
|
0,
|
|
|
|
$refundCustomAmount,
|
|
|
|
$refundCustomCurrency
|
|
|
|
);
|
|
|
|
App::setLocale($payment->company->getLocale());
|
|
|
|
|
|
|
|
$mo = new EmailObject();
|
|
|
|
$mo->subject = ctrans('texts.btcpay_refund_subject');
|
|
|
|
$mo->body = ctrans('texts.btcpay_refund_body') . '<br>' . $refund->getViewLink();
|
|
|
|
$mo->text_body = ctrans('texts.btcpay_refund_body') . '\n' . $refund->getViewLink();
|
|
|
|
$mo->company_key = $payment->company->company_key;
|
|
|
|
$mo->html_template = 'email.template.generic';
|
|
|
|
$mo->to = [new Address($payment->client->present()->email(), $payment->client->present()->name())];
|
|
|
|
$mo->email_template_body = 'btcpay_refund_subject';
|
|
|
|
$mo->email_template_subject = 'btcpay_refund_body';
|
|
|
|
|
|
|
|
Email::dispatch($mo, $payment->company);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'transaction_reference' => $refund->getId(),
|
|
|
|
'transaction_response' => json_encode($refund),
|
|
|
|
'success' => true,
|
|
|
|
'description' => "Please follow this link to claim your refund: " . $refund->getViewLink(),
|
|
|
|
'code' => 202,
|
|
|
|
];
|
2024-05-13 18:33:15 +02:00
|
|
|
|
2024-05-30 15:34:43 +02:00
|
|
|
return $data;
|
2024-05-13 18:33:15 +02:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
throw new PaymentFailed('Error during BTCPay refund : ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|