Class: ActiveMerchant::Billing::StripePaymentIntentsGateway
- Inherits:
-
StripeGateway
- Object
- Gateway
- StripeGateway
- ActiveMerchant::Billing::StripePaymentIntentsGateway
- Defined in:
- lib/active_merchant/billing/gateways/stripe_payment_intents.rb
Overview
This gateway uses the current Stripe Payment Intents API. For the legacy API, see the Stripe gateway
Constant Summary collapse
- ALLOWED_METHOD_STATES =
%w[automatic manual].freeze
- ALLOWED_CANCELLATION_REASONS =
%w[duplicate fraudulent requested_by_customer abandoned].freeze
- CREATE_INTENT_ATTRIBUTES =
%i[description statement_descriptor_suffix statement_descriptor receipt_email save_payment_method]
- CONFIRM_INTENT_ATTRIBUTES =
%i[receipt_email return_url save_payment_method setup_future_usage off_session]
- UPDATE_INTENT_ATTRIBUTES =
%i[description statement_descriptor_suffix statement_descriptor receipt_email setup_future_usage]
- ALLOWED_MULTICAPTURE_OPTIONS =
%w[if_available never].freeze
- DIGITAL_WALLETS =
{ apple_pay: 'apple_pay', google_pay: 'google_pay_dpan' }
Constants inherited from StripeGateway
ActiveMerchant::Billing::StripeGateway::AVS_CODE_TRANSLATOR, ActiveMerchant::Billing::StripeGateway::BANK_ACCOUNT_HOLDER_TYPE_MAPPING, ActiveMerchant::Billing::StripeGateway::CVC_CODE_TRANSLATOR, ActiveMerchant::Billing::StripeGateway::MINIMUM_AUTHORIZE_AMOUNTS, ActiveMerchant::Billing::StripeGateway::STANDARD_ERROR_CODE_MAPPING
Constants inherited from Gateway
Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::RECURRING_DEPRECATION_MESSAGE, Gateway::STANDARD_ERROR_CODE
Instance Attribute Summary
Attributes inherited from Gateway
Instance Method Summary collapse
- #add_payment_method_card_data_token(post_data, payment_method) ⇒ Object
- #add_payment_method_data(payment_method, options = {}) ⇒ Object
- #authorize(money, payment_method, options = {}) ⇒ Object
- #capture(money, intent_id, options = {}) ⇒ Object
- #confirm_intent(intent_id, payment_method, options = {}) ⇒ Object
- #create_intent(money, payment_method, options = {}) ⇒ Object
- #create_payment_method(payment_method, options = {}) ⇒ Object
- #create_setup_intent(payment_method, options = {}) ⇒ Object
- #create_test_customer ⇒ Object
- #customer(payment, options) ⇒ Object
- #inquire(authorization, options = {}) ⇒ Object
- #new_apple_google_pay_flow(payment_method, options) ⇒ Object
- #purchase(money, payment_method, options = {}) ⇒ Object
- #refund(money, intent_id, options = {}) ⇒ Object
- #retrieve_setup_intent(setup_intent_id, options = {}) ⇒ Object
- #setup_purchase(money, options = {}) ⇒ Object
- #show_intent(intent_id, options) ⇒ Object
-
#store(payment_method, options = {}) ⇒ Object
Note: Not all payment methods are currently supported by the Payment Methods API Current implementation will create a PaymentMethod object if the method is a token or credit card All other types will default to legacy Stripe store.
- #supports_network_tokenization? ⇒ Boolean
- #unstore(identification, options = {}, deprecated_options = {}) ⇒ Object
- #update_intent(money, intent_id, payment_method, options = {}) ⇒ Object
- #verify(payment_method, options = {}) ⇒ Object
- #void(intent_id, options = {}) ⇒ Object
Methods inherited from StripeGateway
#delete_latest_test_external_account, #initialize, #refund_application_fee, #scrub, #supports_scrubbing?, #update, #update_customer, #verify_credentials
Methods inherited from Gateway
#add_field_to_post_if_present, #add_fields_to_post_if_present, #card_brand, card_brand, #generate_unique_id, inherited, #initialize, #scrub, #supported_countries, supported_countries, supported_countries=, supports?, #supports_scrubbing?, #test?
Methods included from Versionable
Methods included from CreditCardFormatting
#expdate, #format, #strftime_yyyymm, #strftime_yyyymmdd_last_day
Methods included from PostsData
included, #raw_ssl_request, #ssl_get, #ssl_post, #ssl_request
Constructor Details
This class inherits a constructor from ActiveMerchant::Billing::StripeGateway
Instance Method Details
#add_payment_method_card_data_token(post_data, payment_method) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 136 def add_payment_method_card_data_token(post_data, payment_method) post_data.merge!({ payment_method_types: ['card'], payment_method_data: { type: 'card', card: { token: payment_method } } }) end |
#add_payment_method_data(payment_method, options = {}) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 112 def add_payment_method_data(payment_method, = {}) post = { type: 'card', card: { exp_month: payment_method.month, exp_year: payment_method.year } } post[:card][:number] = payment_method.number unless adding_network_token_card_data?(payment_method) post[:card][:cvc] = payment_method.verification_value if payment_method.verification_value if billing = [:billing_address] || [:address] post[:billing_details] = add_address(billing, ) end # wallet_type is only passed for non-tokenized GooglePay which acts as a CreditCard if [:wallet_type] post[:metadata] ||= {} post[:metadata][:input_method] = 'GooglePay' end add_name_only(post, payment_method) if post[:billing_details].nil? add_network_token_data(post, payment_method, ) post end |
#authorize(money, payment_method, options = {}) ⇒ Object
203 204 205 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 203 def (money, payment_method, = {}) create_intent(money, payment_method, .merge!(confirm: true, capture_method: 'manual')) end |
#capture(money, intent_id, options = {}) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 211 def capture(money, intent_id, = {}) return Response.new(false, 'Only Authorizations performed via the Payment Intent API are capturable') unless intent_id.include?('pi_') post = {} currency = [:currency] || currency(money) post[:amount_to_capture] = localized_amount(money, currency) if [:transfer_amount] post[:transfer_data] = {} post[:transfer_data][:amount] = [:transfer_amount] end post[:application_fee_amount] = [:application_fee] if [:application_fee] post[:final_capture] = normalize([:final_capture]) unless [:final_capture].nil? = format_idempotency_key(, 'capture') commit(:post, "payment_intents/#{intent_id}/capture", post, ) end |
#confirm_intent(intent_id, payment_method, options = {}) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 82 def confirm_intent(intent_id, payment_method, = {}) post = {} if new_apple_google_pay_flow(payment_method, ) add_digital_wallet(post, payment_method, ) else result = add_payment_method_token(post, payment_method, ) return result if result.is_a?(ActiveMerchant::Billing::Response) end add_network_token_info(post, payment_method, ) add_payment_method_types(post, ) CONFIRM_INTENT_ATTRIBUTES.each do |attribute| add_whitelisted_attribute(post, , attribute) end commit(:post, "payment_intents/#{intent_id}/confirm", post, ) end |
#create_intent(money, payment_method, options = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 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 71 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 21 def create_intent(money, payment_method, = {}) MultiResponse.run do |r| if payment_method.is_a?(NetworkTokenizationCreditCard) && digital_wallet_payment_method?(payment_method) && [:new_ap_gp_route] != true r.process { tokenize_apple_google(payment_method, ) } payment_method = (r.params['token']['id']) if r.success? end r.process do post = {} add_amount(post, money, , true) add_capture_method(post, ) add_confirmation_method(post, ) add_customer(post, ) if new_apple_google_pay_flow(payment_method, ) add_digital_wallet(post, payment_method, ) add_billing_address(post, payment_method, ) else result = add_payment_method_token(post, payment_method, ) return result if result.is_a?(ActiveMerchant::Billing::Response) end add_network_token_info(post, payment_method, ) add_external_three_d_secure_auth_data(post, ) (post, ) add_return_url(post, ) add_connected_account(post, ) add_radar_data(post, ) add_shipping_address(post, ) add_stored_credentials(post, ) setup_future_usage(post, ) add_exemption(post, ) add_ntid(post, ) add_claim_without_transaction_id(post, ) add_error_on_requires_action(post, ) add_fulfillment_date(post, ) request_three_d_secure(post, ) add_level_three(post, ) add_card_brand(post, ) add_aft_recipient_details(post, ) add_aft_sender_details(post, ) (post, ) add_statement_descriptor_suffix_kanji_kana(post, ) add_request_multicapture(post, ) CREATE_INTENT_ATTRIBUTES.each do |attribute| add_whitelisted_attribute(post, , attribute) end commit(:post, 'payment_intents', post, ) end end end |
#create_payment_method(payment_method, options = {}) ⇒ Object
100 101 102 103 104 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 100 def create_payment_method(payment_method, = {}) post_data = add_payment_method_data(payment_method, ) = format_idempotency_key(, 'pm') commit(:post, 'payment_methods', post_data, ) end |
#create_setup_intent(payment_method, options = {}) ⇒ Object
169 170 171 172 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 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 169 def create_setup_intent(payment_method, = {}) MultiResponse.run do |r| r.process do post = {} add_customer(post, ) if new_apple_google_pay_flow(payment_method, ) add_digital_wallet(post, payment_method, ) add_billing_address(post, payment_method, ) else result = add_payment_method_token(post, payment_method, , r) return result if result.is_a?(ActiveMerchant::Billing::Response) end add_network_token_info(post, payment_method, ) (post, ) add_return_url(post, ) add_fulfillment_date(post, ) request_three_d_secure(post, ) add_card_brand(post, ) add_exemption(post, ) post[:on_behalf_of] = [:on_behalf_of] if [:on_behalf_of] post[:usage] = [:usage] if %w(on_session off_session).include?([:usage]) post[:description] = [:description] if [:description] commit(:post, 'setup_intents', post, ) end end end |
#create_test_customer ⇒ Object
77 78 79 80 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 77 def create_test_customer response = api_request(:post, 'customers') response['id'] end |
#customer(payment, options) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 277 def customer(payment, ) post = {} post[:description] = [:description] if [:description] post[:expand] = [:sources] post[:email] = [:email] if billing = [:billing_address] || [:address] post.merge!(add_address(billing, )) end if shipping = [:shipping_address] post[:shipping] = add_address(shipping, ).except(:email) end = format_idempotency_key(, 'customer') commit(:post, 'customers', post, ) end |
#inquire(authorization, options = {}) ⇒ Object
318 319 320 321 322 323 324 325 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 318 def inquire(, = {}) .merge!({ request_type: :inquire }) if &.start_with?('pi_') show_intent(, ) else retrieve_setup_intent(, ) end end |
#new_apple_google_pay_flow(payment_method, options) ⇒ Object
106 107 108 109 110 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 106 def new_apple_google_pay_flow(payment_method, ) return false unless [:new_ap_gp_route] payment_method.is_a?(NetworkTokenizationCreditCard) && digital_wallet_payment_method?(payment_method) end |
#purchase(money, payment_method, options = {}) ⇒ Object
207 208 209 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 207 def purchase(money, payment_method, = {}) create_intent(money, payment_method, .merge!(confirm: true, capture_method: 'automatic')) end |
#refund(money, intent_id, options = {}) ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 233 def refund(money, intent_id, = {}) if intent_id.include?('pi_') intent = api_request(:get, "payment_intents/#{intent_id}?expand[]=latest_charge.balance_transaction", nil, ) return Response.new(false, intent['error']['message'], intent) if intent['error'] charge_id = intent.try(:[], 'latest_charge').try(:[], 'id') if charge_id.nil? = "No associated charge for #{intent['id']}" << "; payment_intent has a status of #{intent['status']}" if intent.try(:[], 'status') && intent.try(:[], 'status') != 'succeeded' return Response.new(false, , intent) end else charge_id = intent_id end super(money, charge_id, ) end |
#retrieve_setup_intent(setup_intent_id, options = {}) ⇒ Object
199 200 201 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 199 def retrieve_setup_intent(setup_intent_id, = {}) commit(:get, "setup_intents/#{setup_intent_id}?expand[]=latest_attempt", nil, ) end |
#setup_purchase(money, options = {}) ⇒ Object
308 309 310 311 312 313 314 315 316 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 308 def setup_purchase(money, = {}) requires!(, :payment_method_types) post = {} add_currency(post, , money) add_amount(post, money, ) add_payment_method_types(post, ) (post, ) commit(:post, 'payment_intents', post, ) end |
#show_intent(intent_id, options) ⇒ Object
73 74 75 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 73 def show_intent(intent_id, ) commit(:get, "payment_intents/#{intent_id}?expand[]=latest_charge.balance_transaction", nil, ) end |
#store(payment_method, options = {}) ⇒ Object
Note: Not all payment methods are currently supported by the Payment Methods API Current implementation will create a PaymentMethod object if the method is a token or credit card All other types will default to legacy Stripe store
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 256 def store(payment_method, = {}) params = {} # If customer option is provided, create a payment method and attach to customer id # Otherwise, create a customer, then attach if new_apple_google_pay_flow(payment_method, ) [:customer] = customer(payment_method, ).params['id'] unless [:customer] verify(payment_method, .merge!(action: :store)) elsif payment_method.is_a?(ActiveMerchant::Billing::CreditCard) result = add_payment_method_token(params, payment_method, ) return result if result.is_a?(ActiveMerchant::Billing::Response) customer_id = [:customer] || customer(payment_method, ).params['id'] = format_idempotency_key(, 'attach') attach_parameters = { customer: customer_id } attach_parameters[:validate] = [:validate] unless [:validate].nil? commit(:post, "payment_methods/#{params[:payment_method]}/attach", attach_parameters, ) else super(payment_method, ) end end |
#supports_network_tokenization? ⇒ Boolean
327 328 329 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 327 def supports_network_tokenization? true end |
#unstore(identification, options = {}, deprecated_options = {}) ⇒ Object
295 296 297 298 299 300 301 302 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 295 def unstore(identification, = {}, = {}) if identification.include?('pm_') _, payment_method = identification.split('|') commit(:post, "payment_methods/#{payment_method}/detach", nil, ) else super(identification, , ) end end |
#update_intent(money, intent_id, payment_method, options = {}) ⇒ Object
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 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 143 def update_intent(money, intent_id, payment_method, = {}) post = {} add_amount(post, money, ) if new_apple_google_pay_flow(payment_method, ) add_digital_wallet(post, payment_method, ) else result = add_payment_method_token(post, payment_method, ) return result if result.is_a?(ActiveMerchant::Billing::Response) end add_network_token_info(post, payment_method, ) add_payment_method_types(post, ) add_customer(post, ) (post, ) add_shipping_address(post, ) add_connected_account(post, ) add_fulfillment_date(post, ) add_statement_descriptor_suffix_kanji_kana(post, ) UPDATE_INTENT_ATTRIBUTES.each do |attribute| add_whitelisted_attribute(post, , attribute) end commit(:post, "payment_intents/#{intent_id}", post, ) end |
#verify(payment_method, options = {}) ⇒ Object
304 305 306 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 304 def verify(payment_method, = {}) create_setup_intent(payment_method, .merge!({ confirm: true, verify: true })) end |
#void(intent_id, options = {}) ⇒ Object
227 228 229 230 231 |
# File 'lib/active_merchant/billing/gateways/stripe_payment_intents.rb', line 227 def void(intent_id, = {}) post = {} post[:cancellation_reason] = [:cancellation_reason] if ALLOWED_CANCELLATION_REASONS.include?([:cancellation_reason]) commit(:post, "payment_intents/#{intent_id}/cancel", post, ) end |