lib/pay/braintree/subscription.rb in pay-6.8.1 vs lib/pay/braintree/subscription.rb in pay-7.0.0
- old
+ new
@@ -33,12 +33,13 @@
attributes = {
created_at: object.created_at,
current_period_end: object.billing_period_end_date,
current_period_start: object.billing_period_start_date,
+ payment_method_id: object.payment_method_token,
processor_plan: object.plan_id,
- status: object.status.underscore,
+ status: object.status.parameterize(separator: "_"),
trial_ends_at: (object.created_at + object.trial_duration.send(object.trial_duration_unit) if object.trial_period)
}
# Canceled subscriptions should have access through the paid_through_date or updated_at
if object.status == "Canceled"
@@ -75,10 +76,13 @@
def subscription(**options)
gateway.subscription.find(processor_id)
end
def cancel(**options)
+ return if canceled?
+
+ # Braintree doesn't allow canceling at period end while on trial, so trials are canceled immediately
result = if on_trial?
gateway.subscription.cancel(processor_id)
else
gateway.subscription.update(subscription.id, {
number_of_billing_cycles: subscription.current_billing_cycle
@@ -88,10 +92,12 @@
rescue ::Braintree::BraintreeError => e
raise Pay::Braintree::Error, e
end
def cancel_now!(**options)
+ return if canceled?
+
result = gateway.subscription.cancel(processor_id)
pay_subscription.sync!(object: result.subscription)
rescue ::Braintree::BraintreeError => e
raise Pay::Braintree::Error, e
end
@@ -110,11 +116,15 @@
def pause
raise NotImplementedError, "Braintree does not support pausing subscriptions"
end
+ def resumable?
+ on_grace_period?
+ end
+
def resume
- unless on_grace_period?
+ unless resumable?
raise StandardError, "You can only resume subscriptions within their grace period."
end
if canceled? && on_trial?
duration = trial_ends_at.to_date - Date.today