From afb026bdc929cb50e9436b7103cab3f74194a8cd Mon Sep 17 00:00:00 2001 From: Peng DU Date: Tue, 20 Sep 2016 17:42:30 +0200 Subject: [PATCH] clear invoice_item of wallet/group if payment has a error --- app/models/reservation.rb | 2 +- app/models/subscription.rb | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/models/reservation.rb b/app/models/reservation.rb index 95c87f762..d17a2b98b 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -139,7 +139,7 @@ class Reservation < ActiveRecord::Base customer: user.stp_customer_id, amount: -(total * cp.percent_off / 100).to_i, currency: Rails.application.secrets.stripe_currency, - description: "coupon #{cp.code}" + description: "coupon #{cp.code} - reservation" ) end else diff --git a/app/models/subscription.rb b/app/models/subscription.rb index e6cbf9ac0..e4c6dd1b7 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -21,12 +21,13 @@ class Subscription < ActiveRecord::Base def save_with_payment(invoice = true, coupon_code = nil) if valid? customer = Stripe::Customer.retrieve(user.stp_customer_id) + invoice_items = [] begin # dont add a wallet invoice item if pay subscription by reservation if invoice @wallet_amount_debit = get_wallet_amount_debit if @wallet_amount_debit != 0 - Stripe::InvoiceItem.create( + invoice_items << Stripe::InvoiceItem.create( customer: user.stp_customer_id, amount: -@wallet_amount_debit, currency: Rails.application.secrets.stripe_currency, @@ -38,7 +39,7 @@ class Subscription < ActiveRecord::Base cp = Coupon.find_by_code(coupon_code) if not cp.nil? and cp.status(user.id) == 'active' total = plan.amount - Stripe::InvoiceItem.create( + invoice_items << Stripe::InvoiceItem.create( customer: user.stp_customer_id, amount: -(total * cp.percent_off / 100.0).to_i, currency: Rails.application.secrets.stripe_currency, @@ -54,11 +55,11 @@ class Subscription < ActiveRecord::Base cp = Coupon.find_by_code(coupon_code) if not cp.nil? and cp.status(user.id) == 'active' total = plan.amount - Stripe::InvoiceItem.create( + invoice_items << Stripe::InvoiceItem.create( customer: user.stp_customer_id, amount: -(total * cp.percent_off / 100.0).to_i, currency: Rails.application.secrets.stripe_currency, - description: "coupon #{cp.code}" + description: "coupon #{cp.code} - subscription" ) else raise InvalidCouponError @@ -89,32 +90,38 @@ class Subscription < ActiveRecord::Base cancel return true rescue Stripe::CardError => card_error + clear_wallet_and_goupon_invoice_items(invoice_items) logger.error card_error errors[:card] << card_error.message return false rescue Stripe::InvalidRequestError => e + clear_wallet_and_goupon_invoice_items(invoice_items) # Invalid parameters were supplied to Stripe's API logger.error e errors[:payment] << e.message return false rescue Stripe::AuthenticationError => e + clear_wallet_and_goupon_invoice_items(invoice_items) # Authentication with Stripe's API failed # (maybe you changed API keys recently) logger.error e errors[:payment] << e.message return false rescue Stripe::APIConnectionError => e + clear_wallet_and_goupon_invoice_items(invoice_items) # Network communication with Stripe failed logger.error e errors[:payment] << e.message return false rescue Stripe::StripeError => e + clear_wallet_and_goupon_invoice_items(invoice_items) # Display a very generic error to the user, and maybe send # yourself an email logger.error e errors[:payment] << e.message return false rescue => e + clear_wallet_and_goupon_invoice_items(invoice_items) # Something else happened, completely unrelated to Stripe logger.error e errors[:payment] << e.message @@ -293,4 +300,20 @@ class Subscription < ActiveRecord::Base return WalletService.new(user: user, wallet: user.wallet).debit(amount, self) end end + + def clear_wallet_and_goupon_invoice_items(invoice_items) + begin + invoice_items.each(&:delete) + rescue Stripe::InvalidRequestError => e + logger.error e + rescue Stripe::AuthenticationError => e + logger.error e + rescue Stripe::APIConnectionError => e + logger.error e + rescue Stripe::StripeError => e + logger.error e + rescue => e + logger.error e + end + end end