Changeset 3046162
- Timestamp:
- 03/06/2024 09:02:41 AM (23 months ago)
- Location:
- multi-crypto-currency-payment/trunk
- Files:
-
- 1 added
- 11 edited
-
inc/activate-plugin.php (modified) (2 diffs)
-
inc/apirone_api/Apirone.php (modified) (1 diff)
-
inc/apirone_api/Payment.php (modified) (5 diffs)
-
inc/apirone_api/QRCode.php (added)
-
inc/apirone_api/Utils.php (modified) (2 diffs)
-
inc/assets-assign.php (modified) (1 diff)
-
inc/invoice-admin-trait.php (modified) (3 diffs)
-
inc/invoice-class.php (modified) (7 diffs)
-
inc/invoice-db-trait.php (modified) (1 diff)
-
inc/invoice-utils-trait.php (modified) (1 diff)
-
mccp.php (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
multi-crypto-currency-payment/trunk/inc/activate-plugin.php
r2946588 r3046162 11 11 */ 12 12 function mccp_activate() { 13 global $wpdb;14 13 global $wpdb; 14 15 15 require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 16 16 17 $sql = Db::createInvoicesTableQuery($wpdb->prefix, $wpdb->charset, $wpdb->collate);17 $sql = Db::createInvoicesTableQuery($wpdb->prefix, $wpdb->charset, $wpdb->collate); 18 18 19 dbDelta($sql);19 dbDelta($sql); 20 20 21 21 if ($wpdb->last_error && class_exists('WC_Logger')) { … … 24 24 } 25 25 26 // Remove unused options from v1.0.027 if ( get_option('mccp_db_version' ) ){28 delete_option('mccp_db_version');29 }26 // Remove unused options from v1.0.0 27 if ( get_option('mccp_db_version' ) ){ 28 delete_option('mccp_db_version'); 29 } 30 30 } 31 31 register_activation_hook( MCCP_MAIN, 'mccp_activate' ); -
multi-crypto-currency-payment/trunk/inc/apirone_api/Apirone.php
r2946588 r3046162 243 243 $abbr = substr($abbr, 1); 244 244 } 245 $abbr = str_replace('@', '_', $abbr); 245 246 return sprintf(self::$currencyIconUrl, $abbr); 246 247 } -
multi-crypto-currency-payment/trunk/inc/apirone_api/Payment.php
r2839125 r3046162 3 3 namespace ApironeApi; 4 4 use ApironeApi\Apirone; 5 use QRCode; 5 6 6 7 require_once(__DIR__ . '/Apirone.php'); … … 23 24 24 25 $remains = Payment::exp2dec( Payment::min2cur($remains, $currency->{'units-factor'}) ); 25 $link = Payment::getTransactionLink($currency);26 26 27 27 $status = $invoice->status; … … 44 44 <div class="qr__wrapper"> 45 45 <?php if ($status == 'created' || $status == 'partpaid') : ?> 46 <img src="<?php echo Payment:: getQrLink($currency, $details->address, $remains); ?>">46 <img src="<?php echo Payment::renderQr($currency, $details->address, $totalAmount); ?>"> 47 47 <?php $statusMessageDesc = ($countdown >= 0) ? 'Waiting for payments...' : 'Updating status...'; ?> 48 48 <div class="status-icon-wrapper"> … … 66 66 } 67 67 ?> 68 <img src="<?php echo Payment:: getQrLink($currency, $details->address, $totalAmount); ?>" class="blur">68 <img src="<?php echo Payment::renderQr($currency, $details->address, $totalAmount); ?>" class="blur"> 69 69 <div class="status-icon-wrapper"> 70 70 <div class="status-icon"></div> … … 146 146 <span class="date-gmt"><?php echo $item->date . 'Z'; ?></span> 147 147 <?php if (property_exists($item, 'txid')) : ?> 148 <a class="address-link" href="<?php echo $link . $item->txid; ?>" target="_blank"></a> 148 <a class="address-link" href="<?php echo Payment::getTransactionLink($currency, $item->txid); ?>" target="_blank"> 149 (<?php echo Payment::exp2dec( Payment::min2cur($item->amount, $currency->{'units-factor'}) ); ?>) 150 </a> 149 151 <?php endif; ?> 150 152 </div> -
multi-crypto-currency-payment/trunk/inc/apirone_api/Utils.php
r2943825 r3046162 2 2 namespace ApironeApi; 3 3 4 // require_once(__DIR__ . '/Request.php'); 4 require_once(__DIR__ . '/QRCode.php'); 5 6 use ApironeApi\QRCode; 5 7 6 8 trait Utils { … … 31 33 return sprintf('https://blockchair.com/%s/address/', strtolower(str_replace([' ', '(', ')'], ['-', '/', ''], $currency->name))) . $address; 32 34 } 35 36 /** 37 * Return base64 encoded QR png 38 * 39 * @param mixed $currency 40 * @param mixed $input_address 41 * @param mixed $amount 42 * @return string 43 */ 44 public static function renderQr($currency, $input_address, $amount = null) 45 { 46 $prefix = (substr_count($input_address, ':') > 0) ? '' : strtolower(str_replace([' ', '(', ')'], ['-', '', ''], $currency->name)) . ':'; 47 $amount = ($amount !== null && $amount > 0) ? '?amount=' . $amount : ''; 48 49 return QRCode::init()->data($prefix . $input_address . $amount)->size(225)->levelQrt()->base64(); 50 } 51 33 52 34 53 /** -
multi-crypto-currency-payment/trunk/inc/assets-assign.php
r2910488 r3046162 3 3 function mccp_assets_admin() 4 4 { 5 wp_enqueue_style( 'mccp_style', MCCP_URL . 'assets/mccp-admin.css' );5 wp_enqueue_style( 'mccp_style', MCCP_URL . 'assets/mccp-admin.css' ); 6 6 } 7 7 add_action('admin_enqueue_scripts', 'mccp_assets_admin'); 8 8 9 9 function mccp_assets() { 10 wp_enqueue_style ( 'mccp_style_invoice', MCCP_URL . 'inc/apirone_api/assets/style.min.css' );11 wp_enqueue_script('mccp_script_invoice', MCCP_URL . 'inc/apirone_api/assets/script.min.js', array( 'jquery'));12 wp_enqueue_style( 'mccp_style', MCCP_URL . 'assets/mccp.css' );10 wp_enqueue_style ( 'mccp_style_invoice', MCCP_URL . 'inc/apirone_api/assets/style.min.css' ); 11 wp_enqueue_script('mccp_script_invoice', MCCP_URL . 'inc/apirone_api/assets/script.min.js', array( 'jquery')); 12 wp_enqueue_style( 'mccp_style', MCCP_URL . 'assets/mccp.css' ); 13 13 } 14 14 add_action('get_footer', 'mccp_assets'); -
multi-crypto-currency-payment/trunk/inc/invoice-admin-trait.php
r2946588 r3046162 9 9 trait MCCP_Admin { 10 10 11 public function show_invoice_admin_info($order) {12 if ($order->payment_method == 'mccp') {13 echo '<h3>' . __('Payment details', 'mccp') . '</h3>';14 $invoices = WC_MCCP::get_order_invoices($order->get_id());15 if ($invoices) {16 foreach ($invoices as $invoice) {17 $currency = Apirone::getCurrency($invoice->details->currency);18 echo '<hr />';19 echo sprintf(__('<div>Address: <b>%s</b></div>', 'mccp'), $invoice->details->address);20 echo sprintf(__('<div>Created: <b>%s</b></div>', 'mccp'), get_date_from_gmt($invoice->details->created, 'd.m.Y H:i:s'));21 echo sprintf(__('<div>Amount: <b>%s %s</b></div>', 'mccp'), Apirone::min2cur($invoice->details->amount, $currency->{'units-factor'}), strtoupper($invoice->details->currency));22 echo sprintf(__('<div>Status: <b>%s</b></div>', 'mccp'), $invoice->status);23 foreach ($invoice->details->history as $item) {24 $status = property_exists($item, 'txid') ? ' <a class="address-link" href="' . Apirone::getTransactionLink($currency, $item->txid) . '" target="_blank">' . $item->status . '</a>' : $item->status;25 echo '<div>- <i>' . get_date_from_gmt($item->date, 'd.m.Y H:i:s') . '</i> <b>' . $status . '</b></div>';26 }27 }28 }29 else {30 echo '<p>' . __('Invoice for this order not found', 'mccp') . '</p>';31 }32 }33 return;34 }35 36 /**37 * Generate admin page html code38 *39 * @return void40 */41 public function admin_options() {11 public function show_invoice_admin_info($order) { 12 if ($order->payment_method == 'mccp') { 13 echo '<h3>' . __('Payment details', 'mccp') . '</h3>'; 14 $invoices = WC_MCCP::get_order_invoices($order->get_id()); 15 if ($invoices) { 16 foreach ($invoices as $invoice) { 17 $currency = Apirone::getCurrency($invoice->details->currency); 18 echo '<hr />'; 19 echo sprintf(__('<div>Address: <b>%s</b></div>', 'mccp'), $invoice->details->address); 20 echo sprintf(__('<div>Created: <b>%s</b></div>', 'mccp'), get_date_from_gmt($invoice->details->created, 'd.m.Y H:i:s')); 21 echo sprintf(__('<div>Amount: <b>%s %s</b></div>', 'mccp'), Apirone::min2cur($invoice->details->amount, $currency->{'units-factor'}), strtoupper($invoice->details->currency)); 22 echo sprintf(__('<div>Status: <b>%s</b></div>', 'mccp'), $invoice->status); 23 foreach ($invoice->details->history as $item) { 24 $status = property_exists($item, 'txid') ? ' <a class="address-link" href="' . Apirone::getTransactionLink($currency, $item->txid) . '" target="_blank">' . $item->status . '</a>' : $item->status; 25 echo '<div>- <i>' . get_date_from_gmt($item->date, 'd.m.Y H:i:s') . '</i> <b>' . $status . '</b></div>'; 26 } 27 } 28 } 29 else { 30 echo '<p>' . __('Invoice for this order not found', 'mccp') . '</p>'; 31 } 32 } 33 return; 34 } 35 36 /** 37 * Generate admin page html code 38 * 39 * @return void 40 */ 41 public function admin_options() { 42 42 global $table_prefix; 43 43 ?> 44 <h3><?php _e('Multi Crypto Currency Payment Gateway', 'mccp'); ?></h3>45 <div><?php _e('This plugin uses the Apirone crypto processing service.', 'mccp'); ?> <a href="https://apirone.com" target="_blank"><?php _e('Details'); ?></a></div>44 <h3><?php _e('Multi Crypto Currency Payment Gateway', 'mccp'); ?></h3> 45 <div><?php _e('This plugin uses the Apirone crypto processing service.', 'mccp'); ?> <a href="https://apirone.com" target="_blank"><?php _e('Details'); ?></a></div> 46 46 <?php 47 47 if ($this->is_table_exists()) : ?> … … 55 55 <div class="inline error"> 56 56 <?php if ( Apirone::serviceInfo() ) : ?> 57 <p><strong><?php _e('Currency check error', 'mccp'); ?></strong>: <?php _e(' MCCP don\'t support your shopcurrency', 'mccp'); ?></p>57 <p><strong><?php _e('Currency check error', 'mccp'); ?></strong>: <?php _e('The plugin does not support your store\'s currency', 'mccp'); ?></p> 58 58 <?php else : ?> 59 59 <p><strong><?php _e('Gateway offline', 'mccp'); ?></strong>: <?php _e('Service not available. Please, try later', 'mccp'); ?></p> … … 63 63 <?php else : ?> 64 64 <div class="inline error"> 65 <p><strong><?php _e('Table check error', 'mccp'); ?></strong>: <?php _e(' MCCP invoice table doesn\'t exists. Please contact thesupport.', 'mccp'); ?></p>65 <p><strong><?php _e('Table check error', 'mccp'); ?></strong>: <?php _e('The plugin invoice table does not exist. Try reinstalling the plugin. If the error persists, contact support.', 'mccp'); ?></p> 66 66 </div> 67 67 <?php endif; 68 }69 70 /**71 * Save apirone currensies into options table72 *73 * @return void74 */75 public function save_currencies() {76 if ($_SERVER['REQUEST_METHOD'] == 'POST') {77 $currencies = $this->mccp_currencies();78 79 // Remove not valid currensies before save option80 foreach ($currencies as $key => $value) {81 if (!empty($value->address) && !$value->valid) {82 $currencies[$key]->address = '';83 $currencies[$key]->enabled = 0;84 $currencies[$key]->valid = 0;85 }86 }87 $this->update_option('currencies', $currencies);88 }89 }90 91 /**92 * Generate currencies list options for admin page93 *94 * @param mixed $key95 * @param mixed $data96 * @return string|false97 */98 public function generate_currencies_list_html ($key, $data) {99 ob_start();100 ?>101 <tr valign="top">102 <th scope="row" class="titledesc">103 <label><?php _e('Currencies', 'mccp'); ?><?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label>104 </th>105 <td class="forminp" id="mccp-order_states">106 <div class="table-wrapper">107 <table class="form-table">108 <tbody>109 <?php110 foreach($this->mccp_currencies() as $currency) : ?>111 <tr valign="middle" class="single_select_page">112 <th scope="row" class="titledesc">113 <label for="mccp_<?php echo esc_html( $currency->abbr ); ?>" class="currency-label">114 <div class="<?php echo $this->currency_icon_wrapper($currency) ?>">115 <img src="<?php echo Apirone::currencyIcon($currency->abbr); ?>" alt="<?php echo esc_html( $currency->name ); ?>" class="currency-icon">116 </div>117 <?php echo esc_html( $currency->name ); ?>118 119 <?php echo wc_help_tip(sprintf(__('Enter valid address to activate <b>%s</b> currency', 'mccp'), $currency->name)); ?>120 </label>121 </th>122 <td class="forminp">123 <input type="text" name="woocommerce_mccp_currencies[<?php echo esc_html( $currency->abbr ); ?>][address]" class="input-text regular-input"124 value="<?php echo esc_html( $currency->address ); ?>">125 <?php if ( $currency->address ) : ?>126 <input type="checkbox" name="woocommerce_mccp_currencies[<?php echo esc_html( $currency->abbr ); ?>][enabled]" class="currency-enabled"127 <?php echo $currency->enabled ? ' checked' : ''; ?>128 <?php echo !$currency->valid ? ' disabled' : ''; ?>129 >130 <?php if( $currency->address) : ?>131 <?php if( $currency->valid ) : ?>132 <span class="valid">133 <?php _e('Use currency', 'mccp'); ?>134 </span>135 <?php else: ?>136 <span class="not-valid">137 <?php _e('Address is not valid', 'mccp'); ?>138 </span>139 <?php endif;?>140 <?php endif;?>141 <?php endif;?>142 <?php if ($currency->testnet) : ?>143 <div class="testnet-info"><span class="testnet-info__message"><?php _e('WARNING: Test currency', 'mccp'); ?></span>144 <?php echo wc_help_tip(__('Use this currency for test purposes only! This currency shown for admin only on front of woocommerce!')); ?></div>145 <?php endif; ?>146 </td>147 </tr>148 <?php endforeach; ?>149 150 </tbody>151 </table>152 </div>153 </td>154 </tr>155 156 <?php157 return ob_get_clean();158 }159 160 /**161 * Init Gateway Settings Form Fields 162 *163 * @return void164 */165 public function mccp_init_form_fields() {166 $this->form_fields = array(167 'enabled' => array(168 'title' => __('On/off', 'mccp'),169 'type' => 'checkbox',170 'label' => __('On', 'mccp'),171 'default' => 'no',172 'description' => __('Activate/deactivate MCCP gateway', 'mccp'),173 'desc_tip' => true,174 ),175 'merchant' => array(176 'title' => __('Merchant name', 'mccp'),177 'type' => 'text',178 'default' => '',179 'description' => __('Shows Merchant name in the payment order. If this field is blank then Site Title from General settings will be shown.', 'mccp'),180 'desc_tip' => true,181 ),182 'timeout' => array(183 'title' => __('Payment timeout, sec.', 'mccp'),184 'type' => 'number',185 'default' => '1800',186 'description' => __('The period during which a customer shall pay. Set value in seconds', 'mccp'),187 'desc_tip' => true,188 'custom_attributes' => array('min' => 0,),189 ),190 'currencies' => array(191 'type' => 'currencies_list',192 'description' => __('List of available cryptocurrencies processed by Apirone', 'mccp'),193 'desc_tip' => true,194 'default' => [],195 ),196 'factor' => array( 197 'title' => __('Price adjustment factor', 'mccp'),198 'type' => 'number', 199 'default' => '1',200 'description' => __('If you want to add/substract percent to/from the payment amount, use the following price adjustment factor multiplied by the amount.<br />For example: <br />100% * 0.99 = 99% <br />100% * 1.01 = 101%', 'mccp'),201 'desc_tip' => true,202 'custom_attributes' => array('min' => 0.01, 'step' => '0.01', 'required' => 'required'),203 ),204 'check_timeout' => array(205 'title' => __('Status update period, sec.', 'mccp'),206 'type' => 'number',207 'default' => '10',208 'description' => __('How often the invoice status shall be checked. Min value 5 seconds.', 'mccp'),209 'desc_tip' => true,210 'custom_attributes' => array('min' => 5, 'required' => 'required'),211 ),212 'backlink' => array(213 'title' => __('Back to store', 'mccp'),214 'type' => 'text',215 'default' => '',216 'placeholder' => site_url(''),217 'description' => __('Enter a valid URL for the "Back to store" link on the invoice page. If empty the home page is used.', 'mccp'),218 'desc_tip' => true,219 ),220 'apirone_logo' => array(221 'title' => __('Apirone logo', 'mccp'),222 'type' => 'checkbox',223 'label' => __('Show Apirone logo on invoice page', 'mccp'),224 'default' => 'yes',225 ),226 'debug' => array(227 'title' => __('Debug mode', 'mccp'),228 'type' => 'checkbox',229 'label' => __('On', 'mccp'),230 'default' => 'no',231 'description' => __('Write debug information into log file', 'mccp'),232 'desc_tip' => true,233 ),234 );235 }236 /**237 * Save apirone currensies into options table238 */239 public function validate_currencies_list_field($k, $v) {240 $currencies = $this->mccp_currencies();241 242 foreach ($currencies as $key => $value) {243 if (!empty($value->address) && !$value->valid) {244 $currencies[$key]->address = '';245 $currencies[$key]->enabled = 0;246 $currencies[$key]->valid = 0;247 }248 }249 return $currencies;250 }251 252 253 /**254 * Return css-class for currency icon255 *256 * @param mixed $currency257 * @return string258 */259 public function currency_icon_wrapper($currency) {260 return 'icon-wrapper' . ((substr_count(strtolower($currency->name), 'testnet') > 0) ? ' test-currency': '');261 }262 263 public function mccp_currency($apirone_currency, $apirone_account = false) {264 $currency = $this->get_mccp_currency($apirone_currency->abbr);265 if ($currency == false || gettype($currency) === 'array' || !property_exists($currency, 'name')) {266 $currency = new \stdClass();267 268 $currency->name = $apirone_currency->name;269 $currency->abbr = $apirone_currency->abbr;270 $currency->{'dust-rate'} = $apirone_currency->{'dust-rate'};271 $currency->{'units-factor'} = $apirone_currency->{'units-factor'};272 $currency->address = '';273 $currency->testnet = $apirone_currency->testnet;274 $currency->enabled = 0;275 $currency->valid = 0;276 }277 278 // Set address from config279 if ($_SERVER['REQUEST_METHOD'] == 'POST' && $apirone_account) {280 if (array_key_exists('enabled', $_POST['woocommerce_mccp_currencies'][$currency->abbr])) {281 $currency->enabled = sanitize_text_field($_POST['woocommerce_mccp_currencies'][$currency->abbr]['enabled']);282 }283 $currency->address = sanitize_text_field($_POST['woocommerce_mccp_currencies'][$currency->abbr]['address']);284 if ($currency->address != '') {285 $result = Apirone::setTransferAddress($apirone_account, $apirone_currency->abbr, $currency->address);286 $currency->valid = $result ? 1 : 0;287 }288 }289 return $currency;290 }291 292 public function mccp_currencies() {293 294 $apirone_account = $this->mccp_account();295 $currencies = array();296 297 foreach (Apirone::currencyList() as $apirone_currency) {298 $currencies[$apirone_currency->abbr] = $this->mccp_currency($apirone_currency, $apirone_account);299 }300 301 return $currencies; 302 }68 } 69 70 /** 71 * Save apirone currencies into options table 72 * 73 * @return void 74 */ 75 public function save_currencies() { 76 if ($_SERVER['REQUEST_METHOD'] == 'POST') { 77 $currencies = $this->mccp_currencies(); 78 79 // Remove not valid currencies before save option 80 foreach ($currencies as $key => $value) { 81 if (!empty($value->address) && !$value->valid) { 82 $currencies[$key]->address = ''; 83 $currencies[$key]->enabled = 0; 84 $currencies[$key]->valid = 0; 85 } 86 } 87 $this->update_option('currencies', $currencies); 88 } 89 } 90 91 /** 92 * Generate currencies list options for admin page 93 * 94 * @param mixed $key 95 * @param mixed $data 96 * @return string|false 97 */ 98 public function generate_currencies_list_html ($key, $data) { 99 ob_start(); 100 ?> 101 <tr valign="top"> 102 <th scope="row" class="titledesc"> 103 <label><?php _e('Currencies', 'mccp'); ?><?php echo $this->get_tooltip_html( $data ); // WPCS: XSS ok. ?></label> 104 </th> 105 <td class="forminp" id="mccp-order_states"> 106 <div class="table-wrapper"> 107 <table class="form-table"> 108 <tbody> 109 <?php 110 foreach($this->mccp_currencies() as $currency) : ?> 111 <tr valign="middle" class="single_select_page"> 112 <th scope="row" class="titledesc"> 113 <label for="mccp_<?php echo esc_html( $currency->abbr ); ?>" class="currency-label"> 114 <div class="<?php echo $this->currency_icon_wrapper($currency) ?>"> 115 <img src="<?php echo Apirone::currencyIcon($currency->abbr); ?>" alt="<?php echo esc_html( $currency->name ); ?>" class="currency-icon"> 116 </div> 117 <?php echo esc_html( $currency->name ); ?> 118 119 <?php echo wc_help_tip(sprintf(__('Enter valid address to activate <b>%s</b> currency', 'mccp'), $currency->name)); ?> 120 </label> 121 </th> 122 <td class="forminp"> 123 <input type="text" name="woocommerce_mccp_currencies[<?php echo esc_html( $currency->abbr ); ?>][address]" class="input-text regular-input" 124 value="<?php echo esc_html( $currency->address ); ?>"> 125 <?php if ( $currency->address ) : ?> 126 <input type="checkbox" name="woocommerce_mccp_currencies[<?php echo esc_html( $currency->abbr ); ?>][enabled]" class="currency-enabled" 127 <?php echo $currency->enabled ? ' checked' : ''; ?> 128 <?php echo !$currency->valid ? ' disabled' : ''; ?> 129 > 130 <?php if( $currency->address) : ?> 131 <?php if( $currency->valid ) : ?> 132 <span class="valid"> 133 <?php _e('Use currency', 'mccp'); ?> 134 </span> 135 <?php else: ?> 136 <span class="not-valid"> 137 <?php _e('Address is not valid', 'mccp'); ?> 138 </span> 139 <?php endif;?> 140 <?php endif;?> 141 <?php endif;?> 142 <?php if ($currency->testnet) : ?> 143 <div class="testnet-info"><span class="testnet-info__message"><?php _e('WARNING: Test currency', 'mccp'); ?></span> 144 <?php echo wc_help_tip(__('Use this currency for test purposes only! This currency shown for admin and "test currency customer" (if set) is only on the front end of Woocommerce!')); ?></div> 145 <?php endif; ?> 146 </td> 147 </tr> 148 <?php endforeach; ?> 149 150 </tbody> 151 </table> 152 </div> 153 </td> 154 </tr> 155 156 <?php 157 return ob_get_clean(); 158 } 159 160 /** 161 * Init Gateway Settings Form Fields 162 * 163 * @return void 164 */ 165 public function mccp_init_form_fields() { 166 $this->form_fields = array( 167 'enabled' => array( 168 'title' => __('On/off', 'mccp'), 169 'type' => 'checkbox', 170 'label' => __('On', 'mccp'), 171 'default' => 'no', 172 'description' => __('Activate/deactivate MCCP gateway', 'mccp'), 173 'desc_tip' => true, 174 ), 175 'merchant' => array( 176 'title' => __('Merchant name', 'mccp'), 177 'type' => 'text', 178 'default' => '', 179 'description' => __('Shows Merchant name in the payment order. If this field is blank then Site Title from General settings will be shown.', 'mccp'), 180 'desc_tip' => true, 181 ), 182 'test_customer' => array( 183 'title' => __('Test currency customer', 'mccp'), 184 'type' => 'email', 185 'default' => '', 186 'placeholder' => '[email protected]', 187 'description' => __('Enter an email of the customer to whom the test currencies will be shown.', 'mccp'), 188 'desc_tip' => true, 189 ), 190 'timeout' => array( 191 'title' => __('Payment timeout, sec.', 'mccp'), 192 'type' => 'number', 193 'default' => '1800', 194 'description' => __('The period during which a customer shall pay. Set value in seconds', 'mccp'), 195 'desc_tip' => true, 196 'custom_attributes' => array('min' => 0,), 197 ), 198 'currencies' => array( 199 'type' => 'currencies_list', 200 'description' => __('List of available cryptocurrencies processed by Apirone', 'mccp'), 201 'desc_tip' => true, 202 'default' => [], 203 ), 204 'factor' => array( 205 'title' => __('Price adjustment factor', 'mccp'), 206 'type' => 'number', 207 'default' => '1', 208 'description' => __('If you want to add/substract percent to/from the payment amount, use the following price adjustment factor multiplied by the amount.<br />For example: <br />100% * 0.99 = 99% <br />100% * 1.01 = 101%', 'mccp'), 209 'desc_tip' => true, 210 'custom_attributes' => array('min' => 0.01, 'step' => '0.01', 'required' => 'required'), 211 ), 212 'check_timeout' => array( 213 'title' => __('Status update period, sec.', 'mccp'), 214 'type' => 'number', 215 'default' => '10', 216 'description' => __('How often the invoice status shall be checked. Min value 5 seconds.', 'mccp'), 217 'desc_tip' => true, 218 'custom_attributes' => array('min' => 5, 'required' => 'required'), 219 ), 220 'apirone_logo' => array( 221 'title' => __('Apirone logo', 'mccp'), 222 'type' => 'checkbox', 223 'label' => __('Show Apirone logo on invoice page', 'mccp'), 224 'default' => 'yes', 225 ), 226 'debug' => array( 227 'title' => __('Debug mode', 'mccp'), 228 'type' => 'checkbox', 229 'label' => __('On', 'mccp'), 230 'default' => 'no', 231 'description' => __('Write debug information into log file', 'mccp'), 232 'desc_tip' => true, 233 ), 234 ); 235 } 236 /** 237 * Save apirone currencies into options table 238 */ 239 public function validate_currencies_list_field($k, $v) { 240 $currencies = $this->mccp_currencies(); 241 242 foreach ($currencies as $key => $value) { 243 if (!empty($value->address) && !$value->valid) { 244 $currencies[$key]->address = ''; 245 $currencies[$key]->enabled = 0; 246 $currencies[$key]->valid = 0; 247 } 248 } 249 return $currencies; 250 } 251 252 253 /** 254 * Return css-class for currency icon 255 * 256 * @param mixed $currency 257 * @return string 258 */ 259 public function currency_icon_wrapper($currency) { 260 return 'icon-wrapper' . ((substr_count(strtolower($currency->name), 'testnet') > 0) ? ' test-currency': ''); 261 } 262 263 public function mccp_currency($apirone_currency, $apirone_account = false) { 264 $currency = $this->get_mccp_currency($apirone_currency->abbr); 265 if ($currency == false || gettype($currency) === 'array' || !property_exists($currency, 'name')) { 266 $currency = new \stdClass(); 267 268 $currency->name = $apirone_currency->name; 269 $currency->abbr = $apirone_currency->abbr; 270 $currency->{'dust-rate'} = $apirone_currency->{'dust-rate'}; 271 $currency->{'units-factor'} = $apirone_currency->{'units-factor'}; 272 $currency->address = ''; 273 $currency->testnet = $apirone_currency->testnet; 274 $currency->enabled = 0; 275 $currency->valid = 0; 276 } 277 278 // Set address from config 279 if ($_SERVER['REQUEST_METHOD'] == 'POST' && $apirone_account) { 280 if (array_key_exists('enabled', $_POST['woocommerce_mccp_currencies'][$currency->abbr])) { 281 $currency->enabled = sanitize_text_field($_POST['woocommerce_mccp_currencies'][$currency->abbr]['enabled']); 282 } 283 $currency->address = sanitize_text_field($_POST['woocommerce_mccp_currencies'][$currency->abbr]['address']); 284 if ($currency->address != '') { 285 $result = Apirone::setTransferAddress($apirone_account, $apirone_currency->abbr, $currency->address); 286 $currency->valid = $result ? 1 : 0; 287 } 288 } 289 return $currency; 290 } 291 292 public function mccp_currencies() { 293 294 $apirone_account = $this->mccp_account(); 295 $currencies = array(); 296 297 foreach (Apirone::currencyList() as $apirone_currency) { 298 $currencies[$apirone_currency->abbr] = $this->mccp_currency($apirone_currency, $apirone_account); 299 } 300 301 return $currencies; 302 } 303 303 } -
multi-crypto-currency-payment/trunk/inc/invoice-class.php
r2943825 r3046162 19 19 class WC_MCCP extends WC_Payment_Gateway { 20 20 21 use MCCP_Db, MCCP_Utils, MCCP_Admin;22 23 // Common props24 public $id = 'mccp';25 public $title;26 public $description;27 public $method_title;28 public $method_description;29 30 // Plugin props31 public $enabled = false;32 public $currencies = array();33 public $statuses = array();21 use MCCP_Db, MCCP_Utils, MCCP_Admin; 22 23 // Common props 24 public $id = 'mccp'; 25 public $title; 26 public $description; 27 public $method_title; 28 public $method_description; 29 30 // Plugin props 31 public $enabled = false; 32 public $currencies = array(); 33 public $statuses = array(); 34 34 public $errors_count = 0; 35 35 36 public function __construct() {37 $this->update();38 $this->title = __('Crypto currency payment', 'mccp');39 $this->description = __('Start accepting multi cryptocurrency payments', 'mccp');40 $this->method_title = __('Multi Crypto Currency Payments', 'mccp');41 $this->method_description = __('Start accepting multi cryptocurrency payments', 'mccp');42 43 $this->mccp_init_form_fields();44 45 $this->enabled = $this->get_option('enabled');46 $this->currencies = $this->get_option('currencies');36 public function __construct() { 37 $this->update(); 38 $this->title = __('Crypto currency payment', 'mccp'); 39 $this->description = __('Start accepting multi cryptocurrency payments', 'mccp'); 40 $this->method_title = __('Multi Crypto Currency Payments', 'mccp'); 41 $this->method_description = __('Start accepting multi cryptocurrency payments', 'mccp'); 42 43 $this->mccp_init_form_fields(); 44 45 $this->enabled = $this->get_option('enabled'); 46 $this->currencies = $this->get_option('currencies'); 47 47 48 48 $debug = $this->get_option('debug') == 'yes' ? true : false; … … 50 50 Apirone::setLogger(new \WC_Logger(), $debug); 51 51 52 add_action('woocommerce_receipt_mccp', array( $this, 'invoice_receipt' )); 53 add_action('before_woocommerce_pay', array( $this, 'order_pay_success_redirect' )); 54 55 //Save our GW Options into Woocommerce 56 add_action('woocommerce_update_options_payment_gateways_mccp', array($this, 'process_admin_options')); 57 add_action('woocommerce_update_options_payment_gateways_mccp', array($this, 'save_currencies')); 58 59 add_action('woocommerce_api_mccp_callback', array($this, 'mccp_callback_handler')); 60 add_action('woocommerce_api_mccp_check', array($this, 'mccp_check_handler')); 61 62 add_action( 'woocommerce_admin_order_data_after_billing_address', array($this, 'show_invoice_admin_info')); 63 64 } 65 66 /** 67 * Payment process handler 68 * 69 * @param int $order_id 70 * @return array 71 */ 72 function process_payment($order_id) { 73 $order = new WC_Order($order_id); 74 $order_pay = $this->is_repayment() ? 'order-pay' : 'order'; 75 // Create redirect URL 76 $redirect = get_permalink(wc_get_page_id('pay')); 77 $redirect = add_query_arg($order_pay, $order->id, $redirect); 78 $redirect = add_query_arg('key', $order->order_key, $redirect); 79 $redirect = add_query_arg('mccp_currency', sanitize_text_field($_POST['mccp_currency']), $redirect); 80 81 return array( 82 'result' => 'success', 83 'redirect' => $redirect, 84 ); 85 } 86 87 /** 88 * Handle payment provider callback 89 * @return never 90 */ 91 function mccp_callback_handler () { 52 add_action('woocommerce_receipt_mccp', array( $this, 'invoice_receipt' )); 53 // add_action('before_woocommerce_pay', array( $this, 'before_woocommerce_pay_mccp' )); 54 55 //Save our GW Options into Woocommerce 56 add_action('woocommerce_update_options_payment_gateways_mccp', array($this, 'process_admin_options')); 57 add_action('woocommerce_update_options_payment_gateways_mccp', array($this, 'save_currencies')); 58 59 add_action('woocommerce_api_mccp_callback', array($this, 'mccp_callback_handler')); 60 add_action('woocommerce_api_mccp_check', array($this, 'mccp_check_handler')); 61 62 add_action( 'woocommerce_admin_order_data_after_billing_address', array($this, 'show_invoice_admin_info')); 63 64 } 65 66 /** 67 * Payment process handler 68 * 69 * @param int $order_id 70 * @return array 71 */ 72 public function process_payment($order_id) { 73 $order = new WC_Order($order_id); 74 // Create redirect URL 75 $redirect = $order->get_checkout_payment_url(true); 76 $redirect = add_query_arg('mccp_currency', sanitize_text_field($_POST['mccp_currency']), $redirect); 77 if (isset($_GET['pay_for_order'])) { 78 $redirect = add_query_arg('repayment', true, $redirect); 79 } 80 // pa($redirect); 81 // exit; 82 return array( 83 'result' => 'success', 84 'redirect' => $redirect, 85 ); 86 } 87 88 /** 89 * Handle payment provider callback 90 * @return never 91 */ 92 public function mccp_callback_handler () { 92 93 $data = file_get_contents('php://input'); 93 94 if($data) { … … 106 107 LoggerWrapper::callbackError($msg); 107 108 wp_send_json($msg, 400); 108 return; 109 return; 109 110 } 110 111 … … 128 129 } 129 130 130 $invoice_updated = Apirone::invoiceInfoPublic($invoice->invoice);131 132 if($invoice_updated) {133 $order = new WC_Order($invoice->order_id);134 WC_MCCP::invoice_update($order, $invoice_updated);135 }136 else {137 $msg = "Can't get invoice info";138 wp_send_json($msg, 400);139 }140 141 exit;142 }143 144 /**145 * Invoice status check146 * @return echo status value147 */148 function mccp_check_handler () {131 $invoice_updated = Apirone::invoiceInfoPublic($invoice->invoice); 132 133 if($invoice_updated) { 134 $order = new WC_Order($invoice->order_id); 135 WC_MCCP::invoice_update($order, $invoice_updated); 136 } 137 else { 138 $msg = "Can't get invoice info"; 139 wp_send_json($msg, 400); 140 } 141 142 exit; 143 } 144 145 /** 146 * Invoice status check 147 * @return echo status value 148 */ 149 public function mccp_check_handler () { 149 150 $id = array_key_exists('id', $_GET) ? sanitize_text_field($_GET['id']) : false; 150 if($id) { 151 $invoice = WC_MCCP::get_invoice($id); 152 echo Payment::invoiceStatus($invoice); 153 } 154 exit; 155 } 156 157 function order_pay_success_redirect() { 158 global $wp; 159 if (isset($_GET['order_pay']) && isset($_GET['key'])) { 160 $order_id = $wp->query_vars['order_pay']; 161 $order_key = isset( $_GET['key'] ) ? wc_clean( wp_unslash( $_GET['key'] ) ) : ''; // WPCS: input var ok, CSRF ok. 151 if($id) { 152 $invoice = WC_MCCP::get_invoice($id); 153 echo Payment::invoiceStatus($invoice); 154 } 155 exit; 156 } 157 158 public function before_woocommerce_pay_mccp() { 159 if (isset($_GET['key'])) { 160 $order_id = get_query_var('order-pay'); 162 161 $order = wc_get_order( $order_id ); 163 162 if (!$order || $order->get_payment_method() !== 'mccp') { 163 pa(__FUNCTION__ . ' ' . __LINE__ . ' return'); 164 164 return; 165 165 } 166 166 167 $invoices_list= $this->get_order_invoices($order_id);168 $order_invoice = is_array($invoices_list) ? $invoices_list[0] : false;169 170 if ($order-> status!== 'pending' && $order_invoice && $order_invoice->status == 'completed') {171 // Make redirect to order_re seived167 $invoices_list= $this->get_order_invoices($order_id); 168 $order_invoice = is_array($invoices_list) ? $invoices_list[0] : false; 169 170 if ($order->get_status() !== 'pending' && $order_invoice && $order_invoice->status == 'completed') { 171 // Make redirect to order_received 172 172 $args = array( 173 'order-received' => $order->id, 174 'key' => $order->order_key, 175 // 'mccp_currency' => $crypto_abbr, 176 // 'invoice' => $created->invoice, 173 'key' => $order->get_order_key(), 177 174 ); 178 175 179 $url = add_query_arg($args, get_permalink(wc_get_page_id('pay')));176 $url = add_query_arg($args, $order->get_checkout_order_received_url()); 180 177 wp_redirect($url); // Redirect to payment page 181 178 exit(); … … 184 181 } 185 182 186 /** 187 * Invoice page handler 188 * 189 * @param mixed $order_id 190 * @return mixed 191 * @throws Exception 192 */ 193 function invoice_receipt($order_id) { 194 if( $this->is_available() === false ) 195 return _e("Payment method disabled", 'mccp'); 196 197 $crypto_abbr = array_key_exists('mccp_currency', $_GET) ? sanitize_text_field($_GET['mccp_currency']) : false; 198 199 if (!$crypto_abbr) 200 return _e('Required param not exists', 'mccp'); 201 202 $_currency = $this->currencies[$crypto_abbr]; 203 204 // Process some errors 205 if (empty($_currency->address)) 206 return WC_MCCP::receipt_error_message('not-supported', $crypto_abbr); 207 208 if (!$_currency->valid) 209 return WC_MCCP::receipt_error_message('not-valid', $crypto_abbr); 210 211 if (!$_currency->enabled) 212 return WC_MCCP::receipt_error_message('not-enabled', $crypto_abbr); 213 214 $order = new WC_Order($order_id); 215 216 $woo_currency = get_woocommerce_currency(); 217 $crypto_total = $this->get_crypto_total($order->get_total(), $woo_currency, $crypto_abbr); 218 219 if ($crypto_total == 0) 220 return _e(sprintf('Can\'t convert your paynemt to %s', $crypto_abbr), 'mccp'); 221 if (Apirone::cur2min($crypto_total, $_currency->{'units-factor'}) < $_currency->{'dust-rate'}) 222 return _e('Your payment less than minimal payment value'); 223 224 $invoices_list= $this->get_order_invoices($order_id); 225 $order_invoice = is_array($invoices_list) ? $invoices_list[0] : false; 226 227 // Process failed order_pay 228 if ( $this->is_repayment() && ! isset($_GET['invoice'])) { 229 if ($order_invoice && $order_invoice->status == 'expired' && $order->get_status() === 'failed') { 230 $created = $this->invoice_create($order, $crypto_abbr, $crypto_total); 231 // Save invoice 232 if ($created) { 233 $this->invoice_update($order, $created); 234 // Make redirect to payment page 235 $args = array( 236 'order-pay' => $order->id, 237 'key' => $order->order_key, 238 'mccp_currency' => $crypto_abbr, 239 'invoice' => $created->invoice, 240 ); 241 242 $url = add_query_arg($args, get_permalink(wc_get_page_id('pay'))); 243 wp_redirect($url); // Redirect to payment page 244 exit(); 245 } 246 } 247 } 248 249 if ($order_invoice) { 183 /** 184 * Invoice page handler 185 * 186 * @param mixed $order_id 187 * @return mixed 188 * @throws Exception 189 */ 190 public function invoice_receipt($order_id) { 191 if( $this->is_available() === false ) 192 return _e("Payment method disabled", 'mccp'); 193 194 $crypto_abbr = array_key_exists('mccp_currency', $_GET) ? sanitize_text_field($_GET['mccp_currency']) : false; 195 196 if (!$crypto_abbr) 197 return _e('Required param not exists', 'mccp'); 198 199 $_currency = $this->currencies[$crypto_abbr]; 200 201 // Process some errors 202 if (empty($_currency->address)) 203 return WC_MCCP::receipt_error_message('not-supported', $crypto_abbr); 204 205 if (!$_currency->valid) 206 return WC_MCCP::receipt_error_message('not-valid', $crypto_abbr); 207 208 if (!$_currency->enabled) 209 return WC_MCCP::receipt_error_message('not-enabled', $crypto_abbr); 210 211 $order = new WC_Order($order_id); 212 213 $woo_currency = get_woocommerce_currency(); 214 $crypto_total = $this->get_crypto_total($order->get_total(), $woo_currency, $crypto_abbr); 215 216 if ($crypto_total == 0) 217 return _e(sprintf('Can\'t convert your payment to %s', $crypto_abbr), 'mccp'); 218 if (Apirone::cur2min($crypto_total, $_currency->{'units-factor'}) < $_currency->{'dust-rate'}) 219 return _e('Your payment less than minimal payment value'); 220 221 $invoices_list= $this->get_order_invoices($order_id); 222 $order_invoice = is_array($invoices_list) ? $invoices_list[0] : false; 223 224 if ( isset($_GET['repayment']) && $order_invoice) { 225 $created = null; 226 227 // Create new invoice if expired; 228 if ($order_invoice->status == 'expired' && $order->get_status() === 'failed') { 229 $created = $this->invoice_create($order, $crypto_abbr, $crypto_total); 230 } 231 232 // Create new invoice if total or currency changed (invoice not expired) 233 if (in_array($order_invoice->status, ['created', 'partpaid'])) { 234 $details = $order_invoice->details; 235 $amount = (int) Apirone::cur2min($crypto_total, $_currency->{'units-factor'}); 236 if ($details->amount !== (int) $amount || $details->currency !== $crypto_abbr) { 237 $created = $this->invoice_create($order, $crypto_abbr, $crypto_total); 238 } 239 } 240 241 // Save invoice & redirect to payment page 242 if ($created) { 243 $this->invoice_update($order, $created); 244 // Make redirect to payment page 245 $args = array( 246 'key' => $order->get_order_key(), 247 'mccp_currency' => $crypto_abbr, 248 ); 249 $url = add_query_arg($args, $order->get_checkout_payment_url(true)); 250 wp_redirect($url); // Redirect to payment page 251 exit(); 252 } 253 254 } 255 256 if ($order_invoice) { 250 257 if (Payment::invoiceStatus($order_invoice) != 0) { 251 258 $invoice_data = Apirone::invoiceInfoPublic($order_invoice->invoice); … … 255 262 } 256 263 } 257 }258 else {259 $created = $this->invoice_create($order, $crypto_abbr, $crypto_total);260 261 if ($created) {262 $order_invoice = $this->invoice_update($order, $created);263 }264 }265 266 if (!$order_invoice) {267 ?>268 <h2>Oops! Something went wrong.</h2>269 <p>Please, try again or choose another payment method.</p>270 <p><a href="<?php echo esc_url( $order->get_checkout_payment_url() ); ?>" class="button pay"><?php esc_html_e( 'Pay', 'woocommerce' ); ?></a></p>271 <?php272 273 return; 274 }264 } 265 else { 266 $created = $this->invoice_create($order, $crypto_abbr, $crypto_total); 267 268 if ($created) { 269 $order_invoice = $this->invoice_update($order, $created); 270 } 271 } 272 273 if (!$order_invoice) { 274 ?> 275 <h2>Oops! Something went wrong.</h2> 276 <p>Please, try again or choose another payment method.</p> 277 <p><a href="<?php echo esc_url( $order->get_checkout_payment_url() ); ?>" class="button pay"><?php esc_html_e( 'Pay', 'woocommerce' ); ?></a></p> 278 <?php 279 280 return; 281 } 275 282 276 if (!$this->is_repayment() && ($order_invoice->status === 'completed' || $order_invoice->status === 'expired')) { 277 WC()->cart->empty_cart(); 278 } 279 $currencyInfo = Apirone::getCurrency($order_invoice->details->currency); 280 $this->invoice_show($order_invoice, $currencyInfo); 283 $currencyInfo = Apirone::getCurrency($order_invoice->details->currency); 284 $this->invoice_show($order, $order_invoice, $currencyInfo); 281 285 282 286 if ($order_invoice->status == 'expired' && $order->get_status() === 'failed') { 283 287 wc_get_template( 'checkout/thankyou.php', array( 'order' => $order ) ); 284 288 } 285 }286 287 function invoice_create($order, $crypto_abbr, $crypto_total) {288 $_callback = site_url() . '?wc-api=mccp_callback&id=' . Payment::makeInvoiceSecret($this->mccp_secret(), $order->id);289 $_currency = $this->currencies[$crypto_abbr];290 291 $invoiceData = Payment::makeInvoiceData(292 $crypto_abbr,293 (int)Apirone::cur2min($crypto_total, $_currency->{'units-factor'}),294 (int) $this->get_option('timeout'),295 $_callback,296 $order->get_total(),297 $order->get_currency()298 );299 300 return Apirone::invoiceCreate($this->mccp_account(), $invoiceData);301 }302 303 function invoice_show($invoice, &$currency) {304 $check_timeout = $this->get_option('check_timeout');289 } 290 291 public function invoice_create($order, $crypto_abbr, $crypto_total) { 292 $_callback = site_url() . '?wc-api=mccp_callback&id=' . Payment::makeInvoiceSecret($this->mccp_secret(), $order->get_id()); 293 $_currency = $this->currencies[$crypto_abbr]; 294 295 $invoiceData = Payment::makeInvoiceData( 296 $crypto_abbr, 297 (int)Apirone::cur2min($crypto_total, $_currency->{'units-factor'}), 298 (int) $this->get_option('timeout'), 299 $_callback, 300 $order->get_total(), 301 $order->get_currency() 302 ); 303 $invoiceData->linkback = $order->get_checkout_order_received_url(); 304 return Apirone::invoiceCreate($this->mccp_account(), $invoiceData); 305 } 306 307 public function invoice_show(&$order, &$invoice, &$currency) { 308 $check_timeout = $this->get_option('check_timeout'); 305 309 $merchant = $this->get_option('merchant') ? $this->get_option('merchant') : get_bloginfo(); 306 $backlink = $this->get_option('backlink') ? $this->get_option('backlink') : site_url();307 $logo = $this->get_option('apirone_logo') == 'yes' ? Apirone::getAssets('logo.svg') : false;310 $backlink = $order->needs_payment() ? $order->get_checkout_payment_url() : $order->get_checkout_order_received_url(); 311 $logo = $this->get_option('apirone_logo') == 'yes' ? Apirone::getAssets('logo.svg') : false; 308 312 309 313 $statusLink = site_url() . '?wc-api=mccp_check&id=' . $invoice->invoice; … … 311 315 echo Payment::invoice($invoice, $currency, $statusLink, $check_timeout, $merchant, $backlink, $logo); 312 316 313 }317 } 314 318 } -
multi-crypto-currency-payment/trunk/inc/invoice-db-trait.php
r2946588 r3046162 6 6 trait MCCP_Db { 7 7 8 /**9 * Return order invoces list sort by time descending10 *11 * @param mixed $order_id12 * @return void13 */14 public static function get_order_invoices ($order_id) {15 global $wpdb, $table_prefix;16 17 $result = $wpdb->get_results(\ApironeApi\Db::getOrderInvoiceQuery($order_id, $table_prefix), ARRAY_A);18 if ($result) {19 foreach ($result as $key => $invoice) {20 $result[$key]['details'] = json_decode($invoice['details']);21 $result[$key]['meta'] = unserialize($invoice['meta']);22 }23 24 return json_decode(json_encode($result));25 }26 27 if($wpdb->last_error !== '') { 28 LoggerWrapper::error($wpdb->last_error); 29 } 30 31 return false; 32 }33 34 /**35 * Return invoice object by invoice ID36 *37 * @param mixed $invoice_id38 * @return mixed39 */40 public static function get_invoice ($invoice_id) {41 global $wpdb, $table_prefix;42 43 $result = $wpdb->get_results(\ApironeApi\Db::getInvoiceQuery($invoice_id, $table_prefix), ARRAY_A);44 45 if ($result) {46 $invoice = $result[0];47 $invoice['details'] = json_decode($invoice['details']);48 $invoice['meta'] = unserialize($invoice['meta']);49 50 return json_decode(json_encode($invoice));51 }52 if($wpdb->last_error !== '') { 53 LoggerWrapper::error($wpdb->last_error); 54 } 55 56 return false; 57 }58 59 /**60 * Invoice record update61 *62 * @param mixed $order63 * @param mixed $obj_invoice64 * @return mixed65 */8 /** 9 * Return order invoces list sort by time descending 10 * 11 * @param mixed $order_id 12 * @return void 13 */ 14 public static function get_order_invoices ($order_id) { 15 global $wpdb, $table_prefix; 16 17 $result = $wpdb->get_results(\ApironeApi\Db::getOrderInvoiceQuery($order_id, $table_prefix), ARRAY_A); 18 if ($result) { 19 foreach ($result as $key => $invoice) { 20 $result[$key]['details'] = json_decode($invoice['details']); 21 $result[$key]['meta'] = unserialize($invoice['meta']); 22 } 23 24 return json_decode(json_encode($result)); 25 } 26 27 if($wpdb->last_error !== '') { 28 LoggerWrapper::error($wpdb->last_error); 29 } 30 31 return false; 32 } 33 34 /** 35 * Return invoice object by invoice ID 36 * 37 * @param mixed $invoice_id 38 * @return mixed 39 */ 40 public static function get_invoice ($invoice_id) { 41 global $wpdb, $table_prefix; 42 43 $result = $wpdb->get_results(\ApironeApi\Db::getInvoiceQuery($invoice_id, $table_prefix), ARRAY_A); 44 45 if ($result) { 46 $invoice = $result[0]; 47 $invoice['details'] = json_decode($invoice['details']); 48 $invoice['meta'] = unserialize($invoice['meta']); 49 50 return json_decode(json_encode($invoice)); 51 } 52 if($wpdb->last_error !== '') { 53 LoggerWrapper::error($wpdb->last_error); 54 } 55 56 return false; 57 } 58 59 /** 60 * Invoice record update 61 * 62 * @param mixed $order 63 * @param mixed $obj_invoice 64 * @return mixed 65 */ 66 66 public static function invoice_update($order, $obj_invoice) { 67 global $wpdb, $table_prefix;67 global $wpdb, $table_prefix; 68 68 69 69 $params = array(); 70 70 $invoice = self::get_invoice($obj_invoice->invoice); 71 71 72 if ($invoice) {73 $params['invoice'] = $obj_invoice->invoice;74 $params['status'] = $obj_invoice->status;75 $params['details'] = $obj_invoice;76 77 $result = $wpdb->query(ApironeApi\Db::updateInvoiceQuery($params, $table_prefix));78 }79 else {80 $_details = Apirone::invoiceInfoPublic($obj_invoice->invoice);81 $params['order_id'] = $order->get_id();82 $params['account'] = $obj_invoice->account;83 $params['invoice'] = $obj_invoice->invoice;84 $params['status'] = $obj_invoice->status;85 $params['details'] = $_details ? $_details : $obj_invoice; // fallback if service return false72 if ($invoice) { 73 $params['invoice'] = $obj_invoice->invoice; 74 $params['status'] = $obj_invoice->status; 75 $params['details'] = $obj_invoice; 76 77 $result = $wpdb->query(ApironeApi\Db::updateInvoiceQuery($params, $table_prefix)); 78 } 79 else { 80 $_details = Apirone::invoiceInfoPublic($obj_invoice->invoice); 81 $params['order_id'] = $order->get_id(); 82 $params['account'] = $obj_invoice->account; 83 $params['invoice'] = $obj_invoice->invoice; 84 $params['status'] = $obj_invoice->status; 85 $params['details'] = $_details ? $_details : $obj_invoice; // fallback if service return false 86 86 87 87 $result = $wpdb->query(ApironeApi\Db::insertInvoiceQuery($params, $table_prefix)); 88 }89 90 if ($result) {91 $savedInvoice = self::get_invoice($obj_invoice->invoice);92 93 WC_MCCP::order_status_update($order, $savedInvoice);94 95 return $savedInvoice;96 }97 98 if($wpdb->last_error !== '') { 99 LoggerWrapper::error($wpdb->last_error); 100 } 101 102 return false;103 } 104 105 /**106 * Order status update107 *108 * @param mixed $order109 * @param mixed $invoice110 * @return void111 */112 public static function order_status_update($order, $invoice) {113 $last_status = WC_MCCP::get_invoice_meta($invoice->invoice, 'order-status');114 $cur_status = $order->get_status();115 $new_status = WC_MCCP::get_order_status_by_invoice($invoice);116 117 // Set status for new invoice118 if ($last_status === false && $new_status == 'pending') {119 if ($cur_status == 'pending') {120 WC_MCCP::add_invoice_meta($invoice->invoice, 'order-status', $new_status);121 }122 if ($cur_status == 'failed') {123 $order->update_status('wc-' . $new_status);124 WC_MCCP::add_invoice_meta($invoice->invoice, 'order-status', $new_status);125 }126 return;127 }128 129 if($last_status == $cur_status && $last_status != $new_status) {130 $order->update_status('wc-' . $new_status);131 WC_MCCP::update_invoice_meta($invoice->invoice, 'order-status', $new_status);132 }133 134 return; 135 }136 137 /**138 * Returm invoce meta139 * @param mixed $invoice_id140 * @param bool $name141 * @return mixed142 */143 public static function get_invoice_meta($invoice_id, $name = false) {144 global $wpdb, $table_prefix;145 146 $res = $wpdb->get_row(ApironeApi\Db::getInvoiceMetadataQuery($invoice_id, $table_prefix));147 $meta = unserialize($res->meta);148 $meta = (!$meta) ? new stdClass() : $meta;149 150 if ($name) {151 return property_exists($meta, $name) ? $meta->$name : false;152 }153 154 return $meta;155 }156 157 /**158 * Add meta to invoice159 *160 * @param mixed $invoice_id161 * @param mixed $name162 * @param mixed $value163 * @return mixed164 */165 public static function add_invoice_meta($invoice_id, $name, $value) {166 global $wpdb, $table_prefix;167 168 $meta = WC_MCCP::get_invoice_meta($invoice_id);169 170 $meta->$name = $value;171 172 $result= $wpdb->query(ApironeApi\Db::setInvoiceMetadataQuery($invoice_id, $meta, $table_prefix));173 if ($result) {174 return $meta;175 }176 177 if($wpdb->last_error !== '') { 178 LoggerWrapper::error($wpdb->last_error); 179 } 180 181 return $result;182 }183 184 /**185 * Alias for add_invoice_meta()186 *187 * @param mixed $invoice_id188 * @param mixed $name189 * @param mixed $value190 * @return mixed191 */192 public static function update_invoice_meta($invoice_id, $name, $value) {193 return WC_MCCP::add_invoice_meta($invoice_id, $name, $value);194 }195 196 /**197 * delete invoice meta by name198 * @param mixed $invoice_id199 * @param mixed $name200 * @return mixed201 */202 public static function delete_invoice_meta($invoice_id, $name) {203 global $wpdb, $table_prefix;204 205 $meta = WC_MCCP::get_invoice_meta($invoice_id);206 unset($meta->{$name});207 208 $result= $wpdb->query(ApironeApi\Db::setInvoiceMetadataQuery($invoice_id, $meta, $table_prefix));209 if ($result) {210 return $meta;211 }212 213 if($wpdb->last_error !== '') { 214 LoggerWrapper::error($wpdb->last_error); 215 } 216 217 return $result;218 219 }88 } 89 90 if ($result) { 91 $savedInvoice = self::get_invoice($obj_invoice->invoice); 92 93 WC_MCCP::order_status_update($order, $savedInvoice); 94 95 return $savedInvoice; 96 } 97 98 if($wpdb->last_error !== '') { 99 LoggerWrapper::error($wpdb->last_error); 100 } 101 102 return false; 103 } 104 105 /** 106 * Order status update 107 * 108 * @param mixed $order 109 * @param mixed $invoice 110 * @return void 111 */ 112 public static function order_status_update($order, $invoice) { 113 $last_status = WC_MCCP::get_invoice_meta($invoice->invoice, 'order-status'); 114 $cur_status = $order->get_status(); 115 $new_status = WC_MCCP::get_order_status_by_invoice($invoice); 116 117 // Set status for new invoice 118 if ($last_status === false && $new_status == 'pending') { 119 if ($cur_status == 'pending') { 120 WC_MCCP::add_invoice_meta($invoice->invoice, 'order-status', $new_status); 121 } 122 if ($cur_status == 'failed') { 123 $order->update_status('wc-' . $new_status); 124 WC_MCCP::add_invoice_meta($invoice->invoice, 'order-status', $new_status); 125 } 126 return; 127 } 128 129 if($last_status == $cur_status && $last_status != $new_status) { 130 $order->update_status('wc-' . $new_status); 131 WC_MCCP::update_invoice_meta($invoice->invoice, 'order-status', $new_status); 132 } 133 134 return; 135 } 136 137 /** 138 * Returm invoce meta 139 * @param mixed $invoice_id 140 * @param bool $name 141 * @return mixed 142 */ 143 public static function get_invoice_meta($invoice_id, $name = false) { 144 global $wpdb, $table_prefix; 145 146 $res = $wpdb->get_row(ApironeApi\Db::getInvoiceMetadataQuery($invoice_id, $table_prefix)); 147 $meta = unserialize($res->meta); 148 $meta = (!$meta) ? new stdClass() : $meta; 149 150 if ($name) { 151 return property_exists($meta, $name) ? $meta->$name : false; 152 } 153 154 return $meta; 155 } 156 157 /** 158 * Add meta to invoice 159 * 160 * @param mixed $invoice_id 161 * @param mixed $name 162 * @param mixed $value 163 * @return mixed 164 */ 165 public static function add_invoice_meta($invoice_id, $name, $value) { 166 global $wpdb, $table_prefix; 167 168 $meta = WC_MCCP::get_invoice_meta($invoice_id); 169 170 $meta->$name = $value; 171 172 $result = $wpdb->query(ApironeApi\Db::setInvoiceMetadataQuery($invoice_id, $meta, $table_prefix)); 173 if ($result) { 174 return $meta; 175 } 176 177 if($wpdb->last_error !== '') { 178 LoggerWrapper::error($wpdb->last_error); 179 } 180 181 return $result; 182 } 183 184 /** 185 * Alias for add_invoice_meta() 186 * 187 * @param mixed $invoice_id 188 * @param mixed $name 189 * @param mixed $value 190 * @return mixed 191 */ 192 public static function update_invoice_meta($invoice_id, $name, $value) { 193 return WC_MCCP::add_invoice_meta($invoice_id, $name, $value); 194 } 195 196 /** 197 * delete invoice meta by name 198 * @param mixed $invoice_id 199 * @param mixed $name 200 * @return mixed 201 */ 202 public static function delete_invoice_meta($invoice_id, $name) { 203 global $wpdb, $table_prefix; 204 205 $meta = WC_MCCP::get_invoice_meta($invoice_id); 206 unset($meta->{$name}); 207 208 $result = $wpdb->query(ApironeApi\Db::setInvoiceMetadataQuery($invoice_id, $meta, $table_prefix)); 209 if ($result) { 210 return $meta; 211 } 212 213 if($wpdb->last_error !== '') { 214 LoggerWrapper::error($wpdb->last_error); 215 } 216 217 return $result; 218 219 } 220 220 221 221 public static function is_table_exists() { -
multi-crypto-currency-payment/trunk/inc/invoice-utils-trait.php
r2920820 r3046162 10 10 trait MCCP_Utils { 11 11 12 /** 13 * Generate receipt error html 14 * 15 * @param mixed $reason 16 * @param string $sub 17 * @return void 18 */ 19 static public function receipt_error_message($reason, $sub = '') { 20 21 switch ($reason) { 22 case 'disabled': 23 $message = __('Payment method disabled', 'mccp'); 24 break; 25 case 'required': 26 $message = __('Required param not exists', 'mccp'); 27 break; 28 case 'not-supported': 29 $message = __(sprintf('Currency \'%s\' is not supported', $sub), 'mccp'); 30 break; 31 case 'not-valid': 32 $message = __(sprintf('Currency \'%s\' has no valid address', $sub), 'mccp'); 33 break; 34 case 'not-enabled': 35 $message = __(sprintf('Currency \'%s\' is not enabled', $sub), 'mccp'); 36 break; 37 } 38 39 echo '<div class="receipt_info">' . esc_html( $message ) . '</div>'; 40 } 41 42 /** 43 * Return woocommerce order status depends on invoice status 44 * 45 * @param mixed $invoice 46 * @return mixed 47 */ 48 public static function get_order_status_by_invoice($invoice) { 49 switch ($invoice->status) { 50 case 'created': 51 case 'partpaid': 52 case 'paid': 53 case 'overpaid': 54 $status = 'pending'; 55 break; 56 case 'completed': 57 $status = 'processing'; 58 break; 59 case 'expired': 60 $status = 'failed'; 61 } 62 return $status; 63 } 64 65 /** 66 * Get account. 67 * Create new account when option 'woocommerce_mccp_account' not exist 68 * 69 * @return object|false 70 */ 71 public function mccp_account($renew = false) { 72 $account = get_option('woocommerce_mccp_account'); 73 74 if ( !$account || $renew ) { 75 $account = Apirone::accountCreate(); 76 update_option('woocommerce_mccp_account', $account); 77 } 78 return $account; 79 } 80 81 /** 82 * Get secret. 83 * Create new secret when option 'woocommerce_mccp_secret' not exist 84 * 85 * @return object|false 86 */ 87 88 public function mccp_secret($renew = false) { 89 $secret = get_option('woocommerce_mccp_secret'); 90 91 if ( !$secret || $renew ) { 92 $secret = md5(time()); 93 update_option('woocommerce_mccp_secret', $secret); 94 } 95 return $secret; 96 } 97 98 /** 99 * Return currency object by currency abbr or false if not exist 100 * 101 * @param mixed $abbr currency abbr - btc, doge, etc 102 * @return false|object 103 */ 104 public function get_mccp_currency($abbr) { 105 $currencies = (array) $this->get_option('currencies'); 106 107 if ( $currencies && array_key_exists($abbr, $currencies) ) { 108 return $currencies[$abbr]; 109 } 110 111 return false; 112 } 113 114 public function get_crypto_total($value, $currency, $abbr) { 115 $factor = $this->get_option('factor'); 116 117 return Apirone::fiat2crypto($value * $factor, $currency, $abbr); 118 } 119 120 /** 121 * Generate MCCP payment fields 122 * 123 * @return void 124 */ 125 function payment_fields() { 126 if ($this->is_repayment()) { 127 $order_id = $this->is_repayment(); 128 $order = wc_get_order( $order_id ); 129 $total = $order->get_total(); 130 } 131 else { 132 $total = WC()->cart->total; 133 } 134 135 $woo_currency = get_woocommerce_currency(); 136 $active_currencies = array(); 137 138 foreach ((array) $this->get_option('currencies') as $item) { 139 if ($item->testnet === 1 && !current_user_can('manage_options')) { 140 continue; 141 } 142 143 if (!empty($item->address) && $item->enabled && $item->valid) { 144 $currency = array(); 145 146 $crypto_total = Apirone::exp2dec($this->get_crypto_total($total, $woo_currency, $item->abbr), 0); 147 148 $currency['abbr'] = $item->abbr; 149 $currency['name'] = $item->name; 150 $currency['total'] = $crypto_total; 151 $currency['dust-rate'] = Apirone::exp2dec(Apirone::min2cur($item->{'dust-rate'}, $item->{'units-factor'}), 0); 152 $currency['payable'] = $currency['total'] >= $currency['dust-rate'] ? true : false; 153 $active_currencies[] = $currency; 154 } 155 } 156 // If plugin active but haven't active currencies 157 if ( empty($active_currencies)) { 158 _e('Cryptocurrency payment temporary unavailable. Choose other payment method.', 'mccp'); 159 return; 160 } 161 ?> 162 <select id="mccp_currency" name="mccp_currency"> 163 164 <?php foreach ( $active_currencies as $currency ) : ?> 165 166 <option <?php echo (!$currency['payable']) ? 'disabled' : ''; ?> value="<?php echo esc_html($currency['abbr']); ?>"> 167 <?php echo esc_html($currency['name']); ?>: 168 <?php echo esc_html($currency['total']); ?> 169 </option> 170 171 <?php endforeach; ?> 172 173 </select> 174 <?php 175 } 176 177 function is_repayment () { 178 global $wp; 179 return array_key_exists('order-pay', $wp->query_vars) ? $wp->query_vars['order-pay'] : false; 180 } 181 182 /** 183 * Get plugin version 184 * 185 * @return false|string 186 */ 187 function version() { 188 $settings = get_option('woocommerce_mccp_settings', array()); 189 return array_key_exists('version', $settings) ? $settings['version'] : false; 190 } 191 192 /** 193 * Update plugin version only 194 * 195 * @return void 196 */ 197 function upd_version($new) { 198 $settings = get_option('woocommerce_mccp_settings'); 199 $settings['version'] = $new; 200 201 update_option('woocommerce_mccp_settings', $settings); 202 } 203 204 /** 205 * Plugin version update entry point 206 * 207 * @return void 208 */ 209 function update() { 210 $this->update_1_0_0__1_1_0(); 211 $this->update_1_1_0__1_1_1(); 212 $this->update_1_1_1__1_2_0(); 213 $this->update_1_2_0__1_2_1(); 214 $this->update_1_2_1__1_2_2(); 215 $this->update_1_2_2__1_2_3(); 216 } 217 218 /** 219 * Update plugin from 1.2.1 to 1.2.2 220 * Fix mobile layout 221 * @return void 222 */ 223 function update_1_2_2__1_2_3() { 224 if ($this->version() !== '1.2.2') { 225 return; 226 } 227 $this->upd_version('1.2.3'); 228 } 229 230 /** 231 * Update plugin from 1.2.1 to 1.2.2 232 * Fix mobile layout 233 * @return void 234 */ 235 function update_1_2_1__1_2_2() { 236 if ($this->version() !== '1.2.1') { 237 return; 238 } 239 $this->upd_version('1.2.2'); 240 } 241 242 /** 243 * Update plugin from 1.2.0 to 1.2.1 244 * Add a message when the invoice isn't created/found. 245 * @return void 246 */ 247 function update_1_2_0__1_2_1() { 248 if ($this->version() !== '1.2.0') { 249 return; 250 } 251 $this->upd_version('1.2.1'); 252 } 253 254 /** 255 * Update plugin from 1.1.1 to 1.2.0 256 * Fee plan update to percentage 257 * @return void 258 */ 259 function update_1_1_1__1_2_0() { 260 if ($this->version() !== '1.1.1') { 261 return; 262 } 263 264 $settings = get_option('woocommerce_mccp_settings'); 265 if ( !$settings ) { 266 return; 267 } 268 $account = $this->mccp_account(); 269 $endpoint = '/v2/accounts/' . $account->account; 270 271 foreach ($settings['currencies'] as $currency) { 272 $params['transfer-key'] = $account->{'transfer-key'}; 273 $params['currency'] = $currency->abbr; 274 $params['processing-fee-policy'] = 'percentage'; 275 276 Request::execute('patch', $endpoint, $params, true); 277 } 278 $this->upd_version('1.2.0'); 279 } 280 281 /** 282 * Update plugin from 1.1.0 to 1.1.1 283 * 284 * @return void 285 */ 286 function update_1_1_0__1_1_1() { 287 if ($this->version() === '1.1.0') { 288 $this->upd_version('1.1.1'); 289 } 290 } 291 292 /** 293 * Update plugin from 1.0.0 to 1.1.0 294 * 295 * @return void 296 */ 297 function update_1_0_0__1_1_0() { 298 if ($this->version() !== false) { 299 return; 300 } 301 global $wpdb, $table_prefix; 302 303 // Table update when plugin aleady installed & active 304 $table = $table_prefix . DB::TABLE_INVOICE; 305 $is_metadata = sprintf('SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = "%s" AND column_name = "meta"', $table); 306 $row = $wpdb->get_results($is_metadata); 307 if(empty($row)){ 308 $add_metadata = sprintf("ALTER TABLE `%s` ADD `meta` text NULL AFTER `details`", $table); 309 $wpdb->query($add_metadata); 310 } 311 312 // Old version settings update 313 $settings = get_option('woocommerce_mccp_settings'); 314 if ( !$settings ) 315 return; 316 317 $apirone_account = $this->mccp_account(); 318 319 // Map old currensies 320 foreach (Apirone::currencyList() as $apirone_currency) { 321 $currency = $this->mccp_currency($apirone_currency, $apirone_account); 322 if (array_key_exists($apirone_currency->abbr, (array) $settings['currencies'])) { 323 $old_currency = $settings['currencies'][$apirone_currency->abbr]; 324 if (gettype($old_currency) === 'array') { // Update from version 1.0.0 325 if ($old_currency['address']) { 326 $currency->address = $old_currency['address']; 327 $result = Apirone::setTransferAddress($apirone_account, $currency->abbr, $old_currency['address']); 328 if ($result) { 329 $currency->valid = 1; 330 } 331 } 332 $currency->enabled = ($old_currency['enabled']) ? 1 : 0; 333 } 334 else { // Clear install of 1.1.1 335 $currency = $old_currency; 336 } 337 } 338 $mccp_currencies[$apirone_currency->abbr] = $currency; 339 340 } 341 // Update currensies 342 $settings['currencies'] = $mccp_currencies; 343 // Add new params 344 $settings['factor'] = '1'; 345 $settings['timeout'] = '1800'; 346 $settings['check_timeout'] = '10'; 347 $settings['backlink'] = ''; 348 $settings['apirone_logo'] = 'yes'; 349 $settings['version'] = '1.1.0'; 350 351 // Unset unused 352 unset($settings['count_confirmations']); 353 unset($settings['debug']); 354 unset($settings['wallets']); 355 unset($settings['statuses']); 356 357 update_option('woocommerce_mccp_settings', $settings); 358 delete_option('woocommerce_mccp_wallets'); 359 } 12 /** 13 * Generate receipt error html 14 * 15 * @param mixed $reason 16 * @param string $sub 17 * @return void 18 */ 19 static public function receipt_error_message($reason, $sub = '') { 20 21 switch ($reason) { 22 case 'disabled': 23 $message = __('Payment method disabled', 'mccp'); 24 break; 25 case 'required': 26 $message = __('Required param not exists', 'mccp'); 27 break; 28 case 'not-supported': 29 $message = __(sprintf('Currency \'%s\' is not supported', $sub), 'mccp'); 30 break; 31 case 'not-valid': 32 $message = __(sprintf('Currency \'%s\' has no valid address', $sub), 'mccp'); 33 break; 34 case 'not-enabled': 35 $message = __(sprintf('Currency \'%s\' is not enabled', $sub), 'mccp'); 36 break; 37 } 38 39 echo '<div class="receipt_info">' . esc_html( $message ) . '</div>'; 40 } 41 42 /** 43 * Return woocommerce order status depends on invoice status 44 * 45 * @param mixed $invoice 46 * @return mixed 47 */ 48 public static function get_order_status_by_invoice($invoice) { 49 switch ($invoice->status) { 50 case 'created': 51 case 'partpaid': 52 case 'paid': 53 case 'overpaid': 54 $status = 'pending'; 55 break; 56 case 'completed': 57 $status = 'processing'; 58 break; 59 case 'expired': 60 $status = 'failed'; 61 } 62 return $status; 63 } 64 65 /** 66 * Get account. 67 * Create new account when option 'woocommerce_mccp_account' not exist 68 * 69 * @return object|false 70 */ 71 public function mccp_account($renew = false) { 72 $account = $this->get_option('woocommerce_mccp_account'); 73 74 if ( !$account || $renew ) { 75 $account = Apirone::accountCreate(); 76 update_option('woocommerce_mccp_account', $account); 77 } 78 return $account; 79 } 80 81 /** 82 * Get secret. 83 * Create new secret when option 'woocommerce_mccp_secret' not exist 84 * 85 * @return object|false 86 */ 87 88 public function mccp_secret($renew = false) { 89 $secret = get_option('woocommerce_mccp_secret'); 90 91 if ( !$secret || $renew ) { 92 $secret = md5(time()); 93 update_option('woocommerce_mccp_secret', $secret); 94 } 95 return $secret; 96 } 97 98 /** 99 * Return currency object by currency abbr or false if not exist 100 * 101 * @param mixed $abbr currency abbr - btc, doge, etc 102 * @return false|object 103 */ 104 public function get_mccp_currency($abbr) { 105 $currencies = (array) $this->get_option('currencies'); 106 107 if ( $currencies && array_key_exists($abbr, $currencies) ) { 108 return $currencies[$abbr]; 109 } 110 111 return false; 112 } 113 114 public function get_crypto_total($value, $currency, $abbr) { 115 $factor = $this->get_option('factor'); 116 117 return Apirone::fiat2crypto($value * $factor, $currency, $abbr); 118 } 119 120 /** 121 * Generate MCCP payment fields 122 * 123 * @return void 124 */ 125 function payment_fields() { 126 $show_test_net = false; 127 $show_test_net = WC()->customer->get_billing_email() == $this->get_option('test_customer') ? true : false; 128 if ($this->is_repayment()) { 129 $order_id = $this->is_repayment(); 130 $order = wc_get_order( $order_id ); 131 $total = $order->get_total(); 132 $show_test_net = $order->get_billing_email() == $this->get_option('test_customer') ? true : false; 133 } 134 else { 135 $total = WC()->cart->total; 136 } 137 138 $woo_currency = get_woocommerce_currency(); 139 $active_currencies = array(); 140 141 foreach ((array) $this->get_option('currencies') as $item) { 142 if ($item->testnet === 1 && !current_user_can('manage_options')) { 143 if ( !$show_test_net ) { 144 continue; 145 } 146 } 147 148 if (!empty($item->address) && $item->enabled && $item->valid) { 149 $currency = array(); 150 151 $crypto_total = Apirone::exp2dec($this->get_crypto_total($total, $woo_currency, $item->abbr), 0); 152 153 $currency['abbr'] = $item->abbr; 154 $currency['name'] = $item->name; 155 $currency['total'] = $crypto_total; 156 $currency['dust-rate'] = Apirone::exp2dec(Apirone::min2cur($item->{'dust-rate'}, $item->{'units-factor'}), 0); 157 $currency['payable'] = $currency['total'] >= $currency['dust-rate'] ? true : false; 158 $active_currencies[] = $currency; 159 } 160 } 161 // If plugin active but haven't active currencies 162 if ( empty($active_currencies)) { 163 _e('Cryptocurrency payment temporary unavailable. Choose other payment method.', 'mccp'); 164 return; 165 } 166 ?> 167 <select id="mccp_currency" name="mccp_currency"> 168 <?php foreach ( $active_currencies as $currency ) : ?> 169 <option <?php echo (!$currency['payable']) ? 'disabled' : ''; ?> value="<?php echo esc_html($currency['abbr']); ?>"> 170 <?php echo esc_html($currency['name']); ?>: 171 <?php echo esc_html($currency['total']); ?> 172 </option> 173 <?php endforeach; ?> 174 </select> 175 <?php 176 } 177 178 function is_repayment () { 179 if (isset($_GET['pay_for_order'])) { 180 return get_query_var('order-pay', false); 181 } 182 183 return false; 184 } 185 186 /** 187 * Get plugin version 188 * 189 * @return false|string 190 */ 191 function version() { 192 $settings = get_option('woocommerce_mccp_settings', array()); 193 return array_key_exists('version', $settings) ? $settings['version'] : false; 194 } 195 196 /** 197 * Update plugin version only 198 * 199 * @return void 200 */ 201 function upd_version($new) { 202 $settings = get_option('woocommerce_mccp_settings'); 203 $settings['version'] = $new; 204 205 update_option('woocommerce_mccp_settings', $settings); 206 } 207 208 /** 209 * Plugin version update entry point 210 * 211 * @return void 212 */ 213 function update() { 214 $this->update_1_0_0__1_1_0(); 215 $this->update_1_1_0__1_1_1(); 216 $this->update_1_1_1__1_2_0(); 217 $this->update_1_2_0__1_2_1(); 218 $this->update_1_2_1__1_2_2(); 219 $this->update_1_2_2__1_2_3(); 220 } 221 222 /** 223 * Update plugin from 1.2.1 to 1.2.2 224 * Fix mobile layout 225 * @return void 226 */ 227 function update_1_2_2__1_2_3() { 228 if ($this->version() !== '1.2.2') { 229 return; 230 } 231 $this->upd_version('1.2.3'); 232 } 233 234 /** 235 * Update plugin from 1.2.1 to 1.2.2 236 * Fix mobile layout 237 * @return void 238 */ 239 function update_1_2_1__1_2_2() { 240 if ($this->version() !== '1.2.1') { 241 return; 242 } 243 $this->upd_version('1.2.2'); 244 } 245 246 /** 247 * Update plugin from 1.2.0 to 1.2.1 248 * Add a message when the invoice isn't created/found. 249 * @return void 250 */ 251 function update_1_2_0__1_2_1() { 252 if ($this->version() !== '1.2.0') { 253 return; 254 } 255 $this->upd_version('1.2.1'); 256 } 257 258 /** 259 * Update plugin from 1.1.1 to 1.2.0 260 * Fee plan update to percentage 261 * @return void 262 */ 263 function update_1_1_1__1_2_0() { 264 if ($this->version() !== '1.1.1') { 265 return; 266 } 267 268 $settings = get_option('woocommerce_mccp_settings'); 269 if ( !$settings ) { 270 return; 271 } 272 $account = $this->mccp_account(); 273 $endpoint = '/v2/accounts/' . $account->account; 274 275 foreach ($settings['currencies'] as $currency) { 276 $params['transfer-key'] = $account->{'transfer-key'}; 277 $params['currency'] = $currency->abbr; 278 $params['processing-fee-policy'] = 'percentage'; 279 280 Request::execute('patch', $endpoint, $params, true); 281 } 282 $this->upd_version('1.2.0'); 283 } 284 285 /** 286 * Update plugin from 1.1.0 to 1.1.1 287 * 288 * @return void 289 */ 290 function update_1_1_0__1_1_1() { 291 if ($this->version() === '1.1.0') { 292 $this->upd_version('1.1.1'); 293 } 294 } 295 296 /** 297 * Update plugin from 1.0.0 to 1.1.0 298 * 299 * @return void 300 */ 301 function update_1_0_0__1_1_0() { 302 if ($this->version() !== false) { 303 return; 304 } 305 global $wpdb, $table_prefix; 306 307 // Table update when plugin aleady installed & active 308 $table = $table_prefix . DB::TABLE_INVOICE; 309 $is_metadata = sprintf('SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = "%s" AND column_name = "meta"', $table); 310 $row = $wpdb->get_results($is_metadata); 311 if(empty($row)){ 312 $add_metadata = sprintf("ALTER TABLE `%s` ADD `meta` text NULL AFTER `details`", $table); 313 $wpdb->query($add_metadata); 314 } 315 316 // Old version settings update 317 $settings = get_option('woocommerce_mccp_settings'); 318 if ( !$settings ) 319 return; 320 321 $apirone_account = $this->mccp_account(); 322 323 // Map old currensies 324 foreach (Apirone::currencyList() as $apirone_currency) { 325 $currency = $this->mccp_currency($apirone_currency, $apirone_account); 326 if (array_key_exists($apirone_currency->abbr, (array) $settings['currencies'])) { 327 $old_currency = $settings['currencies'][$apirone_currency->abbr]; 328 if (gettype($old_currency) === 'array') { // Update from version 1.0.0 329 if ($old_currency['address']) { 330 $currency->address = $old_currency['address']; 331 $result = Apirone::setTransferAddress($apirone_account, $currency->abbr, $old_currency['address']); 332 if ($result) { 333 $currency->valid = 1; 334 } 335 } 336 $currency->enabled = ($old_currency['enabled']) ? 1 : 0; 337 } 338 else { // Clear install of 1.1.1 339 $currency = $old_currency; 340 } 341 } 342 $mccp_currencies[$apirone_currency->abbr] = $currency; 343 344 } 345 // Update currensies 346 $settings['currencies'] = $mccp_currencies; 347 // Add new params 348 $settings['factor'] = '1'; 349 $settings['timeout'] = '1800'; 350 $settings['check_timeout'] = '10'; 351 $settings['backlink'] = ''; 352 $settings['apirone_logo'] = 'yes'; 353 $settings['version'] = '1.1.0'; 354 355 // Unset unused 356 unset($settings['count_confirmations']); 357 unset($settings['debug']); 358 unset($settings['wallets']); 359 unset($settings['statuses']); 360 361 update_option('woocommerce_mccp_settings', $settings); 362 delete_option('woocommerce_mccp_wallets'); 363 } 360 364 } -
multi-crypto-currency-payment/trunk/mccp.php
r2946588 r3046162 4 4 Plugin URI: https://github.com/zaytseff/mccp-woo 5 5 Description: Multi currency crypto payments for Woocommerce. Uses Apirone Processing Provider 6 Version: 1.2. 56 Version: 1.2.6 7 7 Author: Alex Zaytseff 8 8 Author URI: https://github.com/zaytseff -
multi-crypto-currency-payment/trunk/readme.txt
r2946588 r3046162 2 2 3 3 Contributors: zaytseff 4 Tags: bitcoin, litecoin, dogecoin, bitcoin cash, BTC, LTC, BCH, Doge, plugin, forwarding, seamless, payment, cryptocurrency,Multi CryptoCurrency Payments, accept BTC, accept LTC, accept BCH, accept Crypto5 Requires at least: 5.66 Tested up to: 6. 2.27 Requires PHP: 7. 08 Stable tag: 1.2. 44 Tags: tron, usdt, bitcoin, litecoin, dogecoin, bitcoin cash, BTC, LTC, BCH, Doge, plugin, forwarding, seamless, payment, cryptocurrency,Multi CryptoCurrency Payments, accept BTC, accept LTC, accept BCH, accept Crypto 5 Requires at least: 7.4 6 Tested up to: 6.4.2 7 Requires PHP: 7.4 8 Stable tag: 1.2.6 9 9 License: GPLv2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html 11 11 12 12 Woocommerce plugin - Multi CryptoCurrency Payments 13 Requires at least WooCommerce: 4.0 Tested up to: 7.7.2License: GPLv2 or later13 Requires at least WooCommerce: 6.0 Tested up to: 8.6.1 License: GPLv2 or later 14 14 15 15 == Description == … … 78 78 79 79 == Changelog == 80 81 = Version 1.2.6 | 05/03/2024 = 82 - Internal QR generator 83 - Improved payment process 84 - Tron support 85 80 86 = Version 1.2.5 | 02/08/2023 = 81 87 - Improved plugin activation
Note: See TracChangeset
for help on using the changeset viewer.