Skip to content

Commit

Permalink
Merge pull request #11 from XPRNetwork/curl-to-wp-methods
Browse files Browse the repository at this point in the history
CHORE: Curl to wp methods
  • Loading branch information
SuperstrongBE authored May 23, 2024
2 parents 31a4435 + 41fa7a0 commit 13ae868
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 108 deletions.
19 changes: 11 additions & 8 deletions includes/rpc/PriceRateRPC.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

class PriceRateRPC
{

Expand All @@ -20,19 +21,21 @@ public function getUSDConvertionRate($currency = "EUR", $usdAmount = 10)
if (is_null($savedPriceRates) || $now > $savedPriceRatesValidity) {

$url = "https://api.freecurrencyapi.com/v1/latest";
$ch = curl_init($url);
$headers = array(
"apikey: " . $this->apiKey,
'Content-Type: application/json'
);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"base_currency": "USD"}');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($response, true);
$response = wp_remote_get($url, array(
'headers' => $headers,
'body' => json_encode(array('base_currency' => 'USD'))
));

if (is_wp_error($response)) {
return $usdAmount; // Handle error
}

$responseData = json_decode(wp_remote_retrieve_body($response), true);

$myPluginGateway->update_option('price_rates_validity', $now + $this->priceValidityInterval);
$myPluginGateway->update_option('price_rates', serialize($responseData['data']));
Expand Down
135 changes: 54 additions & 81 deletions includes/rpc/ProtonRPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,32 @@ public function __construct($endpoint)

public function verifyTransaction($paymentKey)
{

$endpoint = $this->endpoint . '/v1/history/get_transaction';
$data = array(
'id' => $transactionId,
'id' => $paymentKey,
);
error_log("check transaction");
error_log($endpoint);
error_log(print_r($data, 1));

$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
if ($response !== false) {
$responseData = json_decode($response, true);
$memo = $this->findValueByKey($responseData, "memo");
return $memo == $paymentKey;
$response = wp_remote_post($endpoint, array(
'body' => json_encode($data),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 45
));

if (is_wp_error($response)) {
return false; // Handle error
}

return false;
$responseData = json_decode(wp_remote_retrieve_body($response), true);
$memo = $this->findValueByKey($responseData, "memo");
return $memo == $paymentKey;
}

public function verifyPaymentStatusByKey($paymentKey)
{

$endpoint = $this->endpoint . '/v1/chain/get_table_rows';

$data = array(
'scope' => "wookey",
'code' => "wookey",
Expand All @@ -51,35 +46,29 @@ public function verifyPaymentStatusByKey($paymentKey)
'limit' => 1,
'lower_bound' => $this->toEOSIOSha256($paymentKey),
'upper_bound' => $this->toEOSIOSha256($paymentKey),

);

$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
if ($response !== false) {
$responseData = json_decode($response, true);
error_log(print_r($responseData,1));
foreach ($responseData['rows'] as $row) {
if ($row['paymentKey'] == $paymentKey && $row['status'] == 1) return true;
}
$response = wp_remote_post($endpoint, array(
'body' => json_encode($data),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 45
));

return null;
} else {
if (is_wp_error($response)) {
return null; // Handle error
}

return null;
$responseData = json_decode(wp_remote_retrieve_body($response), true);
error_log(print_r($responseData, 1));
foreach ($responseData['rows'] as $row) {
if ($row['paymentKey'] == $paymentKey && $row['status'] == 1) return true;
}

return null;
}

public function fetchPayments($store)
{

$endpoint = $this->endpoint . '/v1/chain/get_table_rows';
$data = array(
'scope' => 'wookey',
Expand All @@ -92,78 +81,62 @@ public function fetchPayments($store)
'lower_bound' => $store,
'upper_bound' => $store,
'reverse' => true

);

$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
if ($response !== false) {
return json_decode($response, true);
} else {
$response = wp_remote_post($endpoint, array(
'body' => json_encode($data),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 45
));

return null;
if (is_wp_error($response)) {
return null; // Handle error
}
return null;

return json_decode(wp_remote_retrieve_body($response), true);
}

public function fetchBalances($store)
{

$endpoint = $this->endpoint . '/v1/chain/get_table_rows';
$data = array(
'scope' => $store,
'code' => 'wookey',
'table' => 'balances',
'json' => true,
'limit' => 100,


);

$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
if ($response !== false) {
return json_decode($response, true);
} else {
$response = wp_remote_post($endpoint, array(
'body' => json_encode($data),
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 45
));

return null;
if (is_wp_error($response)) {
return null; // Handle error
}
return null;

return json_decode(wp_remote_retrieve_body($response), true);
}

public function findTransaction($actor,$afterDate,$paymentKey)
public function findTransaction($actor, $afterDate, $paymentKey)
{

$date = urlencode($afterDate);
$endpoint = $this->endpoint ."/v2/history/get_actions?limit=999&account=$actor&filter=*:transfer&after=$date";
$endpoint = $this->endpoint . "/v2/history/get_actions?limit=999&account=$actor&filter=*:transfer&after=$date";
error_log($endpoint);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
if ($response !== false) {
return json_decode($response,true);
} else {
$response = wp_remote_get($endpoint, array(
'headers' => array('Content-Type' => 'application/json'),
'timeout' => 45
));

return null;
if (is_wp_error($response)) {
return null; // Handle error
}
return null;
}

return json_decode(wp_remote_retrieve_body($response), true);
}

private function toEOSIOSha256($sha256Key)
{
Expand Down
36 changes: 17 additions & 19 deletions includes/rpc/TokensPricesRPC.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
<?php



class TokenPrices
{

private $priceValidityInterval = 21600000;

public function __construct()
{
}

public function getTokenPrices()
{

global $wpdb;
// Do your code checking stuff here e.g.
$myPluginGateway = WC()->payment_gateways->payment_gateways()['xprcheckout'];
Expand All @@ -26,39 +23,40 @@ public function getTokenPrices()
'Content-Type: application/json'
);
$url = "https://www.api.bloks.io/proton/tokens";
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$responseData = json_decode($response, true);
$response = wp_remote_get($url, array(
'headers' => $headers,
'timeout' => 45
));

if ($responseData) {
if (is_wp_error($response)) {
return null; // Handle error
}

$responseData = json_decode(wp_remote_retrieve_body($response), true);

$rawFiltered = array_map(function ($token)use($wpdb) {
if ($responseData) {
$rawFiltered = array_map(function ($token) use ($wpdb) {
if (strpos($token['key'], 'test') !== false) return null;
$tokenBase = [];

$tokenBase['symbol'] = $token['symbol'];
$tokenBase['contract'] = $token['account'];
$tokenBase['decimals'] = $token['supply']['precision'];
$tokenBase['logo'] = $token['metadata']['logo'];
$rawPrices = array_filter($token['pairs'], function ($pair) {
$rawPrices = array_filter($token['pairs'], function ($pair) {
return $pair['pair_quote'] == 'USD';
});

$prices = array_values($rawPrices);

if (isset($prices[0])) {
$mergedToken = array_merge($prices[0], $tokenBase);
if ($mergedToken['pair_base'] == "XPR")error_log(print_r($mergedToken['quote']['price_usd'],1));
$sql = "INSERT INTO wp_".XPRCHECKOUT_TABLE_TOKEN_RATES." (symbol,contract,token_precision,rate) VALUES (%s,%s,%d,%.12f) ON DUPLICATE KEY UPDATE rate = %.12f";
$sql = $wpdb->prepare($sql,$mergedToken['pair_base'],$mergedToken['contract'],$mergedToken['decimals'],$mergedToken['quote']['price_usd'],$mergedToken['quote']['price_usd']);
if ($mergedToken['pair_base'] == "XPR") error_log(print_r($mergedToken['quote']['price_usd'], 1));
$sql = "INSERT INTO wp_" . XPRCHECKOUT_TABLE_TOKEN_RATES . " (symbol,contract,token_precision,rate) VALUES (%s,%s,%d,%.12f) ON DUPLICATE KEY UPDATE rate = %.12f";
$sql = $wpdb->prepare($sql, $mergedToken['pair_base'], $mergedToken['contract'], $mergedToken['decimals'], $mergedToken['quote']['price_usd'], $mergedToken['quote']['price_usd']);
$res = $wpdb->query($sql);



return $mergedToken;
} else {
return null;
Expand Down

0 comments on commit 13ae868

Please sign in to comment.