1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/PaymentDrivers/BlockonomicsPaymentDriver.php

228 lines
7.5 KiB
PHP
Raw Normal View History

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) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers;
use App\Utils\Traits\MakesHash;
use App\Models\PaymentHash;
use App\Models\GatewayType;
use App\PaymentDrivers\Blockonomics\Blockonomics;
use App\Models\SystemLog;
use App\Models\Payment;
use App\Models\Client;
use App\Exceptions\PaymentFailed;
use App\Models\PaymentType;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Models\Invoice;
class BlockonomicsPaymentDriver extends BaseDriver
{
use MakesHash;
public $refundable = false; //does this gateway support refunds?
2024-09-02 09:59:12 +02:00
public $token_billing = false; //does this gateway support token billing?
public $can_authorise_credit_card = false; //does this gateway support authorizations?
public $gateway; //initialized gateway
public $payment_method; //initialized payment method
public static $methods = [
GatewayType::CRYPTO => Blockonomics::class, //maps GatewayType => Implementation class
];
public const SYSTEM_LOG_TYPE = SystemLog::TYPE_CHECKOUT; //define a constant for your gateway ie TYPE_YOUR_CUSTOM_GATEWAY - set the const in the SystemLog model
public $blockonomics;
public $BASE_URL = 'https://www.blockonomics.co';
public $NEW_ADDRESS_URL = 'https://www.blockonomics.co/api/new_address';
public $PRICE_URL = 'https://www.blockonomics.co/api/price';
public $SET_CALLBACK_URL = 'https://www.blockonomics.co/api/update_callback';
2024-09-02 09:59:12 +02:00
2024-09-03 04:30:10 +02:00
public function init()
{
2024-09-03 04:30:10 +02:00
$this->api_key = $this->company_gateway->getConfigField('apiKey');
2024-09-04 04:50:08 +02:00
$this->callback_secret = $this->company_gateway->getConfigField('callbackSecret');
$this->callback_url = $this->company_gateway->getConfigField('callbackUrl');
2024-09-03 04:30:10 +02:00
return $this; /* This is where you boot the gateway with your auth credentials*/
}
2024-09-03 04:30:10 +02:00
private function get($url, $apiKey)
{
2024-09-03 04:30:10 +02:00
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json'
]);
// Execute cURL session and get the response
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
throw new Exception(curl_error($ch));
}
// Close cURL session
curl_close($ch);
// Return the response
return json_decode($response, true);
}
2024-09-03 04:30:10 +02:00
public function get_callbacks($api_key)
2024-09-02 09:59:12 +02:00
{
2024-09-03 04:30:10 +02:00
$GET_CALLBACKS_URL = 'https://www.blockonomics.co/api/address?&no_balance=true&only_xpub=true&get_callback=true';
$response = $this->get($GET_CALLBACKS_URL, $api_key);
return $response;
2024-09-02 09:59:12 +02:00
}
2024-09-04 04:50:08 +02:00
public function getBTCPrice($id_currency)
{
$BLOCKONOMICS_BASE_URL = 'https://www.blockonomics.co';
$BLOCKONOMICS_PRICE_URL = $BLOCKONOMICS_BASE_URL . '/api/price?currency=';
// Getting price
// $currency = new Currency((int) $id_currency);
// $options = ['http' => ['method' => 'GET']];
// $context = stream_context_create($options);
// $contents = Tools::file_get_contents(Configuration::get('BLOCKONOMICS_PRICE_URL') . $currency->iso_code, false, $context);
// $priceObj = Tools::jsonDecode($contents);
return $priceObj->price;
}
2024-09-03 04:30:10 +02:00
// public function get_callbackSecret()
// {
// return md5(uniqid(rand(), true));
// }
2024-09-02 09:59:12 +02:00
/* Returns an array of gateway types for the payment gateway */
public function gatewayTypes(): array
{
$types = [];
$types[] = GatewayType::CRYPTO;
return $types;
}
public function setPaymentMethod($payment_method_id)
{
$class = self::$methods[$payment_method_id];
$this->payment_method = new $class($this);
return $this;
}
public function processPaymentView(array $data)
{
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');
/** @var \stdClass $blockonomicsRep */
$blockonomicsRep = json_decode($webhook_payload);
if ($blockonomicsRep == null) {
throw new PaymentFailed('Empty data');
}
if (true === empty($blockonomicsRep->invoiceId)) {
throw new PaymentFailed(
'Invalid payment notification- did not receive invoice ID.'
);
}
if (
str_starts_with($blockonomicsRep->invoiceId, "__test__")
|| $blockonomicsRep->type == "InvoiceProcessing"
|| $blockonomicsRep->type == "InvoiceCreated"
) {
return;
}
$sig = '';
$headers = getallheaders();
foreach ($headers as $key => $value) {
if (strtolower($key) === 'blockonomics-sig') {
$sig = $value;
}
}
$this->init();
2024-09-03 04:30:10 +02:00
$webhookClient = new Webhook($this->callback_url , $this->api_key);
2024-09-02 09:59:12 +02:00
if (!$webhookClient->isIncomingWebhookRequestValid($webhook_payload, $sig, $this->webhook_secret)) {
throw new \RuntimeException(
2024-09-04 04:50:08 +02:00
'Invalid blockonomics payment notification message received - signature did not match.'
2024-09-02 09:59:12 +02:00
);
}
$this->setPaymentMethod(GatewayType::CRYPTO);
$this->payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$blockonomicsRep->metadata->InvoiceNinjaPaymentHash])->firstOrFail();
$StatusId = Payment::STATUS_PENDING;
if ($this->payment_hash->payment_id == null) {
$_invoice = Invoice::with('client')->withTrashed()->find($this->payment_hash->fee_invoice_id);
$this->client = $_invoice->client;
$dataPayment = [
'payment_method' => $this->payment_method,
'payment_type' => PaymentType::CRYPTO,
'amount' => $_invoice->amount,
'gateway_type_id' => GatewayType::CRYPTO,
'transaction_reference' => $blockonomicsRep->invoiceId
];
$payment = $this->createPayment($dataPayment, $StatusId);
} else {
/** @var \App\Models\Payment $payment */
$payment = Payment::withTrashed()->find($this->payment_hash->payment_id);
$StatusId = $payment->status_id;
}
switch ($blockonomicsRep->type) {
case "InvoiceExpired":
$StatusId = Payment::STATUS_CANCELLED;
break;
case "InvoiceInvalid":
$StatusId = Payment::STATUS_FAILED;
break;
case "InvoiceSettled":
$StatusId = Payment::STATUS_COMPLETED;
break;
}
if ($payment->status_id != $StatusId) {
$payment->status_id = $StatusId;
$payment->save();
}
}
public function refund(Payment $payment, $amount, $return_client_response = false)
{
$this->setPaymentMethod(GatewayType::CRYPTO);
return $this->payment_method->refund($payment, $amount); //this is your custom implementation from here
}
}