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 ;
2024-09-03 03:04:27 +02:00
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 ;
2024-09-03 03:04:27 +02:00
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' ;
2024-09-02 09:59:12 +02:00
2024-09-03 04:30:10 +02:00
public function init ()
2024-09-03 03:04:27 +02:00
{
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-10 08:02:55 +02:00
// $this->setCallbackUrl();
2024-09-03 04:30:10 +02:00
return $this ; /* This is where you boot the gateway with your auth credentials*/
2024-09-03 03:04:27 +02:00
}
2024-09-10 08:02:55 +02:00
public function doCurlCall ( $url , $post_content = '' )
2024-09-03 03:04:27 +02:00
{
2024-09-03 04:30:10 +02:00
$ch = curl_init ();
curl_setopt ( $ch , CURLOPT_URL , $url );
2024-09-10 08:02:55 +02:00
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
if ( $post_content ) {
curl_setopt ( $ch , CURLOPT_POST , 1 );
curl_setopt ( $ch , CURLOPT_POSTFIELDS , $post_content );
2024-09-05 02:39:32 +02:00
}
2024-09-10 08:02:55 +02:00
curl_setopt ( $ch , CURLOPT_TIMEOUT , 60 );
curl_setopt ( $ch , CURLOPT_HTTPHEADER , [
'Authorization: Bearer ' . $this -> api_key ,
'Content-type: application/x-www-form-urlencoded' ,
]);
2024-09-03 04:30:10 +02:00
2024-09-10 08:02:55 +02:00
$contents = curl_exec ( $ch );
2024-09-03 04:30:10 +02:00
if ( curl_errno ( $ch )) {
2024-09-10 08:02:55 +02:00
echo " Error: " . curl_error ( $ch );
2024-09-03 04:30:10 +02:00
}
2024-09-10 08:02:55 +02:00
$responseObj = json_decode ( $contents );
$status = curl_getinfo ( $ch , CURLINFO_HTTP_CODE );
curl_close ( $ch );
2024-09-03 04:30:10 +02:00
2024-09-10 08:02:55 +02:00
if ( $status != 200 ) {
echo " ERROR: " . $status . ' ' . $responseObj -> message ;
}
return $responseObj ;
2024-09-03 03:04:27 +02:00
}
2024-09-10 08:02:55 +02:00
public function setCallbackUrl ()
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' ;
2024-09-10 08:02:55 +02:00
$SET_CALLBACK_URL = 'https://www.blockonomics.co/api/update_callback' ;
$get_callback_response = $this -> doCurlCall ( $GET_CALLBACKS_URL );
$callback_url = $this -> callback_url ;
$xpub = $get_callback_response [ 0 ] -> address ;
$post_content = '{"callback": "' . $callback_url . '", "xpub": "' . $xpub . '"}' ;
$responseObj = $this -> doCurlCall ( $SET_CALLBACK_URL , $post_content );
return $responseObj ;
2024-09-02 09:59:12 +02:00
}
2024-09-10 08:02:55 +02:00
public function findPaymentByTxid ( $txid )
{
return Payment :: whereRaw ( 'BINARY `transaction_reference` LIKE ? AND BINARY `transaction_reference` LIKE ?' , [
" %payment hash:% " ,
" %txid: " . $txid . " % "
]) -> firstOrFail ();
}
public function findPaymentHashInTransactionReference ( $transaction_reference )
{
$pattern = '/payment hash:\s*([a-zA-Z0-9]+)/' ;
// Perform the regex match
if ( preg_match ( $pattern , $transaction_reference , $matches )) {
// Return the matched payment hash
return $matches [ 1 ];
} else {
// Return null if no match is found
return null ;
}
}
2024-09-03 04:30:10 +02:00
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 ()
{
2024-09-10 08:02:55 +02:00
// TODO: Figure out why init does not work
// $this->init();
// $secret = $this->company_gateway->getConfigField('callbackSecret');
// //Match secret for security
// if ($_GET['secret'] != $secret) {
// echo "Invalid Secret";
// return;
// }
2024-09-08 13:24:13 +02:00
$txid = $_GET [ 'txid' ];
$value = $_GET [ 'value' ];
$status = $_GET [ 'status' ];
$addr = $_GET [ 'addr' ];
// Only accept confirmed transactions
if ( $status != 2 ) {
throw new PaymentFailed ( 'Transaction not confirmed' );
}
2024-09-10 08:02:55 +02:00
$payment = $this -> findPaymentByTxid ( $txid );
$payment_hash = $this -> findPaymentHashInTransactionReference ( $payment -> transaction_reference );
2024-09-08 13:24:13 +02:00
switch ( $status ) {
case 0 :
2024-09-10 08:02:55 +02:00
$statusId = Payment :: STATUS_PENDING ;
2024-09-08 13:24:13 +02:00
break ;
case 1 :
2024-09-10 08:02:55 +02:00
$statusId = Payment :: STATUS_PENDING ;
2024-09-08 13:24:13 +02:00
break ;
case 2 :
2024-09-10 08:02:55 +02:00
$statusId = Payment :: STATUS_COMPLETED ;
2024-09-08 13:24:13 +02:00
break ;
}
// Save the updated payment status
2024-09-10 08:02:55 +02:00
if ( $payment -> status_id != $statusId ) {
$payment -> status_id = $statusId ;
2024-09-08 13:24:13 +02:00
$payment -> save ();
}
2024-09-10 08:19:09 +02:00
header ( 'HTTP/1.1 200 OK' );
echo 'SUCCESS' ;
return ;
2024-09-02 09:59:12 +02:00
}
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
}
}