From 48ea7509cfda57c9e77a53ac86ffb697262da4aa Mon Sep 17 00:00:00 2001 From: Du Peng Date: Mon, 19 Jun 2023 21:09:26 +0200 Subject: [PATCH] (bug) Incorrect amount calculation when paying monthly subcription with a wallet --- CHANGELOG.md | 1 + lib/pay_zen/service.rb | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index feb19ba0d..9ba0521e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Fix a bug: OpenAPI accounting gateway_object_id missing error - Fix a bug: unable to modify the price of prepaid pack - Fix a bug: notification type missing +- Fix critical bug: Incorrect amount calculation when paying monthly subcription with a wallet for PayZen ## v6.0.6 2023 May 4 diff --git a/lib/pay_zen/service.rb b/lib/pay_zen/service.rb index 2b5f096ce..f3b15be56 100644 --- a/lib/pay_zen/service.rb +++ b/lib/pay_zen/service.rb @@ -25,8 +25,15 @@ class PayZen::Service < Payment::Service order_id: order_id } unless first_item.details['adjustment']&.zero? && first_item.details['other_items']&.zero? - params[:initial_amount] = payzen_amount(first_item.amount) - params[:initial_amount_number] = 1 + initial_amount = first_item.amount + initial_amount -= payment_schedule.wallet_amount if payment_schedule.wallet_amount + if initial_amount.zero? + params[:effect_date] = (first_item.due_date + 1.month).iso8601 + params[:rrule] = rrule(payment_schedule, -1) + else + params[:initial_amount] = payzen_amount(initial_amount) + params[:initial_amount_number] = 1 + end end pz_subscription = client.create_subscription(**params) @@ -123,16 +130,21 @@ class PayZen::Service < Payment::Service private - def rrule(payment_schedule) + def rrule(payment_schedule, offset = 0) count = payment_schedule.payment_schedule_items.count - "RRULE:FREQ=MONTHLY;COUNT=#{count}" + "RRULE:FREQ=MONTHLY;COUNT=#{count + offset}" end # check if the given transaction matches the given PaymentScheduleItem def transaction_matches?(transaction, payment_schedule_item) transaction_date = Time.zone.parse(transaction['creationDate']).to_date - transaction['amount'] == payment_schedule_item.amount && + amount = payment_schedule_item.amount + if !payment_schedule_item.details['adjustment']&.zero? && payment_schedule_item.payment_schedule.wallet_amount + amount -= payment_schedule_item.payment_schedule.wallet_amount + end + + transaction['amount'] == amount && transaction_date >= payment_schedule_item.due_date.to_date && transaction_date <= payment_schedule_item.due_date.to_date + 7.days end