Module: MuckCommerce::Models::MuckSubscription::ClassMethods
- Defined in:
- lib/muck-commerce/models/subscription.rb
Instance Method Summary collapse
-
#already_subscribed?(user, subscription_plan_id) ⇒ Boolean
Determines if a user is already subscribed to a given plan.
- #authorize_subscription(user, options = {}) ⇒ Object
- #process_past_due_subscriptions ⇒ Object
- #subscribe(user, subscription_plan_id, discount_code = '', authorize_options = {}) ⇒ Object
-
#subscribe_custom(user, amount, subscription_values = {}, renewal_period = nil, discount_codes = [], run_authorization = false) ⇒ Object
Setup a custom subscription.
Instance Method Details
#already_subscribed?(user, subscription_plan_id) ⇒ Boolean
Determines if a user is already subscribed to a given plan
160 161 162 |
# File 'lib/muck-commerce/models/subscription.rb', line 160 def already_subscribed?(user, subscription_plan_id) user.subscriptions.any? { |subscription| subscription.subscription_plan_id == subscription_plan_id } end |
#authorize_subscription(user, options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/muck-commerce/models/subscription.rb', line 69 def (user, = {}) amount = 100 # run a $1 authorization on their credit card User.transaction do begin = OrderTransaction.(amount, user.billing_information, ) raise MuckCommerce::Exceptions::PaymentGatewayError, I18n.t('muck.commerce.billing_information_update_error_friendly', :error => .) if !.success? store_information_response = OrderTransaction.cim_create_update(user.billing_information) rescue => ex #puts pp user.billing_information.credit_card.errors #puts ex success = false raise ex end [success, , store_information_response] end end |
#process_past_due_subscriptions ⇒ Object
164 165 166 167 |
# File 'lib/muck-commerce/models/subscription.rb', line 164 def process_past_due_subscriptions results = past_due.map {|subscription| subscription.charge} ("PAST DUE SUBSCRIPTIONS:", results) end |
#subscribe(user, subscription_plan_id, discount_code = '', authorize_options = {}) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/muck-commerce/models/subscription.rb', line 86 def subscribe(user, subscription_plan_id, discount_code = '', = {}) raise MuckCommerce::Exceptions::SubscriptionError, I18n.t('muck.commerce.already_subscribed') if already_subscribed?(user, subscription_plan_id) subscription_plan = SubscriptionPlan.find(subscription_plan_id) coupon, coupon_amount, coupon_override = Coupon.calculate_total_discount(subscription_plan.amount, discount_code) success, , store_information_response = Subscription.(user, ) if !coupon_override sub = nil if success sub = Subscription.new sub.number = sub.order_number sub.user = user sub.coupon = coupon if coupon sub.subscription_plan = subscription_plan sub.next_renewal_at = DateTime.now.advance(:days => subscription_plan.trial_period.days) sub.renewal_period = subscription_plan.renewal_period if coupon_override sub.amount = 0 else sub.amount = subscription_plan.amount - coupon_amount end sub.save! end [sub, , store_information_response] end |
#subscribe_custom(user, amount, subscription_values = {}, renewal_period = nil, discount_codes = [], run_authorization = false) ⇒ Object
Setup a custom subscription. This is done in the event that every subscription for every user is different. These subscriptions are not associated with a subscription plan because each one is custom. It is assumed that the first bill will not go out for the amount of time specified by renewal_period. This is done so that the first bill can be handled by order.checkout so that there is an order associated with the user. user: User to setup subscription for. amount: Amount of the subscription. Specified in cents. subscription_values: Optional values for the subscription. renewal_period: Amount of time between charges. If no value is provided 1 month will be assumed. discount_codes: An array containing potential discount codes or coupons. These will be checked against the
coupons table and associated with the subscription if they are valid.
run_authorization: Determines whether or not to run an authorization on the user’s billing information to see
if it is valid.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/muck-commerce/models/subscription.rb', line 125 def subscribe_custom(user, amount, subscription_values = {}, renewal_period = nil, discount_codes = [], = false) coupons, coupon_amount, coupon_override = Coupon.calculate_total_discount(amount, discount_codes) if !coupon_override && success, , store_information_response = Subscription.(user, params) else success = true end sub = nil if success sub = Subscription.new(subscription_values) sub.number = sub.order_number sub.user = user sub.coupons << coupons unless coupons.empty? if renewal_period sub.next_renewal_at = DateTime.now.advance(:days => renewal_period) sub.renewal_period = renewal_period else sub.next_renewal_at = DateTime.now.advance(:months => 1) end if coupon_override sub.amount = 0 else sub.amount = amount - coupon_amount end sub.save! sub end [sub, , store_information_response] end |