Skip to content

Commit a494367

Browse files
authored
discounts (#36)
1 parent 537cfe3 commit a494367

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

zaprite-payment-gateway/changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Zaprite Payment Gateway
22

3+
## [1.0.5] - 2024-05-22
4+
5+
- Update: Add discounts.
6+
37
## [1.0.4] - 2024-04-09
48

59
- Update: Add method to order metadata.

zaprite-payment-gateway/includes/zaprite_api.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ public function createCharge( $amount, $currency, $order_id ) {
4242
'externalOrderId' => "$order_id",
4343
'externalUniqId' => $key,
4444
);
45-
$response = $c->post( "$this->api_url/api/public/woo/create-order", array(), json_encode( $data ), $headers );
45+
$query_params = array(
46+
'wooPluginVersion' => ZAPRITE_WOOCOMMERCE_VERSION,
47+
);
48+
$response = $c->post( "$this->api_url/api/public/woo/create-order", $query_params, json_encode( $data ), $headers );
4649
error_log( 'Send invoice status ===>' . $response['status'] );
4750
return $response;
4851
}
@@ -60,8 +63,11 @@ public function checkCharge( $order_id ) {
6063
'apiKey' => $this->api_key,
6164
'orderId' => "$zapriteOrderId",
6265
);
66+
$query_params = array(
67+
'wooPluginVersion' => ZAPRITE_WOOCOMMERCE_VERSION,
68+
);
6369
$apiKey = $this->api_key;
64-
$response = $c->post( "$this->api_url/api/public/woo/check-order", array(), json_encode( $data ), $headers );
70+
$response = $c->post( "$this->api_url/api/public/woo/check-order", $query_params, json_encode( $data ), $headers );
6571
error_log( 'Check order status ===>' . $response['status'] );
6672
return $response;
6773
}

zaprite-payment-gateway/readme.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Tags: payment, gateway, woocommerce, bitcoin, lightning
44
Requires at least: 6.4.0
55
Tested up to: 6.5.2
66
Requires PHP: 7.2
7-
Stable tag: 1.0.4
7+
Stable tag: 1.0.5
88
License: MIT
99
License URI: https://mit-license.org/
1010

@@ -42,6 +42,9 @@ https://youtu.be/ohepHnGE3Tk?si=LEV1z86-BQOgB3i6
4242

4343
== Changelog ==
4444

45+
= 1.0.5 =
46+
* Update: Add discounts.
47+
4548
= 1.0.4 =
4649
* Update: Add method to order metadata.
4750

@@ -59,6 +62,9 @@ https://youtu.be/ohepHnGE3Tk?si=LEV1z86-BQOgB3i6
5962

6063
== Upgrade Notice ==
6164

65+
= 1.0.5 =
66+
Add Discounts.
67+
6268
= 1.0.4 =
6369
This update adds method in the order metadata.
6470

zaprite-payment-gateway/zaprite-payment-gateway.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Plugin Name: Zaprite Payment Gateway
55
* Plugin URI: https://github.com/ZapriteApp/zaprite-for-woocommerce
66
* Description: Accept bitcoin (on-chain and lightning) and fiat payments in one unified Zaprite Checkout.
7-
* Version: 1.0.4
7+
* Version: 1.0.5
88
* Author: zaprite
99
* Author URI: https://zaprite.com
1010
* Text Domain: zaprite-payment-gateway
@@ -23,7 +23,7 @@
2323
);
2424

2525

26-
define( 'ZAPRITE_WOOCOMMERCE_VERSION', '1.0.0' );
26+
define( 'ZAPRITE_WOOCOMMERCE_VERSION', '1.0.5' );
2727

2828
define( 'WC_PAYMENT_GATEWAY_ZAPRITE_FILE', __FILE__ );
2929
define( 'WC_PAYMENT_GATEWAY_ZAPRITE_URL', plugins_url( '', WC_PAYMENT_GATEWAY_ZAPRITE_FILE ) );
@@ -273,36 +273,38 @@ function zaprite_server_add_update_status_callback( $data ) {
273273

274274
switch ( $wooStatus ) {
275275
case 'processing':
276-
// check if fiat premium was applied, if so, save to custom data in woo
277-
$paidPremium = $orderStatusRes['response']['paidPremium'];
278-
$paidPremiumCurrency = $orderStatusRes['response']['currency'];
279-
error_log( "ZAPRITE: paidPremium minor units $paidPremium $paidPremiumCurrency " );
280-
if ( $paidPremium ) {
276+
// check if premium or discount was applied, if so, save to custom data in woo
277+
$appliedDiscount = $orderStatusRes['response']['appliedDiscount'];
278+
$appliedDiscountCurrency = $orderStatusRes['response']['currency'];
279+
error_log( "ZAPRITE: appliedDiscount minor units $appliedDiscount $appliedDiscountCurrency " );
280+
if ( $appliedDiscount ) {
281281
// add fee to order
282-
283282
// Edge Case
284283
// return error if the currencies do not match...this could be an edge case where the
285284
// woo store owner changes his currency before this order's payment is settled.
286285
// TODO: in the future we could convert the currencies and do the math but I do
287286
// not know how to easily do that in PHP
288287
$wooDefaultCurrency = get_woocommerce_currency();
289-
if ( $wooDefaultCurrency !== $paidPremiumCurrency ) {
290-
return new WP_REST_Response( "Currencies do not match. Woo currency is $wooDefaultCurrency. Zaprite currency for premium paid is $paidPremiumCurrency", 400 );
288+
if ( $wooDefaultCurrency !== $appliedDiscountCurrency ) {
289+
return new WP_REST_Response( "Currencies do not match. Woo currency is $wooDefaultCurrency. Zaprite currency for applied discount is $appliedDiscountCurrency", 400 );
291290
}
292291
// convert to major units (woo requires major units)
293292
$currency = $order->get_currency();
294-
$paidPremiumAmountMajorUnits = Utils::from_smallest_unit( $paidPremium, $currency );
295-
error_log( "ZAPRITE: paidPremium major units $paidPremiumAmountMajorUnits" );
293+
// Discounts are positive numbers and Premiums are negative in Zaprite
294+
$appliedDiscountAmountMajorUnits = -(Utils::from_smallest_unit( $appliedDiscount, $currency ));
295+
error_log( "ZAPRITE: appliedDiscount major units $appliedDiscountAmountMajorUnits" );
296+
$isDiscount = $appliedDiscountAmountMajorUnits < 0;
296297
$item_fee = new WC_Order_Item_Fee();
297-
$item_fee->set_name( 'Fiat Premium Fee' );
298-
$item_fee->set_amount( $paidPremiumAmountMajorUnits );
298+
$item_fee->set_name( $isDiscount ? 'Discount' : 'Premium' );
299+
$item_fee->set_amount( $appliedDiscountAmountMajorUnits );
299300
$item_fee->set_tax_class( '' ); // or 'standard' if the fee is taxable
300301
$item_fee->set_tax_status( 'none' ); // or 'taxable'
301-
$item_fee->set_total( $paidPremiumAmountMajorUnits ); // The total amount of the fee
302+
$item_fee->set_total( $appliedDiscountAmountMajorUnits );
302303
$order->add_item( $item_fee );
303304
// Calculate totals and save the order
304305
$order->calculate_totals();
305-
$order->add_meta_data( 'zaprite_fiat_premium_extra_paid_amount', $paidPremiumAmountMajorUnits, true );
306+
$meta_data_label = $isDiscount ? 'zaprite_discount_amount' : 'zaprite_premium_amount';
307+
$order->add_meta_data( $meta_data_label, $appliedDiscountAmountMajorUnits, true );
306308
$order->save();
307309
}
308310
if ( ! $order->has_status( 'completed' ) ) {

0 commit comments

Comments
 (0)