2024-09-02 09:59:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Blockonomics;
|
|
|
|
|
|
|
|
use App\Models\Payment;
|
2024-09-04 04:50:08 +02:00
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\Models\GatewayType;
|
2024-09-06 06:02:18 +02:00
|
|
|
use App\Models\SystemLog;
|
2024-09-02 09:59:12 +02:00
|
|
|
use App\PaymentDrivers\BlockonomicsPaymentDriver;
|
|
|
|
use App\Utils\Traits\MakesHash;
|
|
|
|
use App\PaymentDrivers\Common\MethodInterface;
|
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
|
|
|
use App\Exceptions\PaymentFailed;
|
2024-09-06 06:02:18 +02:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2024-09-02 09:59:12 +02:00
|
|
|
use App\Jobs\Mail\PaymentFailureMailer;
|
2024-09-16 01:30:05 +02:00
|
|
|
use Illuminate\Support\Facades\Http;
|
2024-09-02 09:59:12 +02:00
|
|
|
|
|
|
|
class Blockonomics implements MethodInterface
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
2024-09-16 01:32:39 +02:00
|
|
|
public function __construct(public BlockonomicsPaymentDriver $blockonomics)
|
2024-09-02 09:59:12 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-09-02 10:24:29 +02:00
|
|
|
public function authorizeView($data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeRequest($request)
|
|
|
|
{
|
|
|
|
}
|
2024-09-16 01:32:39 +02:00
|
|
|
|
2024-09-02 10:24:29 +02:00
|
|
|
public function authorizeResponse($request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-09-05 07:29:16 +02:00
|
|
|
|
2024-09-16 01:32:39 +02:00
|
|
|
public function getBTCAddress(): string
|
2024-09-05 02:49:46 +02:00
|
|
|
{
|
2024-09-06 04:40:59 +02:00
|
|
|
$api_key = $this->blockonomics->api_key;
|
2024-09-19 00:04:24 +02:00
|
|
|
// $params = config('ninja.environment') == 'development' ? '?reset=1' : '';
|
|
|
|
$url = 'https://www.blockonomics.co/api/new_address';
|
2024-09-05 02:49:46 +02:00
|
|
|
|
2024-09-16 01:30:05 +02:00
|
|
|
$r = Http::withToken($api_key)
|
|
|
|
->post($url, []);
|
2024-09-05 02:49:46 +02:00
|
|
|
|
2024-09-16 01:30:05 +02:00
|
|
|
if($r->successful())
|
|
|
|
return $r->object()->address ?? 'Something went wrong';
|
2024-09-05 02:49:46 +02:00
|
|
|
|
2024-09-16 01:30:05 +02:00
|
|
|
return $r->object()->message ?? 'Something went wrong';
|
2024-09-05 02:49:46 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:39:32 +02:00
|
|
|
public function getBTCPrice()
|
|
|
|
{
|
2024-09-18 13:11:25 +02:00
|
|
|
|
|
|
|
$r = Http::get('https://www.blockonomics.co/api/price', ['currency' => $this->blockonomics->client->getCurrencyCode()]);
|
|
|
|
|
|
|
|
return $r->successful() ? $r->object()->price : 'Something went wrong';
|
|
|
|
|
2024-09-05 02:39:32 +02:00
|
|
|
}
|
|
|
|
|
2024-09-02 09:59:12 +02:00
|
|
|
public function paymentView($data)
|
|
|
|
{
|
2024-09-13 05:26:22 +02:00
|
|
|
$btc_price = $this->getBTCPrice();
|
|
|
|
$btc_address = $this->getBTCAddress();
|
|
|
|
$fiat_amount = $data['total']['amount_with_fee'];
|
|
|
|
$btc_amount = $fiat_amount / $btc_price;
|
2024-09-06 06:02:18 +02:00
|
|
|
$_invoice = collect($this->blockonomics->payment_hash->data->invoices)->first();
|
2024-09-06 04:39:31 +02:00
|
|
|
$data['gateway'] = $this->blockonomics;
|
2024-09-13 05:26:22 +02:00
|
|
|
$data['company_gateway_id'] = $this->blockonomics->getCompanyGatewayId();
|
|
|
|
$data['amount'] = $fiat_amount;
|
2024-09-06 04:39:31 +02:00
|
|
|
$data['currency'] = $this->blockonomics->client->getCurrencyCode();
|
2024-09-06 02:52:21 +02:00
|
|
|
$data['btc_amount'] = number_format($btc_amount, 10, '.', '');
|
|
|
|
$data['btc_address'] = $btc_address;
|
2024-09-11 10:07:43 +02:00
|
|
|
$data['btc_price'] = $btc_price;
|
2024-09-06 06:02:18 +02:00
|
|
|
$data['invoice_number'] = $_invoice->invoice_number;
|
2024-09-02 09:59:12 +02:00
|
|
|
return render('gateways.blockonomics.pay', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
|
|
{
|
|
|
|
$request->validate([
|
|
|
|
'payment_hash' => ['required'],
|
|
|
|
'amount' => ['required'],
|
|
|
|
'currency' => ['required'],
|
2024-09-13 05:26:22 +02:00
|
|
|
'txid' => ['required'],
|
|
|
|
'payment_method_id' => ['required'],
|
2024-09-02 09:59:12 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
2024-09-06 06:02:18 +02:00
|
|
|
$data = [];
|
2024-09-18 13:11:25 +02:00
|
|
|
$fiat_amount = round(($request->btc_price * $request->btc_amount), 2);
|
2024-09-13 05:26:22 +02:00
|
|
|
$data['amount'] = $fiat_amount;
|
|
|
|
$data['currency'] = $request->currency;
|
2024-09-06 06:02:18 +02:00
|
|
|
$data['payment_method_id'] = $request->payment_method_id;
|
|
|
|
$data['payment_type'] = PaymentType::CRYPTO;
|
|
|
|
$data['gateway_type_id'] = GatewayType::CRYPTO;
|
2024-09-18 09:12:52 +02:00
|
|
|
$data['transaction_reference'] = $request->txid;
|
2024-09-06 06:02:18 +02:00
|
|
|
|
2024-09-08 13:24:13 +02:00
|
|
|
$statusId = Payment::STATUS_PENDING;
|
|
|
|
$payment = $this->blockonomics->createPayment($data, $statusId);
|
2024-09-18 09:12:52 +02:00
|
|
|
|
2024-09-06 06:02:18 +02:00
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $payment, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_BLOCKONOMICS,
|
|
|
|
$this->blockonomics->client,
|
|
|
|
$this->blockonomics->client->company,
|
|
|
|
);
|
2024-09-16 01:32:39 +02:00
|
|
|
|
|
|
|
return redirect()->route('client.payments.show', ['payment' => $payment->hashed_id]);
|
2024-09-02 09:59:12 +02:00
|
|
|
|
|
|
|
} catch (\Throwable $e) {
|
2024-09-06 04:39:31 +02:00
|
|
|
$blockonomics = $this->blockonomics;
|
|
|
|
PaymentFailureMailer::dispatch($blockonomics->client, $blockonomics->payment_hash->data, $blockonomics->client->company, $request->amount);
|
2024-09-02 09:59:12 +02:00
|
|
|
throw new PaymentFailed('Error during Blockonomics payment : ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-03 12:48:59 +02:00
|
|
|
// Not supported yet
|
2024-09-02 09:59:12 +02:00
|
|
|
public function refund(Payment $payment, $amount)
|
|
|
|
{
|
2024-09-18 23:45:00 +02:00
|
|
|
return;
|
2024-09-02 09:59:12 +02:00
|
|
|
}
|
|
|
|
}
|