Class: SolidusAfterpay::Gateway
- Inherits:
-
Object
- Object
- SolidusAfterpay::Gateway
- Defined in:
- app/models/solidus_afterpay/gateway.rb
Constant Summary collapse
- VOIDABLE_STATUSES =
['AUTH_APPROVED', 'PARTIALLY_CAPTURED'].freeze
Instance Method Summary collapse
- #authorize(amount, payment_source, gateway_options) ⇒ Object
- #capture(amount, response_code, gateway_options) ⇒ Object
- #create_checkout(order, gateway_options) ⇒ Object
- #credit(amount, response_code, gateway_options) ⇒ Object
- #find_order(token:) ⇒ Object
- #find_payment(order_id:) ⇒ Object
-
#initialize(options) ⇒ Gateway
constructor
A new instance of Gateway.
- #purchase(amount, payment_source, gateway_options) ⇒ Object
- #retrieve_configuration ⇒ Object
- #void(response_code, gateway_options) ⇒ Object
Constructor Details
#initialize(options) ⇒ Gateway
Returns a new instance of Gateway.
9 10 11 12 13 14 15 16 |
# File 'app/models/solidus_afterpay/gateway.rb', line 9 def initialize() ::Afterpay.configure do |config| config.merchant_id = [:merchant_id] config.secret_key = [:secret_key] config.server = 'https://global-api-sandbox.afterpay.com/' if [:test_mode] config.user_agent = SolidusAfterpay::UserAgentGenerator.new(merchant_id: [:merchant_id]).generate end end |
Instance Method Details
#authorize(amount, payment_source, gateway_options) ⇒ Object
18 19 20 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 |
# File 'app/models/solidus_afterpay/gateway.rb', line 18 def (amount, payment_source, ) result = {} unless payment_source.payment_method.auto_capture response = ::Afterpay::API::Payment::Auth.call( payment: ::Afterpay::Components::Payment.new( token: payment_source.token, amount: ::Afterpay::Components::Money.new( amount: Money.from_cents(amount).amount.to_s, currency: [:currency] ) ) ) result = response.body end ActiveMerchant::Billing::Response.new(true, 'Transaction approved', result, authorization: result[:id]) rescue ::Afterpay::BaseError => e ::Afterpay::API::Payment::Reversal.call(token: payment_source.token) if e.is_a?(::Afterpay::RequestTimeoutError) = e. error_code = e.error_code if == 'Afterpay::PaymentRequiredError' = I18n.t('solidus_afterpay.payment_declined') error_code = 'payment_declined' end ActiveMerchant::Billing::Response.new(false, , {}, error_code: error_code) end |
#capture(amount, response_code, gateway_options) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/solidus_afterpay/gateway.rb', line 47 def capture(amount, response_code, ) payment_method = [:originator].payment_method response = if payment_method.auto_capture immediate_capture(amount, response_code, ) else deferred_capture(amount, response_code, ) end result = response.body if result.status != 'APPROVED' raise ::Afterpay::BaseError.new('payment_declined'), I18n.t('solidus_afterpay.payment_declined') end ActiveMerchant::Billing::Response.new(true, 'Transaction captured', result, authorization: result.id) rescue ::Afterpay::BaseError => e if e.is_a?(::Afterpay::RequestTimeoutError) payment_source = [:originator].payment_source ::Afterpay::API::Payment::Reversal.call(token: payment_source.token) end ActiveMerchant::Billing::Response.new(false, e., {}, error_code: e.error_code) end |
#create_checkout(order, gateway_options) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/models/solidus_afterpay/gateway.rb', line 121 def create_checkout(order, ) response = ::Afterpay::API::Order::Create.call( order: SolidusAfterpay::OrderComponentBuilder.new( order: order, mode: [:mode], redirect_confirm_url: [:redirect_confirm_url], redirect_cancel_url: [:redirect_cancel_url], popup_origin_url: [:popup_origin_url] ).call ) result = response.body ActiveMerchant::Billing::Response.new(true, 'Checkout created', result) rescue ::Afterpay::BaseError => e ActiveMerchant::Billing::Response.new(false, e., {}, error_code: e.error_code) end |
#credit(amount, response_code, gateway_options) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/models/solidus_afterpay/gateway.rb', line 78 def credit(amount, response_code, ) response = ::Afterpay::API::Payment::Refund.call( order_id: response_code, refund: ::Afterpay::Components::Refund.new( amount: ::Afterpay::Components::Money.new( amount: Money.from_cents(amount).amount.to_s, currency: [:originator].payment.currency ), merchant_reference: [:originator].payment.id ) ) result = response.body ActiveMerchant::Billing::Response.new(true, "Transaction Credited with #{amount}", result, authorization: result.refundId) rescue ::Afterpay::BaseError => e ActiveMerchant::Billing::Response.new(false, e., {}, error_code: e.error_code) end |
#find_order(token:) ⇒ Object
144 145 146 147 148 |
# File 'app/models/solidus_afterpay/gateway.rb', line 144 def find_order(token:) ::Afterpay::API::Order::Find.call(token: token).body rescue ::Afterpay::BaseError nil end |
#find_payment(order_id:) ⇒ Object
138 139 140 141 142 |
# File 'app/models/solidus_afterpay/gateway.rb', line 138 def find_payment(order_id:) ::Afterpay::API::Payment::Find.call(order_id: order_id).body rescue ::Afterpay::BaseError nil end |
#purchase(amount, payment_source, gateway_options) ⇒ Object
71 72 73 74 75 76 |
# File 'app/models/solidus_afterpay/gateway.rb', line 71 def purchase(amount, payment_source, ) result = (amount, payment_source, ) return result unless result.success? capture(amount, result., ) end |
#retrieve_configuration ⇒ Object
150 151 152 153 154 |
# File 'app/models/solidus_afterpay/gateway.rb', line 150 def retrieve_configuration ::Afterpay::API::Configuration::Retrieve.call.body rescue ::Afterpay::BaseError nil end |
#void(response_code, gateway_options) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'app/models/solidus_afterpay/gateway.rb', line 97 def void(response_code, ) payment_method = [:originator].payment_method if payment_method.auto_capture return ActiveMerchant::Billing::Response.new(false, "Transaction can't be voided", {}, error_code: 'void_not_allowed') end response = ::Afterpay::API::Payment::Void.call( order_id: response_code, payment: ::Afterpay::Components::Payment.new( amount: ::Afterpay::Components::Money.new( amount: [:originator].amount.to_s, currency: [:currency] ) ) ) result = response.body ActiveMerchant::Billing::Response.new(true, 'Transaction voided', result, authorization: result.id) rescue ::Afterpay::BaseError => e ActiveMerchant::Billing::Response.new(false, e., {}, error_code: e.error_code) end |