diff --git a/app/models/payment_schedule.rb b/app/models/payment_schedule.rb index c3edf9758..0b3b4ec6f 100644 --- a/app/models/payment_schedule.rb +++ b/app/models/payment_schedule.rb @@ -85,6 +85,10 @@ class PaymentSchedule < PaymentDocument PaymentGatewayService.new.pay_subscription(self, gateway_method_id) end + def post_save_extend(gateway_method_id) + PaymentGatewayService.new.extend_subscription(self, gateway_method_id) + end + def render_resource { partial: 'api/payment_schedules/payment_schedule', locals: { payment_schedule: self } } end diff --git a/app/services/payment_gateway_service.rb b/app/services/payment_gateway_service.rb index ef98896e5..48a7940fa 100644 --- a/app/services/payment_gateway_service.rb +++ b/app/services/payment_gateway_service.rb @@ -27,6 +27,10 @@ class PaymentGatewayService @gateway.pay_subscription(payment_schedule, gateway_object_id) end + def extend_subscription(payment_schedule, gateway_object_id) + @gateway.extend_subscription(payment_schedule, gateway_object_id) + end + def create_user(user_id) @gateway.create_user(user_id) end diff --git a/app/services/subscriptions/subscribe.rb b/app/services/subscriptions/subscribe.rb index 806aa4d37..13dfac28b 100644 --- a/app/services/subscriptions/subscribe.rb +++ b/app/services/subscriptions/subscribe.rb @@ -54,7 +54,7 @@ class Subscriptions::Subscribe ) end payment.save - payment.post_save(schedule&.gateway_payment_mean&.id) + payment.post_save_extend(schedule&.gateway_payment_mean&.id) UsersCredits::Manager.new(user: new_sub.user).reset_credits return new_sub end diff --git a/lib/pay_zen/service.rb b/lib/pay_zen/service.rb index b09678bd8..b8158892c 100644 --- a/lib/pay_zen/service.rb +++ b/lib/pay_zen/service.rb @@ -48,6 +48,10 @@ class PayZen::Service < Payment::Service pgo_sub.save! end + def extend_subscription(payment_schedule, payment_method_id) + create_subscription(payment_schedule, payment_method_id) + end + def process_payment_schedule_item(payment_schedule_item) pz_order = payment_schedule_item.payment_schedule.gateway_order.retrieve transaction = pz_order['answer']['transactions'].last diff --git a/lib/stripe/item.rb b/lib/stripe/item.rb index 903ea497b..a81e2c671 100644 --- a/lib/stripe/item.rb +++ b/lib/stripe/item.rb @@ -15,7 +15,7 @@ class Stripe::Item < Payment::Item end def payment_mean? - klass == 'Stripe::SetupIntent' + klass == 'Stripe::PaymentMethod' end def subscription? diff --git a/lib/stripe/service.rb b/lib/stripe/service.rb index 71a0eeb2b..f904898a9 100644 --- a/lib/stripe/service.rb +++ b/lib/stripe/service.rb @@ -14,8 +14,11 @@ class Stripe::Service < Payment::Service handle_wallet_transaction(payment_schedule) stp_subscription = Stripe::Subscription.retrieve(subscription_id, api_key: stripe_key) + + payment_method_id = stp_subscription.default_payment_method + payment_method = Stripe::PaymentMethod.retrieve(payment_method_id, api_key: stripe_key) pgo = PaymentGatewayObject.new(item: payment_schedule) - pgo.gateway_object = stp_subscription + pgo.gateway_object = payment_method pgo.save! end @@ -56,6 +59,16 @@ class Stripe::Service < Payment::Service }, { api_key: stripe_key }) end + def extend_subscription(payment_schedule, payment_method_id) + stp_subscription = pay_subscription(payment_schedule, payment_method_id) + + handle_wallet_transaction(payment_schedule) + + pgo = PaymentGatewayObject.new(item: payment_schedule) + pgo.gateway_object = stp_subscription + pgo.save! + end + def create_user(user_id) StripeWorker.perform_async(:create_stripe_customer, user_id) end