Class: Pay::Stripe::Billable
- Inherits:
-
Object
- Object
- Pay::Stripe::Billable
- Defined in:
- lib/pay/stripe/billable.rb
Instance Attribute Summary collapse
-
#pay_customer ⇒ Object
readonly
Returns the value of attribute pay_customer.
Class Method Summary collapse
Instance Method Summary collapse
- #add_payment_method(payment_method_id, default: false) ⇒ Object
- #authorize(amount, options = {}) ⇒ Object
- #billing_portal(**options) ⇒ Object
-
#charge(amount, options = {}) ⇒ Object
Charges an amount to the customer’s default payment method.
- #checkout(**options) ⇒ Object
- #checkout_charge(amount:, name:, quantity: 1, **options) ⇒ Object
-
#create_payment_intent(amount, options = {}) ⇒ Object
Creates and returns a Stripe::PaymentIntent.
- #create_setup_intent(options = {}) ⇒ Object
-
#customer ⇒ Object
Retrieves a Stripe::Customer object.
-
#customer_attributes ⇒ Object
Returns a hash of attributes for the Stripe::Customer object.
-
#initialize(pay_customer) ⇒ Billable
constructor
A new instance of Billable.
- #invoice!(options = {}) ⇒ Object
- #processor_subscription(subscription_id, options = {}) ⇒ Object
-
#save_payment_method(payment_method, default:) ⇒ Object
Save the Stripe::PaymentMethod to the database.
- #subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options) ⇒ Object
-
#sync_subscriptions(**options) ⇒ Object
Syncs a customer’s subscriptions from Stripe to the database.
-
#terminal_charge(amount, options = {}) ⇒ Object
Used for creating Stripe Terminal charges.
- #trial_end_date(stripe_sub) ⇒ Object
- #upcoming_invoice ⇒ Object
-
#update_customer!(**attributes) ⇒ Object
Syncs name and email to Stripe::Customer You can also pass in other attributes that will be merged into the default attributes.
Constructor Details
#initialize(pay_customer) ⇒ Billable
Returns a new instance of Billable.
21 22 23 |
# File 'lib/pay/stripe/billable.rb', line 21 def initialize(pay_customer) @pay_customer = pay_customer end |
Instance Attribute Details
#pay_customer ⇒ Object (readonly)
Returns the value of attribute pay_customer.
6 7 8 |
# File 'lib/pay/stripe/billable.rb', line 6 def pay_customer @pay_customer end |
Class Method Details
.default_url_options ⇒ Object
17 18 19 |
# File 'lib/pay/stripe/billable.rb', line 17 def self. Rails.application.config.action_mailer. || {} end |
Instance Method Details
#add_payment_method(payment_method_id, default: false) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/pay/stripe/billable.rb', line 143 def add_payment_method(payment_method_id, default: false) customer unless processor_id? payment_method = ::Stripe::PaymentMethod.attach(payment_method_id, {customer: processor_id}, ) if default ::Stripe::Customer.update(processor_id, { invoice_settings: { default_payment_method: payment_method.id } }, ) end save_payment_method(payment_method, default: default) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#authorize(amount, options = {}) ⇒ Object
288 289 290 |
# File 'lib/pay/stripe/billable.rb', line 288 def (amount, = {}) charge(amount, .merge(capture_method: :manual)) end |
#billing_portal(**options) ⇒ Object
279 280 281 282 283 284 285 286 |
# File 'lib/pay/stripe/billable.rb', line 279 def billing_portal(**) customer unless processor_id? args = { customer: processor_id, return_url: .delete(:return_url) || root_url } ::Stripe::BillingPortal::Session.create(args.merge(), ) end |
#charge(amount, options = {}) ⇒ Object
Charges an amount to the customer’s default payment method
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/pay/stripe/billable.rb', line 81 def charge(amount, = {}) add_payment_method(payment_method_token, default: true) if payment_method_token? payment_method = pay_customer.default_payment_method args = { confirm: true, payment_method: payment_method&.processor_id }.merge() payment_intent = create_payment_intent(amount, args) Pay::Payment.new(payment_intent).validate charge = payment_intent.latest_charge Pay::Stripe::Charge.sync(charge.id, object: charge) rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#checkout(**options) ⇒ Object
stripe.com/docs/api/checkout/sessions/create
checkout(mode: “payment”) checkout(mode: “setup”) checkout(mode: “subscription”)
checkout(line_items: “price_12345”, quantity: 2) checkout(line_items: [{ price: “price_123” }, { price: “price_456” }]) checkout(line_items: “price_12345”, allow_promotion_codes: true)
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/pay/stripe/billable.rb', line 223 def checkout(**) customer unless processor_id? args = { customer: processor_id, mode: "payment" } # Hosted (the default) checkout sessions require a success_url and cancel_url if ["", "hosted"].include? [:ui_mode].to_s args[:success_url] = merge_session_id_param(.delete(:success_url) || root_url) args[:cancel_url] = merge_session_id_param(.delete(:cancel_url) || root_url) end if [:return_url] args[:return_url] = merge_session_id_param(.delete(:return_url)) end # Line items are optional if (line_items = .delete(:line_items)) quantity = .delete(:quantity) || 1 args[:line_items] = Array.wrap(line_items).map { |item| if item.is_a? Hash item else { price: item, quantity: quantity } end } end ::Stripe::Checkout::Session.create(args.merge(), ) end |
#checkout_charge(amount:, name:, quantity: 1, **options) ⇒ Object
stripe.com/docs/api/checkout/sessions/create
checkout_charge(amount: 15_00, name: “T-shirt”, quantity: 2)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/pay/stripe/billable.rb', line 263 def checkout_charge(amount:, name:, quantity: 1, **) customer unless processor_id? currency = .delete(:currency) || "usd" checkout( line_items: { price_data: { currency: currency, product_data: {name: name}, unit_amount: amount }, quantity: quantity }, ** ) end |
#create_payment_intent(amount, options = {}) ⇒ Object
Creates and returns a Stripe::PaymentIntent
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/pay/stripe/billable.rb', line 100 def create_payment_intent(amount, = {}) args = { amount: amount, currency: "usd", customer: processor_id, expand: ["latest_charge.refunds"], return_url: root_url }.merge() ::Stripe::PaymentIntent.create(args, ) end |
#create_setup_intent(options = {}) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/pay/stripe/billable.rb', line 189 def create_setup_intent( = {}) customer unless processor_id? ::Stripe::SetupIntent.create({ customer: processor_id, usage: :off_session }.merge(), ) end |
#customer ⇒ Object
Retrieves a Stripe::Customer object
Finds an existing Stripe::Customer if processor_id exists Creates a new Stripe::Customer using ‘customer_attributes` if empty processor_id
Updates the default payment method automatically if a payment_method_token is set
Returns a Stripe::Customer object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/pay/stripe/billable.rb', line 50 def customer stripe_customer = if processor_id? ::Stripe::Customer.retrieve({id: processor_id, expand: ["tax", "invoice_credit_balance"]}, ) else sc = ::Stripe::Customer.create(customer_attributes.merge(expand: ["tax"]), ) pay_customer.update!(processor_id: sc.id, stripe_account: stripe_account) sc end if payment_method_token? add_payment_method(payment_method_token, default: true) pay_customer.payment_method_token = nil end stripe_customer rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#customer_attributes ⇒ Object
Returns a hash of attributes for the Stripe::Customer object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/pay/stripe/billable.rb', line 26 def customer_attributes owner = pay_customer.owner attributes = case owner.class.pay_stripe_customer_attributes when Symbol owner.send(owner.class.pay_stripe_customer_attributes, pay_customer) when Proc owner.class.pay_stripe_customer_attributes.call(pay_customer) end # Guard against attributes being returned nil attributes ||= {} {email: email, name: customer_name}.merge(attributes) end |
#invoice!(options = {}) ⇒ Object
180 181 182 183 |
# File 'lib/pay/stripe/billable.rb', line 180 def invoice!( = {}) return unless processor_id? ::Stripe::Invoice.create(.merge(customer: processor_id), ).pay end |
#processor_subscription(subscription_id, options = {}) ⇒ Object
176 177 178 |
# File 'lib/pay/stripe/billable.rb', line 176 def processor_subscription(subscription_id, = {}) ::Stripe::Subscription.retrieve(.merge(id: subscription_id), ) end |
#save_payment_method(payment_method, default:) ⇒ Object
Save the Stripe::PaymentMethod to the database
161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/pay/stripe/billable.rb', line 161 def save_payment_method(payment_method, default:) pay_payment_method = pay_customer.payment_methods.where(processor_id: payment_method.id).first_or_initialize attributes = Pay::Stripe::PaymentMethod.extract_attributes(payment_method).merge(default: default) # Ignore the payment method if it's already in the database pay_customer.payment_methods.where.not(id: pay_payment_method.id).update_all(default: false) if default pay_payment_method.update!(attributes) # Reload the Rails association pay_customer.reload_default_payment_method pay_payment_method end |
#subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **options) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/pay/stripe/billable.rb', line 117 def subscribe(name: Pay.default_product_name, plan: Pay.default_plan_name, **) quantity = .delete(:quantity) opts = { expand: ["pending_setup_intent", "latest_invoice.payment_intent", "latest_invoice.charge"], items: [plan: plan, quantity: quantity] }.merge() # Load the Stripe customer to verify it exists and update payment method if needed opts[:customer] = customer.id # Create subscription on Stripe stripe_sub = ::Stripe::Subscription.create(opts.merge(Pay::Stripe::Subscription.), ) # Save Pay::Subscription subscription = Pay::Stripe::Subscription.sync(stripe_sub.id, object: stripe_sub, name: name) # No trial, payment method requires SCA if [:payment_behavior].to_s != "default_incomplete" && subscription.incomplete? Pay::Payment.new(stripe_sub.latest_invoice.payment_intent).validate end subscription rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#sync_subscriptions(**options) ⇒ Object
Syncs a customer’s subscriptions from Stripe to the database. Note that by default canceled subscriptions are NOT returned by Stripe. In order to include them, use ‘sync_subscriptions(status: “all”)`.
204 205 206 207 208 209 210 211 |
# File 'lib/pay/stripe/billable.rb', line 204 def sync_subscriptions(**) subscriptions = ::Stripe::Subscription.list(.merge(customer: customer), ) subscriptions.map do |subscription| Pay::Stripe::Subscription.sync(subscription.id) end rescue ::Stripe::StripeError => e raise Pay::Stripe::Error, e end |
#terminal_charge(amount, options = {}) ⇒ Object
Used for creating Stripe Terminal charges
113 114 115 |
# File 'lib/pay/stripe/billable.rb', line 113 def terminal_charge(amount, = {}) create_payment_intent(amount, .merge(payment_method_types: ["card_present"], capture_method: "manual")) end |
#trial_end_date(stripe_sub) ⇒ Object
197 198 199 200 |
# File 'lib/pay/stripe/billable.rb', line 197 def trial_end_date(stripe_sub) # Times in Stripe are returned in UTC stripe_sub.trial_end.present? ? Time.at(stripe_sub.trial_end) : nil end |
#upcoming_invoice ⇒ Object
185 186 187 |
# File 'lib/pay/stripe/billable.rb', line 185 def upcoming_invoice ::Stripe::Invoice.upcoming({customer: processor_id}, ) end |
#update_customer!(**attributes) ⇒ Object
Syncs name and email to Stripe::Customer You can also pass in other attributes that will be merged into the default attributes
71 72 73 74 75 76 77 78 |
# File 'lib/pay/stripe/billable.rb', line 71 def update_customer!(**attributes) customer unless processor_id? ::Stripe::Customer.update( processor_id, customer_attributes.merge(attributes), ) end |