Skip to content

Commit

Permalink
add money dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ntheile committed Mar 8, 2024
1 parent f4218c0 commit 114035e
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 65 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/zip-woo-plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
- name: Install zip
run: sudo apt-get install zip

- name: Install php deps
uses: php-actions/composer@v6
run: cd zaprite-payment-gateway && composer install

- name: Zip the folder
run: |
zip -r zaprite-payment-gateway.zip zaprite-payment-gateway
Expand Down
29 changes: 20 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ _In order to use this plugin you have to create an account on

## Local development

### Dependencies
This project uses PHP composer to manage dependencies. Install it here:
https://getcomposer.org/doc/00-intro.md

Then run: `cd zaprite-payment-gateway && composer install` . This should create a vendor folder with the deps

### With Docker

To develop locally, you can run WordPress locally with docker. This will run
Expand Down Expand Up @@ -63,17 +69,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:

```
fastcgi_param ZAPRITE_APP_URL 'http://localhost:3000';
```

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

```php
define('ZAPRITE_APP_URL', getenv('ZAPRITE_APP_URL') ? getenv('ZAPRITE_APP_URL') : 'http://localhost:3000' );
```
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 +128,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`
6 changes: 6 additions & 0 deletions zaprite-payment-gateway/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "nick/zaprite-payment-gateway",
"require": {
"moneyphp/money": "^4.5"
}
}
29 changes: 18 additions & 11 deletions zaprite-payment-gateway/includes/utils.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
<?php
namespace ZapritePlugin;

class Utils {
use Money\Money;
use Money\Currencies\ISOCurrencies;
use Money\Parser\DecimalMoneyParser;
use Money\Formatter\DecimalMoneyFormatter;
use Money\Currency;

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 );
class Utils {
public static function convert_to_smallest_unit( $amount, $currencyCode ) {
$currencies = new ISOCurrencies();
$moneyParser = new DecimalMoneyParser( $currencies );
$money = $moneyParser->parse( $amount, new Currency( $currencyCode ) );
return $money->getAmount();
}
// 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 convert_to_major_unit( $amount, $currencyCode ) {
$currencies = new ISOCurrencies();
$moneyParser = new DecimalMoneyParser( $currencies );
$money = $moneyParser->parse( $amount, new Currency( $currencyCode ) );
$moneyFormatter = new DecimalMoneyFormatter( $currencies );
$majorAmount = $moneyFormatter->format( $money );
return $majorAmount;
}
// 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-08T21:37:54+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:68
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:101
#: zaprite-payment-gateway.php:143
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:106
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:133
msgid "Enable Zaprite Payments"
msgstr ""

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

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

#: zaprite-payment-gateway.php:133
#: zaprite-payment-gateway.php:142
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:146
msgid "Description"
msgstr ""

#: zaprite-payment-gateway.php:139
#: zaprite-payment-gateway.php:148
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:149
#: zaprite-payment-gateway.php:150
msgid "Powered by Zaprite"
msgstr ""

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

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

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

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

#. translators: %s: URL to Woo store connection settings
#: zaprite-payment-gateway.php:156
#: zaprite-payment-gateway.php:165
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:386
msgid "Settings"
msgstr ""

#: zaprite-payment-gateway.php:386
#: zaprite-payment-gateway.php:397
msgid "Setup Guide"
msgstr ""
2 changes: 1 addition & 1 deletion zaprite-payment-gateway/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"scripts": {
"start": "wp-scripts start",
"build": "wp-scripts build && npm run i18n:build",
"build": "composer install && wp-scripts build && npm run i18n:build",
"i18n": "npm run i18n:build",
"i18n:build": "npm run i18n:pot && ../bin/build_i18n.sh",
"i18n:pot": "php -d xdebug.max_nesting_level=512 $(which wp) i18n make-pot --exclude=\"node_modules/,languages/,assets/\" . languages/zaprite-payment-gateway.pot",
Expand Down
28 changes: 19 additions & 9 deletions zaprite-payment-gateway/zaprite-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,22 @@

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' );

define( 'WC_PAYMENT_GATEWAY_ZAPRITE_FILE', __FILE__ );
define( 'WC_PAYMENT_GATEWAY_ZAPRITE_URL', plugins_url( '', WC_PAYMENT_GATEWAY_ZAPRITE_FILE ) );
define( 'WC_PAYMENT_GATEWAY_ZAPRITE_ASSETS', WC_PAYMENT_GATEWAY_ZAPRITE_URL . '/assets' );

require_once plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';

require_once __DIR__ . '/includes/hooks.php';
register_activation_hook( __FILE__, 'zaprite_message_on_plugin_activate' );
add_action( 'admin_notices', 'zaprite_admin_notices' );
Expand Down Expand Up @@ -62,7 +71,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 +188,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();
$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::convert_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 +218,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 +292,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::convert_to_major_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 114035e

Please sign in to comment.