From ea774e2080b1201af1273124e0af01f12b5421d4 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Thu, 3 Jun 2021 14:28:52 +0200 Subject: [PATCH] wait for the first payzen transaction + fix initial amount --- .../src/javascript/api/clients/api-client.ts | 2 +- lib/pay_zen/service.rb | 56 ++++++++++--------- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/app/frontend/src/javascript/api/clients/api-client.ts b/app/frontend/src/javascript/api/clients/api-client.ts index 8deb6d7b4..ab6ef7833 100644 --- a/app/frontend/src/javascript/api/clients/api-client.ts +++ b/app/frontend/src/javascript/api/clients/api-client.ts @@ -1,5 +1,5 @@ import axios, { AxiosInstance } from 'axios' -// compile + const token: HTMLMetaElement = document.querySelector('[name="csrf-token"]'); const client: AxiosInstance = axios.create({ headers: { diff --git a/lib/pay_zen/service.rb b/lib/pay_zen/service.rb index f851e05fc..2096c4c2a 100644 --- a/lib/pay_zen/service.rb +++ b/lib/pay_zen/service.rb @@ -24,7 +24,7 @@ class PayZen::Service < Payment::Service rrule: rrule(payment_schedule), order_id: order_id } - unless first_item.details['adjustment']&.zero? + unless first_item.details['adjustment']&.zero? && first_item.details['other_items']&.zero? params[:initial_amount] = first_item.amount params[:initial_amount_number] = 1 end @@ -51,31 +51,37 @@ class PayZen::Service < Payment::Service def process_payment_schedule_item(payment_schedule_item) pz_order = payment_schedule_item.payment_schedule.payment_gateway_objects.find { |pgo| pgo.gateway_object_type == 'PayZen::Order' }.gateway_object.retrieve transaction = pz_order['answer']['transactions'].last - if transaction['status'] == 'PAID' - PaymentScheduleService.new.generate_invoice(payment_schedule_item, - payment_method: 'card', - payment_id: transaction['uuid'], - payment_type: 'PayZen::Transaction') - payment_schedule_item.update_attributes(state: 'paid', payment_method: 'card') - pgo = PaymentGatewayObject.find_or_initialize_by(item: payment_schedule_item) - pgo.gateway_object = PayZen::Item.new('PayZen::Transaction', transaction['uuid']) - pgo.save! - elsif transaction['status'] == 'RUNNING' - if payment_schedule_item.state == 'new' - # notify only for new deadlines, to prevent spamming - NotificationCenter.call type: 'notify_admin_payment_schedule_failed', - receiver: User.admins_and_managers, - attached_object: payment_schedule_item - NotificationCenter.call type: 'notify_member_payment_schedule_failed', - receiver: payment_schedule_item.payment_schedule.user, - attached_object: payment_schedule_item + transaction_date = DateTime.parse(transaction['creationDate']).to_date + # check that the transaction matches the current PaymentScheduleItem + if transaction['amount'] == payment_schedule_item.amount && + transaction_date >= payment_schedule_item.due_date.to_date && + transaction_date <= payment_schedule_item.due_date.to_date + 7.days + if transaction['status'] == 'PAID' + PaymentScheduleService.new.generate_invoice(payment_schedule_item, + payment_method: 'card', + payment_id: transaction['uuid'], + payment_type: 'PayZen::Transaction') + payment_schedule_item.update_attributes(state: 'paid', payment_method: 'card') + pgo = PaymentGatewayObject.find_or_initialize_by(item: payment_schedule_item) + pgo.gateway_object = PayZen::Item.new('PayZen::Transaction', transaction['uuid']) + pgo.save! + elsif transaction['status'] == 'RUNNING' + if payment_schedule_item.state == 'new' + # notify only for new deadlines, to prevent spamming + NotificationCenter.call type: 'notify_admin_payment_schedule_failed', + receiver: User.admins_and_managers, + attached_object: payment_schedule_item + NotificationCenter.call type: 'notify_member_payment_schedule_failed', + receiver: payment_schedule_item.payment_schedule.user, + attached_object: payment_schedule_item + end + payment_schedule_item.update_attributes(state: transaction['detailedStatus']) + pgo = PaymentGatewayObject.find_or_initialize_by(item: payment_schedule_item) + pgo.gateway_object = PayZen::Item.new('PayZen::Transaction', transaction['uuid']) + pgo.save! + else + payment_schedule_item.update_attributes(state: 'error') end - payment_schedule_item.update_attributes(state: transaction['detailedStatus']) - pgo = PaymentGatewayObject.find_or_initialize_by(item: payment_schedule_item) - pgo.gateway_object = PayZen::Item.new('PayZen::Transaction', transaction['uuid']) - pgo.save! - else - payment_schedule_item.update_attributes(state: 'error') end end