Class: SolidusStripe::PaymentIntent
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- SolidusStripe::PaymentIntent
- Defined in:
- app/models/solidus_stripe/payment_intent.rb
Class Method Summary collapse
- .create_for_payment(payment, **stripe_intent_options) ⇒ Object
- .prepare_for_payment(payment, **stripe_creation_options) ⇒ Object
- .retrieve_for_payment(payment) ⇒ Object
Instance Method Summary collapse
- #create_stripe_intent(payment, **stripe_intent_options) ⇒ Object
- #process_payment ⇒ Object
- #reload ⇒ Object
- #stripe_intent ⇒ Object
- #stripe_order_amount ⇒ Object
- #usable? ⇒ Boolean
Class Method Details
.create_for_payment(payment, **stripe_intent_options) ⇒ Object
18 19 20 21 |
# File 'app/models/solidus_stripe/payment_intent.rb', line 18 def self.create_for_payment(payment, **) new(payment_method: payment.payment_method, order: payment.order) .tap { _1.update!(stripe_intent_id: _1.create_stripe_intent(payment, **).id) } end |
.prepare_for_payment(payment, **stripe_creation_options) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'app/models/solidus_stripe/payment_intent.rb', line 8 def self.prepare_for_payment(payment, **) # Find or create the intent for the payment. intent = retrieve_for_payment(payment) || create_for_payment(payment, **) # Attach the payment intent to the payment. payment.update!(response_code: intent.stripe_intent.id) intent end |
.retrieve_for_payment(payment) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/solidus_stripe/payment_intent.rb', line 23 def self.retrieve_for_payment(payment) intent = where(payment_method: payment.payment_method, order: payment.order).last return unless intent&.usable? # Update the intent with the previously acquired payment method. intent.payment_method.gateway.request { Stripe::PaymentIntent.update( intent.stripe_intent_id, payment_method: payment.source.stripe_payment_method_id, payment_method_types: [payment.source.stripe_payment_method.type], ) } intent end |
Instance Method Details
#create_stripe_intent(payment, **stripe_intent_options) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/models/solidus_stripe/payment_intent.rb', line 104 def create_stripe_intent(payment, **) stripe_customer_id = SolidusStripe::Customer.retrieve_or_create_stripe_customer_id( payment_method: payment_method, order: order ) payment_method.gateway.request do Stripe::PaymentIntent.create({ amount: stripe_order_amount, currency: order.currency, capture_method: payment_method.auto_capture? ? 'automatic' : 'manual', setup_future_usage: payment_method.preferred_setup_future_usage.presence, customer: stripe_customer_id, payment_method: payment.source.stripe_payment_method_id, payment_method_types: [payment.source.stripe_payment_method.type], metadata: { solidus_order_number: order.number }, **, }) end end |
#process_payment ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'app/models/solidus_stripe/payment_intent.rb', line 54 def process_payment payment = order.payments.valid.find_by!( payment_method: payment_method, response_code: stripe_intent.id, ) payment.started_processing! case stripe_intent.status when 'processing' successful = true when 'requires_capture' payment.pend! unless payment.pending? successful = true when 'succeeded' payment.complete! unless payment.completed? successful = true else payment.failure! successful = false end SolidusStripe::LogEntries.payment_log( payment, success: successful, message: I18n.t("solidus_stripe.intent_status.#{stripe_intent.status}"), data: stripe_intent, ) if successful order.complete! order.user.wallet.add(payment.source) if order.user && stripe_intent.setup_future_usage.present? else order.payment_failed! end successful end |
#reload ⇒ Object
99 100 101 102 |
# File 'app/models/solidus_stripe/payment_intent.rb', line 99 def reload(...) @stripe_intent = nil super end |
#stripe_intent ⇒ Object
93 94 95 96 97 |
# File 'app/models/solidus_stripe/payment_intent.rb', line 93 def stripe_intent @stripe_intent ||= payment_method.gateway.request do Stripe::PaymentIntent.retrieve(stripe_intent_id) end end |
#stripe_order_amount ⇒ Object
47 48 49 50 51 52 |
# File 'app/models/solidus_stripe/payment_intent.rb', line 47 def stripe_order_amount payment_method.gateway.to_stripe_amount( order.display_order_total_after_store_credit.money.fractional, order.currency, ) end |
#usable? ⇒ Boolean
40 41 42 43 44 45 |
# File 'app/models/solidus_stripe/payment_intent.rb', line 40 def usable? stripe_intent_id && (stripe_intent.status == 'requires_payment_method' || stripe_intent.status == 'requires_confirmation') && stripe_intent.amount == stripe_order_amount end |