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;
|
|
|
|
use Illuminate\Mail\Mailables\Address;
|
|
|
|
use App\Services\Email\EmailObject;
|
|
|
|
use App\Services\Email\Email;
|
|
|
|
use Illuminate\Support\Facades\App;
|
2024-09-05 02:39:32 +02:00
|
|
|
use App\Models\Invoice;
|
2024-09-02 09:59:12 +02:00
|
|
|
|
|
|
|
class Blockonomics implements MethodInterface
|
|
|
|
{
|
|
|
|
use MakesHash;
|
|
|
|
|
2024-09-11 03:03:51 +02:00
|
|
|
public $driver_class;
|
2024-09-02 09:59:12 +02:00
|
|
|
|
|
|
|
public function __construct(BlockonomicsPaymentDriver $driver_class)
|
|
|
|
{
|
2024-09-06 04:39:31 +02:00
|
|
|
$this->blockonomics = $driver_class;
|
|
|
|
$this->blockonomics->init();
|
2024-09-02 09:59:12 +02:00
|
|
|
}
|
|
|
|
|
2024-09-02 10:24:29 +02:00
|
|
|
public function authorizeView($data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeRequest($request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
public function authorizeResponse($request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-09-05 07:29:16 +02:00
|
|
|
|
2024-09-05 02:49:46 +02:00
|
|
|
public function getBTCAddress()
|
|
|
|
{
|
2024-09-06 04:40:59 +02:00
|
|
|
$api_key = $this->blockonomics->api_key;
|
2024-09-05 07:29:16 +02:00
|
|
|
// TODO: remove ?reset=1 before marking PR as ready
|
|
|
|
$url = 'https://www.blockonomics.co/api/new_address?reset=1';
|
2024-09-05 02:49:46 +02:00
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
|
|
|
|
|
|
|
|
$header = "Authorization: Bearer " . $api_key;
|
|
|
|
$headers = array();
|
|
|
|
$headers[] = $header;
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
|
|
|
|
$contents = curl_exec($ch);
|
|
|
|
if (curl_errno($ch)) {
|
|
|
|
echo "Error:" . curl_error($ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
$responseObj = json_decode($contents);
|
|
|
|
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
|
|
curl_close ($ch);
|
|
|
|
|
|
|
|
if ($status == 200) {
|
|
|
|
return $responseObj->address;
|
|
|
|
} else {
|
|
|
|
echo "ERROR: " . $status . ' ' . $responseObj->message;
|
|
|
|
}
|
|
|
|
return "Something went wrong";
|
|
|
|
}
|
|
|
|
|
2024-09-05 03:06:44 +02:00
|
|
|
public function getTenMinutesCountDownEndTime()
|
|
|
|
{
|
|
|
|
$duration_in_sec = 10 * 60; // 10 minutes in seconds
|
|
|
|
$current_time = time();
|
|
|
|
return $current_time + $duration_in_sec;
|
|
|
|
}
|
|
|
|
|
2024-09-05 02:39:32 +02:00
|
|
|
public function getBTCPrice()
|
|
|
|
{
|
2024-09-06 04:39:31 +02:00
|
|
|
$currency_code = $this->blockonomics->client->getCurrencyCode();
|
2024-09-05 02:39:32 +02:00
|
|
|
$BLOCKONOMICS_BASE_URL = 'https://www.blockonomics.co';
|
|
|
|
$BLOCKONOMICS_PRICE_URL = $BLOCKONOMICS_BASE_URL . '/api/price?currency=';
|
|
|
|
$response = file_get_contents($BLOCKONOMICS_PRICE_URL . $currency_code);
|
|
|
|
$data = json_decode($response, true);
|
|
|
|
// TODO: handle error
|
|
|
|
return $data['price'];
|
|
|
|
}
|
|
|
|
|
2024-09-02 09:59:12 +02:00
|
|
|
public function paymentView($data)
|
|
|
|
{
|
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-02 09:59:12 +02:00
|
|
|
$data['amount'] = $data['total']['amount_with_fee'];
|
2024-09-06 04:39:31 +02:00
|
|
|
$data['currency'] = $this->blockonomics->client->getCurrencyCode();
|
2024-09-05 02:39:32 +02:00
|
|
|
$btc_amount = $data['amount'] / $this->getBTCPrice();
|
2024-09-06 02:52:21 +02:00
|
|
|
$data['btc_amount'] = number_format($btc_amount, 10, '.', '');
|
|
|
|
$btc_address = $this->getBTCAddress();
|
|
|
|
$data['btc_address'] = $btc_address;
|
2024-09-06 06:02:18 +02:00
|
|
|
$data['invoice_id'] = $_invoice->invoice_id;
|
|
|
|
$data['invoice_number'] = $_invoice->invoice_number;
|
2024-09-05 03:06:44 +02:00
|
|
|
$data['end_time'] = $this->getTenMinutesCountDownEndTime();
|
2024-09-08 12:50:29 +02:00
|
|
|
$data['invoice_redirect_url'] = "/client/invoices/{$_invoice->invoice_id}";
|
2024-09-06 02:52:21 +02:00
|
|
|
|
|
|
|
$data['websocket_url'] = 'wss://www.blockonomics.co/payment/' . $btc_address;
|
2024-09-02 09:59:12 +02:00
|
|
|
return render('gateways.blockonomics.pay', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
|
|
{
|
2024-09-10 08:02:55 +02:00
|
|
|
echo "Payment response received";
|
2024-09-02 09:59:12 +02:00
|
|
|
$request->validate([
|
|
|
|
'payment_hash' => ['required'],
|
|
|
|
'amount' => ['required'],
|
|
|
|
'currency' => ['required'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
try {
|
2024-09-06 06:02:18 +02:00
|
|
|
$data = [];
|
|
|
|
$data['amount'] = $request->amount;
|
|
|
|
$data['payment_method_id'] = $request->payment_method_id;
|
|
|
|
$data['payment_type'] = PaymentType::CRYPTO;
|
|
|
|
$data['gateway_type_id'] = GatewayType::CRYPTO;
|
2024-09-06 06:58:57 +02:00
|
|
|
$data['transaction_reference'] = "payment hash: " . $request->payment_hash . " txid: " . $request->txid;
|
|
|
|
$data['txid'] = $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-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-06 04:39:31 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->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)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|