Module: StripeMock::RequestHandlers::Subscriptions
- Included in:
- Instance
- Defined in:
- lib/stripe_mock/request_handlers/subscriptions.rb
Class Method Summary collapse
Instance Method Summary collapse
- #cancel_subscription(route, method_url, params, headers) ⇒ Object
- #create_customer_subscription(route, method_url, params, headers) ⇒ Object
- #create_subscription(route, method_url, params, headers) ⇒ Object
- #retrieve_customer_subscription(route, method_url, params, headers) ⇒ Object
- #retrieve_customer_subscriptions(route, method_url, params, headers) ⇒ Object
- #retrieve_subscription(route, method_url, params, headers) ⇒ Object
- #retrieve_subscriptions(route, method_url, params, headers) ⇒ Object
- #update_subscription(route, method_url, params, headers) ⇒ Object
Class Method Details
.included(klass) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/stripe_mock/request_handlers/subscriptions.rb', line 5 def Subscriptions.included(klass) klass.add_handler 'get /v1/subscriptions', :retrieve_subscriptions klass.add_handler 'post /v1/subscriptions', :create_subscription klass.add_handler 'get /v1/subscriptions/(.*)', :retrieve_subscription klass.add_handler 'post /v1/subscriptions/(.*)', :update_subscription klass.add_handler 'delete /v1/subscriptions/(.*)', :cancel_subscription klass.add_handler 'post /v1/customers/(.*)/subscriptions', :create_customer_subscription klass.add_handler 'get /v1/customers/(.*)/subscriptions/(.*)', :retrieve_customer_subscription klass.add_handler 'get /v1/customers/(.*)/subscriptions', :retrieve_customer_subscriptions end |
Instance Method Details
#cancel_subscription(route, method_url, params, headers) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/stripe_mock/request_handlers/subscriptions.rb', line 173 def cancel_subscription(route, method_url, params, headers) route =~ method_url subscription = assert_existence :subscription, $1, subscriptions[$1] customer_id = subscription[:customer] customer = assert_existence :customer, customer_id, customers[customer_id] cancel_params = { canceled_at: Time.now.utc.to_i } cancelled_at_period_end = (params[:at_period_end] == true) if cancelled_at_period_end cancel_params[:cancel_at_period_end] = true else cancel_params[:status] = "canceled" cancel_params[:cancel_at_period_end] = false cancel_params[:ended_at] = Time.now.utc.to_i end subscription.merge!(cancel_params) unless cancelled_at_period_end delete_subscription_from_customer customer, subscription end subscription end |
#create_customer_subscription(route, method_url, params, headers) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/stripe_mock/request_handlers/subscriptions.rb', line 33 def create_customer_subscription(route, method_url, params, headers) route =~ method_url plan_id = params[:plan].to_s plan = assert_existence :plan, plan_id, plans[plan_id] customer = assert_existence :customer, $1, customers[$1] if params[:source] new_card = get_card_by_token(params.delete(:source)) add_card_to_object(:customer, new_card, customer) customer[:default_source] = new_card[:id] end # Ensure customer has card to charge if plan has no trial and is not free verify_card_present(customer, plan, params) subscription = Data.mock_subscription({ id: (params[:id] || new_id('su')) }) subscription.merge!(custom_subscription_params(plan, customer, params)) if params[:coupon] coupon_id = params[:coupon] raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400) unless coupons[coupon_id] # FIXME assert_existence returns 404 error code but Stripe returns 400 # coupon = assert_existence :coupon, coupon_id, coupons[coupon_id] coupon = Data.mock_coupon({ id: coupon_id }) subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {}) end subscriptions[subscription[:id]] = subscription add_subscription_to_customer(customer, subscription) subscriptions[subscription[:id]] end |
#create_subscription(route, method_url, params, headers) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 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/stripe_mock/request_handlers/subscriptions.rb', line 72 def create_subscription(route, method_url, params, headers) route =~ method_url plan_id = params[:plan].to_s plan = assert_existence :plan, plan_id, plans[plan_id] customer_id = params[:customer].to_s customer = assert_existence :customer, customer_id, customers[customer_id] if params[:source] new_card = get_card_by_token(params.delete(:source)) add_card_to_object(:customer, new_card, customer) customer[:default_source] = new_card[:id] end # Ensure customer has card to charge if plan has no trial and is not free verify_card_present(customer, plan, params) subscription = Data.mock_subscription({ id: (params[:id] || new_id('su')) }) subscription.merge!(custom_subscription_params(plan, customer, params)) if params[:coupon] coupon_id = params[:coupon] raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400) unless coupons[coupon_id] # FIXME assert_existence returns 404 error code but Stripe returns 400 # coupon = assert_existence :coupon, coupon_id, coupons[coupon_id] coupon = Data.mock_coupon({ id: coupon_id }) subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {}) end subscriptions[subscription[:id]] = subscription add_subscription_to_customer(customer, subscription) subscriptions[subscription[:id]] end |
#retrieve_customer_subscription(route, method_url, params, headers) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/stripe_mock/request_handlers/subscriptions.rb', line 17 def retrieve_customer_subscription(route, method_url, params, headers) route =~ method_url customer = :customer, $1, customers[$1] subscription = get_customer_subscription(customer, $2) assert_existence :subscription, $2, subscription end |
#retrieve_customer_subscriptions(route, method_url, params, headers) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/stripe_mock/request_handlers/subscriptions.rb', line 26 def retrieve_customer_subscriptions(route, method_url, params, headers) route =~ method_url customer = assert_existence :customer, $1, customers[$1] customer[:subscriptions] end |
#retrieve_subscription(route, method_url, params, headers) ⇒ Object
112 113 114 115 116 |
# File 'lib/stripe_mock/request_handlers/subscriptions.rb', line 112 def retrieve_subscription(route, method_url, params, headers) route =~ method_url assert_existence :subscription, $1, subscriptions[$1] end |
#retrieve_subscriptions(route, method_url, params, headers) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/stripe_mock/request_handlers/subscriptions.rb', line 118 def retrieve_subscriptions(route, method_url, params, headers) route =~ method_url Data.mock_list_object(subscriptions.values, params) #customer = assert_existence :customer, $1, customers[$1] #customer[:subscriptions] end |
#update_subscription(route, method_url, params, headers) ⇒ Object
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/stripe_mock/request_handlers/subscriptions.rb', line 126 def update_subscription(route, method_url, params, headers) route =~ method_url subscription = assert_existence :subscription, $1, subscriptions[$1] customer_id = subscription[:customer] customer = assert_existence :customer, customer_id, customers[customer_id] if params[:source] new_card = get_card_by_token(params.delete(:source)) add_card_to_object(:customer, new_card, customer) customer[:default_source] = new_card[:id] end # expand the plan for addition to the customer object plan_name = params[:plan] if params[:plan] && params[:plan] != {} plan_name ||= subscription[:plan][:id] plan = plans[plan_name] if params[:coupon] coupon_id = params[:coupon] raise Stripe::InvalidRequestError.new("No such coupon: #{coupon_id}", 'coupon', 400) unless coupons[coupon_id] # FIXME assert_existence returns 404 error code but Stripe returns 400 # coupon = assert_existence :coupon, coupon_id, coupons[coupon_id] coupon = Data.mock_coupon({ id: coupon_id }) subscription[:discount] = Stripe::Util.convert_to_stripe_object({ coupon: coupon }, {}) end assert_existence :plan, plan_name, plan params[:plan] = plan if params[:plan] verify_card_present(customer, plan) if subscription[:cancel_at_period_end] subscription[:cancel_at_period_end] = false subscription[:canceled_at] = nil end subscription.merge!(custom_subscription_params(plan, customer, params)) # delete the old subscription, replace with the new subscription customer[:subscriptions][:data].reject! { |sub| sub[:id] == subscription[:id] } customer[:subscriptions][:data] << subscription subscription end |