diff --git a/app/PaymentDrivers/BTCPay/BTCPay.php b/app/PaymentDrivers/BTCPay/BTCPay.php index 708018b30a46..5989bb21296d 100644 --- a/app/PaymentDrivers/BTCPay/BTCPay.php +++ b/app/PaymentDrivers/BTCPay/BTCPay.php @@ -20,6 +20,10 @@ use App\Utils\Traits\MakesHash; use App\PaymentDrivers\Common\MethodInterface; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Exceptions\PaymentFailed; +use Illuminate\Mail\Mailables\Address; +use App\Services\Email\EmailObject; +use App\Services\Email\Email; +use Illuminate\Support\Facades\App; class BTCPay implements MethodInterface { @@ -128,18 +132,55 @@ class BTCPay implements MethodInterface public function refund(Payment $payment, $amount) { try { - $invoice = $payment->invoices()->first(); - $isPartialRefund = ($amount < $payment->amount); + 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; + } $client = new \BTCPayServer\Client\Invoice($this->driver_class->btcpay_url, $this->driver_class->api_key); - $refund = $client->refundInvoice($this->driver_class->store_id, $payment->transaction_reference); + $refund = $client->refundInvoice( + $this->driver_class->store_id, + $payment->transaction_reference, + $refundVariant, + $refundPaymentMethod, + null, + $refundDescription, + 0, + $refundCustomAmount, + $refundCustomCurrency + ); + App::setLocale($payment->company->getLocale()); - /* $data = []; - $data['InvoiceNumber'] = $invoice->number; - $data['isPartialRefund'] = $isPartialRefund; - $data['BTCPayLink'] = $refund->getViewLink();*/ + $mo = new EmailObject(); + $mo->subject = ctrans('texts.btcpay_refund_subject'); + $mo->body = ctrans('texts.btcpay_refund_body') . '
' . $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'; - return $refund->getViewLink(); + 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, + ]; + + return $data; } catch (\Throwable $e) { throw new PaymentFailed('Error during BTCPay refund : ' . $e->getMessage()); } diff --git a/app/PaymentDrivers/BTCPayPaymentDriver.php b/app/PaymentDrivers/BTCPayPaymentDriver.php index 4ede193c149b..368f5b7beb0b 100644 --- a/app/PaymentDrivers/BTCPayPaymentDriver.php +++ b/app/PaymentDrivers/BTCPayPaymentDriver.php @@ -79,10 +79,14 @@ class BTCPayPaymentDriver extends BaseDriver return $this->payment_method->paymentView($data); //this is your custom implementation from here } + public function processPaymentResponse($request) + { + return $this->payment_method->paymentResponse($request); + } + public function processWebhookRequest() { $webhook_payload = file_get_contents('php://input'); - //file_put_contents("/home/claude/invoiceninja/storage/my.log", $webhook_payload); $btcpayRep = json_decode($webhook_payload); if ($btcpayRep == null) { diff --git a/lang/en/texts.php b/lang/en/texts.php index 2be8de976f42..003509f2c939 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -5330,9 +5330,10 @@ $lang = array( 'disconnected' => 'Disconnected', 'reconnect' => 'Reconnect', 'e_invoice_settings' => 'E-Invoice Settings', + 'btcpay_refund_subject' => 'Refund of your invoice via BTCPay', + 'btcpay_refund_body' => 'A refund intended for you has been issued. To claim it via BTCPay, please click on this link:', 'currency_mauritanian_ouguiya' => 'Mauritanian Ouguiya', 'currency_bhutan_ngultrum' => 'Bhutan Ngultrum', - ); -return $lang; +return $lang; \ No newline at end of file