From 886d1af8245cbf66bd71a2ab4acbd6be5b1ee2c3 Mon Sep 17 00:00:00 2001 From: cnohall Date: Thu, 5 Sep 2024 14:29:16 +0900 Subject: [PATCH] setup callback logic --- .../Blockonomics/Blockonomics.php | 52 ++++++++++++++++++- .../BlockonomicsPaymentDriver.php | 1 - database/seeders/PaymentLibrariesSeeder.php | 3 +- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/app/PaymentDrivers/Blockonomics/Blockonomics.php b/app/PaymentDrivers/Blockonomics/Blockonomics.php index 6078290f1f..e5cc5c7281 100644 --- a/app/PaymentDrivers/Blockonomics/Blockonomics.php +++ b/app/PaymentDrivers/Blockonomics/Blockonomics.php @@ -52,10 +52,58 @@ class Blockonomics implements MethodInterface { } + public function doCurlCall($url, $post_content = '') + { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + if ($post_content) { + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $post_content); + } + curl_setopt($ch, CURLOPT_TIMEOUT, 60); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Authorization: Bearer ' . $this->driver_class->api_key, + 'Content-type: application/x-www-form-urlencoded', + ]); + + $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) { + echo "ERROR: " . $status . ' ' . $responseObj->message; + } else { + echo "Success: " . $status; + echo json_encode($responseObj); + } + + return $responseObj; + } + + public function setCallbackUrl() + { + $GET_CALLBACKS_URL = 'https://www.blockonomics.co/api/address?&no_balance=true&only_xpub=true&get_callback=true'; + $SET_CALLBACK_URL = 'https://www.blockonomics.co/api/update_callback'; + $get_callback_response = $this->doCurlCall($GET_CALLBACKS_URL); + + $callback_url = $this->driver_class->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; + } + public function getBTCAddress() { $api_key = $this->driver_class->api_key; - $url = 'https://www.blockonomics.co/api/new_address'; + // TODO: remove ?reset=1 before marking PR as ready + $url = 'https://www.blockonomics.co/api/new_address?reset=1'; $ch = curl_init(); @@ -113,12 +161,12 @@ class Blockonomics implements MethodInterface $data['btc_address'] = $this->getBTCAddress(); $data['invoice_id'] = $this->invoice_id; $data['end_time'] = $this->getTenMinutesCountDownEndTime(); + $data['callback_url'] = $this->setCallbackUrl(); return render('gateways.blockonomics.pay', $data); } public function paymentResponse(PaymentResponseRequest $request) { - $request->validate([ 'payment_hash' => ['required'], 'amount' => ['required'], diff --git a/app/PaymentDrivers/BlockonomicsPaymentDriver.php b/app/PaymentDrivers/BlockonomicsPaymentDriver.php index 77f5ddf2e4..e71273879b 100644 --- a/app/PaymentDrivers/BlockonomicsPaymentDriver.php +++ b/app/PaymentDrivers/BlockonomicsPaymentDriver.php @@ -48,7 +48,6 @@ class BlockonomicsPaymentDriver extends BaseDriver 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'; public function init() { diff --git a/database/seeders/PaymentLibrariesSeeder.php b/database/seeders/PaymentLibrariesSeeder.php index fa650e0620..2137e2ce90 100644 --- a/database/seeders/PaymentLibrariesSeeder.php +++ b/database/seeders/PaymentLibrariesSeeder.php @@ -24,8 +24,7 @@ class PaymentLibrariesSeeder extends Seeder { Model::unguard(); - // TODO: use get callbacks API to get the callback URL - $callbackUrl = config('ninja.app_url') . '/payment/callback'; + $callbackUrl = config('ninja.app_url') . '/client/payments/process/?secret='; $callbackSecret = md5(uniqid(rand(), true)); $blockonomics_fields = "{ \"apiKey\": \"\",