|
4 | 4 | * Plugin Name: Zaprite Payment Gateway |
5 | 5 | * Plugin URI: https://github.com/ZapriteApp/zaprite-for-woocommerce |
6 | 6 | * 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 |
8 | 8 | * Author: zaprite |
9 | 9 | * Author URI: https://zaprite.com |
10 | 10 | * Text Domain: zaprite-payment-gateway |
|
23 | 23 | ); |
24 | 24 |
|
25 | 25 |
|
26 | | -define( 'ZAPRITE_WOOCOMMERCE_VERSION', '1.0.0' ); |
| 26 | +define( 'ZAPRITE_WOOCOMMERCE_VERSION', '1.0.5' ); |
27 | 27 |
|
28 | 28 | define( 'WC_PAYMENT_GATEWAY_ZAPRITE_FILE', __FILE__ ); |
29 | 29 | 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 ) { |
273 | 273 |
|
274 | 274 | switch ( $wooStatus ) { |
275 | 275 | 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 ) { |
281 | 281 | // add fee to order |
282 | | - |
283 | 282 | // Edge Case |
284 | 283 | // return error if the currencies do not match...this could be an edge case where the |
285 | 284 | // woo store owner changes his currency before this order's payment is settled. |
286 | 285 | // TODO: in the future we could convert the currencies and do the math but I do |
287 | 286 | // not know how to easily do that in PHP |
288 | 287 | $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 ); |
291 | 290 | } |
292 | 291 | // convert to major units (woo requires major units) |
293 | 292 | $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; |
296 | 297 | $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 ); |
299 | 300 | $item_fee->set_tax_class( '' ); // or 'standard' if the fee is taxable |
300 | 301 | $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 ); |
302 | 303 | $order->add_item( $item_fee ); |
303 | 304 | // Calculate totals and save the order |
304 | 305 | $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 ); |
306 | 308 | $order->save(); |
307 | 309 | } |
308 | 310 | if ( ! $order->has_status( 'completed' ) ) { |
|
0 commit comments