Skip to content

Commit

Permalink
First commit, public release
Browse files Browse the repository at this point in the history
  • Loading branch information
KoenPaas authored Nov 12, 2020
0 parents commit 4134007
Show file tree
Hide file tree
Showing 15 changed files with 1,516 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 KoenPaas, XRPL Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# WooCommerce plugin for XRP ledger payments using XUMM

See: xumm.app
107 changes: 107 additions & 0 deletions inc/admin_form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
require 'language.php';

$form = $lang->form;

$headers = array(
'Content-Type' => 'application/json',
'X-API-Key' => $this->api,
'X-API-Secret' => $this->api_secret
);

$response = wp_remote_get('https://xumm.app/api/v1/platform/curated-assets', array(
'method' => 'GET',
'headers' => $headers,
));
$body = json_decode( $response['body'], true );

$this->form_fields = array(
'enabled' => array(
'title' => $form->enabled->title,
'label' => $form->enabled->label,
'type' => 'checkbox',
'description' => $form->enabled->description,
'default' => 'no'
),
'title' => array(
'title' => $form->title->title,
'type' => 'text',
'description' => $form->title->description,
'default' => $form->title->default,
'desc_tip' => true,
),
'description' => array(
'title' => $form->description->title,
'type' => 'textarea',
'description' => $form->description->description,
'default' => $form->description->default,
),
'destination' => array(
'title' => $form->destination->title,
'type' => 'text',
'description' => $form->destination->description,
'desc_tip' => true,
),
'explorer' => array(
'title' => $form->explorer->title,
'description' => $form->explorer->description,
'type' => 'select',
'options' => array(
'https://bithomp.com/explorer/' => 'Bithomp',
'https://xrpscan.com/tx/' => 'XRPScan',
'https://livenet.xrpl.org/transactions/' => 'XRPL.org'
),
'default' => 'https://bithomp.com/explorer/',
'desc_tip' => true
),
'api' => array(
'title' => $form->api->title,
'type' => 'text',
'description' => $form->api->description .' <a href="https://apps.xumm.dev/">https://apps.xumm.dev/</a>',
'default' => '',
),
'api_secret' => array(
'title' => $form->api_secret->title,
'type' => 'text',
'description' => $form->api_secret->description .' <a href="https://apps.xumm.dev/">https://apps.xumm.dev/</a>',
'default' => '',
)
);

$this->availableCurrencies['XRP'] = 'XRP';
foreach ($body['currencies'] as $v) {
if(get_woocommerce_currency() == $v){
$this->availableCurrencies[$v] = $v;
}
}

$this->form_fields['currencies'] = array(
'title' => $form->currencies->title,
'description' => $form->currencies->description,
'type' => 'select',
'options' => $this->availableCurrencies,
);

$availableIssuers = [];
foreach ($body['details'] as $exchange) {
$exchangeName = $exchange['name'];
foreach ($exchange['currencies'] as $currency) {
$value = $currency['issuer'];
$availableIssuers[$value] = $exchangeName;
}
}

$this->form_fields['issuers'] = array(
'title' => $form->issuers->title,
'description' => $form->issuers->description,
'type' => 'select',
'options' => $availableIssuers,
'default' => ''
);

$body['account'] = $this->destination;
$body['store_currency'] = get_woocommerce_currency();

wp_localize_script( 'xumm_js', 'xumm_object', $body);

?>
199 changes: 199 additions & 0 deletions inc/admin_options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
<?php
require 'language.php';

$admin = $lang->admin;

function getXummData($id, $self){
$response = wp_remote_get('https://xumm.app/api/v1/platform/payload/ci/'. $id, array(
'method' => 'GET',
'headers' => array(
'Content-Type' => 'application/json',
'X-API-Key' => $self->api,
'X-API-Secret' => $self->api_secret
)
));
$body = json_decode( $response['body'], true );
return $body;
}

if(!empty($_GET['xumm-id'])) {
$data = getXummData($_GET['xumm-id'], $this);
//Todo:: first check if success
if (!empty($data['payload'])) {
switch ($data['payload']['tx_type']) {
case 'SignIn':
$account = $data['response']['account'];
if(!empty($account))
$this->update_option('destination', $account );
echo('<div class="notice notice-success"><p>'.$admin->signin->success.'</p></div>');
break;

case 'TrustSet':
//Todo show message when trustset is success with: $admin->trustset->success
break;

default:
break;
}
}
}

?>
<h2><?php _e($admin->title,'woocommerce'); ?></h2>
<?php
if(!empty($_POST["specialAction"])) {
?>
<div id="customFormActionResult" style="display: none;">
<?php
$query_arr = array(
'page' => 'wc-settings',
'tab' => 'checkout',
'section' => 'xumm'
);

$return_url = get_home_url() .'/wp-admin/admin.php?' . http_build_query($query_arr);

$headers = [
'Content-Type' => 'application/json',
'X-API-Key' => $_POST['woocommerce_xumm_api'],
'X-API-Secret' => $_POST['woocommerce_xumm_api_secret']
];

switch($_POST["specialAction"]) {
case 'set_destination':
$identifier = 'sign-in_' . strtoupper(substr(md5(microtime()), 0, 10));
$return_url = add_query_arg( 'xumm-id', $identifier, $return_url);
$body = [
"txjson" => [
"TransactionType" => "SignIn"
],
"options" => [
"submit" => true,
"return_url" => [
"web" => $return_url ]
],
'custom_meta' => array(
'identifier' => $identifier
)
];
break;
case 'set_trustline':
$identifier = 'trustline_' . strtoupper(substr(md5(microtime()), 0, 10));
$return_url = add_query_arg( 'xumm-id', $identifier, $return_url);
$body = [
"txjson" => [
"TransactionType" => "TrustSet",
"Account" => $this->destination,
"Fee" => "12",
"LimitAmount" => [
"currency" => $_POST['woocommerce_xumm_currencies'],
"issuer" => $_POST['woocommerce_xumm_issuers'],
"value" => "999999999"
]
],
"options" => [
"submit" => true,
"return_url" => [
"web" => $return_url ]
],
'custom_meta' => array(
'identifier' => $identifier
)
];
break;
}

if (wp_is_mobile()){
$body['options']['return_url']['app'] = $return_url;
}

$body = wp_json_encode($body);

$response = wp_remote_post('https://xumm.app/api/v1/platform/payload', array(
'method' => 'POST',
'headers' => $headers,
'body' => $body
)
);

if( !is_wp_error( $response ) ) {
$body = json_decode( $response['body'], true );
$redirect = $body['next']['always'];
if ( $redirect != null ) {
// Redirect to the XUMM processor page
echo($redirect);
} else {
echo('<div class="notice notice-error"><p>'.$admin->api->redirect_error.' <a href="https://apps.xumm.dev/">'. $admin->api->href .'</a>. '. $admin->api->keys .'Error Code:'. $body['error']['code'] .'</p></div>');
}

} else {
echo('<div class="notice notice-error"><p>'.$admin->api->no_response.' <a href="https://apps.xumm.dev/">'. $admin->api->href .'</a>.</p></div>');
}
?>
</div>
<?php
}
?>
<table class="form-table">
<?php
$this->generate_settings_html();
$storeCurrency = get_woocommerce_currency();
if(empty($this->api) || empty($this->api_secret)) echo('<div class="notice notice-info"><p>'. $admin->api->no_keys .' <a href="https://apps.xumm.dev/">'. $admin->api->href .'</a></p></div>');
else {
$response = wp_remote_get('https://xumm.app/api/v1/platform/ping', array(
'method' => 'GET',
'headers' => array(
'Content-Type' => 'application/json',
'X-API-Key' => $this->api,
'X-API-Secret' => $this->api_secret
)
));
if( !is_wp_error( $response ) ) {
$body = json_decode( $response['body'], true );
if(!empty($body['pong'] && $body['pong'] == true)) {
echo('<div class="notice notice-success"><p>'.$admin->api->ping_success.' <a href="https://apps.xumm.dev/">'.$admin->api->href.'</a></p></div>');

$webhookApi = $body['auth']['application']['webhookurl'];
$webhook = get_home_url() . '/?wc-api='. $this->id;
if($webhook != $webhookApi) echo('<div class="notice notice-error"><p>'.$admin->api->incorrect_webhook.' <a href="https://apps.xumm.dev/">'.$admin->api->href.'</a>, '.$admin->api->corrected_webhook.' '.$webhook.'</p></div>');
}
else echo('<div class="notice notice-error"><p>'.$admin->api->ping_error.' <a href="https://apps.xumm.dev/">'.$admin->api->href.'</a>. '.$admin->api->keys .'Error Code:'. $body['error']['code'].'</p></div>');
} else {
echo('<div class="notice notice-error"><p>'.$admin->api->no_response.' <a href="https://apps.xumm.dev/">'.$admin->api->href.'</a></p></div>');
}
}
if(!in_array($storeCurrency, $this->availableCurrencies)) echo('<div class="notice notice-error"><p>'.$admin->currency->store_unsupported.'</p></div>');
if ($storeCurrency != 'XRP' && $this->currencies != 'XRP' && $storeCurrency != $this->currencies) echo('<div class="notice notice-error"><p>'.$admin->currency->gateway_unsupported.'</p></div>');
?>
</table>

<input type="hidden" id="specialAction" name="specialAction" value="">
<button type="button" class="customFormActionBtn" id="set_destination" style="border-style: none; cursor:pointer; background-color: Transparent;">
<?php echo(file_get_contents(dirname(plugin_dir_path( __FILE__ )) .'/public/images/signin.svg')); ?>
</button>
<button type="button" class="customFormActionBtn button-primary" id="set_trustline">
<?php echo ($admin->trustset->button); ?>
</button>

<script>
jQuery("form#mainform").submit(function (e) {
if (jQuery(this).find("input#specialAction").val() !== '') {
e.preventDefault()
jQuery.ajax({
url: document.location.href,
type: 'POST',
data: jQuery(this).serialize(),
success: function (response) {
let tlResponse = jQuery(response).find("#customFormActionResult").html().trim()
window.location.href = tlResponse
}
});
return false
}
})
jQuery("button.customFormActionBtn").click(function () {
jQuery("input#specialAction").val(jQuery(this).attr('id'))
jQuery("form#mainform").submit()
})
</script>
<?php
61 changes: 61 additions & 0 deletions inc/callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
$headers = array(
'Content-Type' => 'application/json',
'X-API-Key' => $this->api,
'X-API-Secret' => $this->api_secret
);

$explorer = $this->explorer;

$json = file_get_contents('php://input');
$json = json_decode($json, true);

$uuid = $json['payloadResponse']['payload_uuidv4'];

if($uuid != null) {
$custom_identifier = $json['custom_meta']['identifier'];
if ($custom_identifier != null) {
$data = getPayloadXummById($custom_identifier, $headers);
if (!empty($data['payload'])) {

switch ($data['payload']['tx_type']) {

case 'Payment':
$txid = $data['response']['txid'];
$xr = $data['custom_meta']['blob']['xr'];

$txbody = getTransactionDetails($txid, $headers);

$order_id = explode("_", $custom_identifier)[0];
$order = wc_get_order( $order_id );
$delivered_amount = $txbody['transaction']['meta']['delivered_amount'];
if(!checkDeliveredAmount($delivered_amount, $order, $xr, $this->issuers, $txid, $explorer)) {
exit();
}

$order->payment_complete();
wc_reduce_stock_levels( $order_id );

$success = $lang->callback->note->success;
// A notes to the customer (replace true with false to make it private)
$order->add_order_note( $success->thanks . '<br>'. $success->check .'<a href="'.$explorer.$txid.'"> '.$success->href.'</a>', true );

WC()->cart->empty_cart();
break;

case 'SignIn':
$account = $data['response']['account'];
if(!empty($account))
echo($account);
$this->update_option('destination', $account );
break;

case 'TrustSet':

break;
}
}

}
}
?>
Loading

0 comments on commit 4134007

Please sign in to comment.