Skip to content

Commit

Permalink
fix: decimal conversion (#28)
Browse files Browse the repository at this point in the history
* add money dependency

* update name

* github action

* major unit

* bitcoin

* simple conversion function

* update version
  • Loading branch information
ntheile authored Mar 13, 2024
1 parent f4218c0 commit 6d96077
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 66 deletions.
23 changes: 14 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,20 @@ you want then just create a symlink to the `plugin` directory:
ln -s <path to the cloned repo>/zaprite-for-woocommerce/plugin <path to local wordpress>/wp-content/plugins/zaprite-payment-gateway
```

Currenly there is no way to manage env variables if you use Locals by Flywheel,
and there appears to be timing issues if you use the wp-config.php file. So to
test locally edit `zaprite-payment-gateway.php`
Next you need to set an environment variable to point to your local instance of Zaprite. You can set this at the web server level in Flywheel Locals.
If your instance of Wordpress uses nginx you can "Go to site folder" then open `conf/nginx/site.conf.hbs`. Add this in the `location ~ \.php$` config:

```php
define('ZAPRITE_APP_URL', getenv('ZAPRITE_APP_URL') ? getenv('ZAPRITE_APP_URL') : 'http://localhost:3000' );
```
fastcgi_param ZAPRITE_APP_URL 'http://localhost:3000';
```

To turn on debugging, open the `wp-config.php`. Add

```
define('WP_DEBUG', true);
```

Also make sure you turn on debugging: Edit the `wp-config.php` File. Look for
the line that says `define('WP_DEBUG', false);`. If it's not there, you'll need
to add it. And change it to true. Then restart your site. This should create a
Lastly, restart your site. This should create a
debug.log file in `wp-content`

# Block Checkout
Expand Down Expand Up @@ -119,4 +122,6 @@ Dependencies

1. Install php `brew install php`
2. Install wordpress cli tools `brew install wp-cli`
3. `npm run build`
3. `cd zaprite-payment-gateway`
4. `npm run build`
5. If you want to package a zip to upload to wordpress plugin manually run `npm run package`
4 changes: 4 additions & 0 deletions zaprite-payment-gateway/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Zaprite Payment Gateway

## [1.0.1] - 2024-03-11

- Bug fix: 3 decimal currency support and conversion fix.

## [1.0.0] - 2024-01-29

- Initial release: Zaprite Payment Gateway with Bitcoin, Lightning and card Checkout support.
29 changes: 17 additions & 12 deletions zaprite-payment-gateway/includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
namespace ZapritePlugin;

class Utils {

public static function convert_to_smallest_unit( $amount ) {
// Get the number of decimals for pricing from WooCommerce settings
$decimals = get_option( 'woocommerce_price_num_decimals' );
// Convert the total to the smallest unit
return $amount * pow( 10, $decimals );
public static function get_currency_smallest_unit_factor( $currency ) {
// source: https://stripe.com/docs/currencies
if ( in_array( $currency, array( 'CLP', 'JPY', 'KRW', 'UGX', 'VND' ) ) ) {
return 1;
}
if ( in_array( $currency, array( 'BHD', 'JOD', 'KWD', 'OMR', 'TND' ) ) ) {
return 1000;
}
if ( in_array( $currency, array( 'BTC', 'LBTC' ) ) ) {
return 100_000_000;
}
return 100;
}
public static function to_smallest_unit( $amount, $currency ) {
return round( $amount * self::get_currency_smallest_unit_factor( $currency ) );
}
// Woo uses major units
public static function convert_to_major_unit( $amount ) {
// Get the number of decimals for pricing from WooCommerce settings
$decimals = get_option( 'woocommerce_price_num_decimals' );
// Convert the total from the smallest unit to the major unit
return $amount / pow( 10, $decimals );
public static function from_smallest_unit( $amount, $currency ) {
return $amount / self::get_currency_smallest_unit_factor( $currency );
}
// convert zaprite to woo status
public static function convert_zaprite_order_status_to_woo_status( $zapriteStatus ) {
Expand Down
65 changes: 30 additions & 35 deletions zaprite-payment-gateway/languages/zaprite-payment-gateway.pot
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Copyright (C) 2024 zaprite
# This file is distributed under the same license as the Zaprite Payment Gateway plugin.
# This file is distributed under the MIT.
msgid ""
msgstr ""
"Project-Id-Version: Zaprite Payment Gateway 1.0.0\n"
Expand All @@ -9,108 +9,103 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"POT-Creation-Date: 2024-01-29T16:27:40+00:00\n"
"POT-Creation-Date: 2024-03-11T15:47:41+00:00\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"X-Generator: WP-CLI 2.9.0\n"
"X-Generator: WP-CLI 2.10.0\n"
"X-Domain: zaprite-payment-gateway\n"

#. Plugin Name of the plugin
#: zaprite-payment-gateway.php
msgid "Zaprite Payment Gateway"
msgstr ""

#. Plugin URI of the plugin
#: zaprite-payment-gateway.php
msgid "https://github.com/ZapriteApp/zaprite-for-woocommerce"
msgstr ""

#. Description of the plugin
#: zaprite-payment-gateway.php
msgid "Accept bitcoin (on-chain and lightning) and fiat payments in one unified Zaprite Checkout."
msgstr ""

#. Author of the plugin
#: zaprite-payment-gateway.php
msgid "zaprite"
msgstr ""

#. Author URI of the plugin
#: zaprite-payment-gateway.php
msgid "https://zaprite.com"
msgstr ""

#: zaprite-payment-gateway.php:58
#: zaprite-payment-gateway.php:66
msgid "Bitcoin payments made easy. Accept on-chain, lightning and fiat payments in one unified Zaprite Checkout experience."
msgstr ""

#: zaprite-payment-gateway.php:91
#: zaprite-payment-gateway.php:134
#: zaprite-payment-gateway.php:99
#: zaprite-payment-gateway.php:141
msgid "Zaprite"
msgstr ""

#: zaprite-payment-gateway.php:95
msgid ""
"\n"
" \n"
" Accept bitcoin (on-chain and lightning) and fiat payments instantly through your\n"
" hosted Zaprite Checkout. Enable the Woo connection on the Zaprite Connections page then paste the provided API Key into the field below.\n"
" \n"
" <a href='https://blog.zaprite.com/how-to-connect-your-woocommerce-store/' target='_blank' rel='noreferrer'>\n"
" Setup Guide\n"
" </a>\n"
" \n"
" "
msgstr ""

#: zaprite-payment-gateway.php:124
#: zaprite-payment-gateway.php:104
msgid "Accept bitcoin (on-chain and lightning) and fiat payments instantly through your hosted Zaprite Checkout. Enable the Woo connection on the Zaprite Connections page then paste the provided API Key into the field below. <a href='https://blog.zaprite.com/how-to-connect-your-woocommerce-store/' target='_blank' rel='noreferrer'>Setup Guide</a>"
msgstr ""

#: zaprite-payment-gateway.php:131
msgid "Enable Zaprite Payments"
msgstr ""

#: zaprite-payment-gateway.php:125
#: zaprite-payment-gateway.php:132
msgid "Enable payments via Zaprite Checkout."
msgstr ""

#: zaprite-payment-gateway.php:131
#: zaprite-payment-gateway.php:138
msgid "Payment Method Name"
msgstr ""

#: zaprite-payment-gateway.php:133
#: zaprite-payment-gateway.php:140
msgid "The payment method title which a customer sees at the checkout of your store."
msgstr ""

#: zaprite-payment-gateway.php:137
#: zaprite-payment-gateway.php:144
msgid "Description"
msgstr ""

#: zaprite-payment-gateway.php:139
#: zaprite-payment-gateway.php:146
msgid "The payment method description which a customer sees at the checkout of your store."
msgstr ""

#: zaprite-payment-gateway.php:140
#: zaprite-payment-gateway.php:141
#: zaprite-payment-gateway.php:147
#: zaprite-payment-gateway.php:148
msgid "Powered by Zaprite"
msgstr ""

#: zaprite-payment-gateway.php:145
#: zaprite-payment-gateway.php:152
msgid "Show checkout Image"
msgstr ""

#: zaprite-payment-gateway.php:146
#: zaprite-payment-gateway.php:153
msgid "Show Zaprite image on checkout"
msgstr ""

#: zaprite-payment-gateway.php:149
#: zaprite-payment-gateway.php:156
msgid "yes"
msgstr ""

#: zaprite-payment-gateway.php:152
#: zaprite-payment-gateway.php:159
msgid "Zaprite API Key"
msgstr ""

#. translators: %s: URL to Woo store connection settings
#: zaprite-payment-gateway.php:156
#: zaprite-payment-gateway.php:163
msgid "Enter the Zaprite API Key from your <a href='%s' target='_blank' rel='noopener noreferrer'>Woo store connection settings</a>."
msgstr ""

#: zaprite-payment-gateway.php:375
#: zaprite-payment-gateway.php:383
msgid "Settings"
msgstr ""

#: zaprite-payment-gateway.php:386
#: zaprite-payment-gateway.php:394
msgid "Setup Guide"
msgstr ""
7 changes: 5 additions & 2 deletions zaprite-payment-gateway/readme.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
=== Zaprite Payment Gateway ===
=== Zaprite Payment Gateway - Accept Bitcoin and Fiat payments in WooCommerce ===
Contributors: zaprite
Tags: payment, gateway, woocommerce, bitcoin, lightning
Requires at least: 6.4.0
Tested up to: 6.4.0
Requires PHP: 7.2
Stable tag: 1.0.0
Stable tag: 1.0.1
License: MIT
License URI: https://mit-license.org/

Expand Down Expand Up @@ -36,6 +36,9 @@ This plugin integrates seamlessly with your WooCommerce Checkout, providing a fa

== Changelog ==

= 1.0.1 =
* Bug fix: 3 decimal currency support and conversion fix.

= 1.0.0 =
* Initial release: Zaprite payment gateway with Bitcoin, Lightning and card Checkout support.

Expand Down
24 changes: 16 additions & 8 deletions zaprite-payment-gateway/zaprite-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Zaprite Payment Gateway
* Plugin URI: https://github.com/ZapriteApp/zaprite-for-woocommerce
* Description: Accept bitcoin (on-chain and lightning) and fiat payments in one unified Zaprite Checkout.
* Version: 1.0.0
* Version: 1.0.1
* Author: zaprite
* Author URI: https://zaprite.com
* Text Domain: zaprite-payment-gateway
Expand All @@ -15,6 +15,13 @@

define( 'ZAPRITE_APP_URL', getenv( 'ZAPRITE_APP_URL' ) ? getenv( 'ZAPRITE_APP_URL' ) : 'https://app.zaprite.com' );
define( 'ZAPRITE_API_URL', getenv( 'ZAPRITE_API_URL' ) ? getenv( 'ZAPRITE_APP_URL' ) : ZAPRITE_APP_URL );
define(
'ZAPRITE_PAY_URL',
ZAPRITE_APP_URL === 'https://app.zaprite.com'
? 'https://pay.zaprite.com'
: ZAPRITE_APP_URL . '/_domains/pay'
);


define( 'ZAPRITE_WOOCOMMERCE_VERSION', '1.0.0' );

Expand Down Expand Up @@ -62,7 +69,7 @@ public function __construct() {
$this->init_settings();

$this->title = $this->get_option( 'title' );
$this->description = 'Powered by Zaprite'; // $this->get_option('description'); // hard code for now, disbled in form setting does not work as you would think, see https://chat.openai.com/share/308744d4-a771-41e0-879e-306c112ec0c4
$this->description = 'Powered by Zaprite'; // $this->get_option('description'); // hard code for now, disabled in form setting does not work as you would think, see https://chat.openai.com/share/308744d4-a771-41e0-879e-306c112ec0c4

$url = $this->get_option( 'zaprite_server_url' );
$api_key = $this->get_option( 'zaprite_api_key' );
Expand Down Expand Up @@ -179,14 +186,14 @@ public function thankyou() {
* Call Zaprite API to create an invoice, and store the invoice in the order metadata.
*/
public function process_payment( $order_id ) {
$zaprite_url = ZAPRITE_APP_URL;
$zaprite_url = ZAPRITE_APP_URL;
$zaprite_pay_url = ZAPRITE_PAY_URL;

error_log( 'ZAPRITE: process_payment' );
$order = wc_get_order( $order_id );
$amount = $order->get_total();
$currency = $order->get_currency();
// error_log(print_r($order, true));
$total_in_smallest_unit = Utils::convert_to_smallest_unit( $amount );
error_log( "ZAPRITE: Amount - $amount Currency: $currency" );
$total_in_smallest_unit = Utils::to_smallest_unit( $amount, $currency );
error_log( "ZAPRITE: currency in smallest unit $total_in_smallest_unit $currency" );

// Call the Zaprite public api to create the invoice
Expand All @@ -209,7 +216,7 @@ public function process_payment( $order_id ) {
$checkout_page_id = get_option( 'woocommerce_checkout_page_id' );
$checkout_page_url = get_permalink( $checkout_page_id );
$backUrl = urlencode( $checkout_page_url );
$redirect_url = "$zaprite_url/_domains/pay/order/$order_id?backUrl=$backUrl";
$redirect_url = "$zaprite_pay_url/order/$order_id?backUrl=$backUrl";
return array(
'result' => 'success',
'redirect' => $redirect_url,
Expand Down Expand Up @@ -283,7 +290,8 @@ function zaprite_server_add_update_status_callback( $data ) {
return new WP_REST_Response( "Currencies do not match. Woo currency is $wooDefaultCurrency. Zaprite currency for premium paid is $paidPremiumCurrency", 400 );
}
// convert to major units (woo requires major units)
$paidPremiumAmountMajorUnits = Utils::convert_to_major_unit( $paidPremium );
$currency = $order->get_currency();
$paidPremiumAmountMajorUnits = Utils::from_smallest_unit( $paidPremium, $currency );
error_log( "ZAPRITE: paidPremium major units $paidPremiumAmountMajorUnits" );
$item_fee = new WC_Order_Item_Fee();
$item_fee->set_name( 'Fiat Premium Fee' );
Expand Down

0 comments on commit 6d96077

Please sign in to comment.