Class: SolidusPaypalCommercePlatform::Client
- Inherits:
-
Object
- Object
- SolidusPaypalCommercePlatform::Client
- Defined in:
- lib/solidus_paypal_commerce_platform/client.rb
Defined Under Namespace
Classes: Response
Constant Summary collapse
- SUCCESS_STATUS_CODES =
[201, 204].freeze
- PARTNER_ATTRIBUTION_INJECTOR =
->(request) { request.headers["PayPal-Partner-Attribution-Id"] = SolidusPaypalCommercePlatform.config.partner_code }.freeze
Instance Attribute Summary collapse
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
Class Method Summary collapse
Instance Method Summary collapse
- #execute(request) ⇒ Object
- #execute_with_response(request, success_message: nil, failure_message: nil) ⇒ Object
- #i18n_scope_for(request) ⇒ Object
-
#initialize(client_id:, client_secret: "", test_mode: nil) ⇒ Client
constructor
A new instance of Client.
- #wrap_response(response, success_message: nil, failure_message: nil) ⇒ Object
Constructor Details
#initialize(client_id:, client_secret: "", test_mode: nil) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 |
# File 'lib/solidus_paypal_commerce_platform/client.rb', line 20 def initialize(client_id:, client_secret: "", test_mode: nil) test_mode = SolidusPaypalCommercePlatform.config.env.sandbox? if test_mode.nil? env_class = test_mode ? PayPal::SandboxEnvironment : PayPal::LiveEnvironment @environment = env_class.new(client_id, client_secret) @paypal_client = PayPal::PayPalHttpClient.new(@environment) @paypal_client.add_injector(&PARTNER_ATTRIBUTION_INJECTOR) end |
Instance Attribute Details
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
18 19 20 |
# File 'lib/solidus_paypal_commerce_platform/client.rb', line 18 def environment @environment end |
Class Method Details
.fetch_api_credentials(auth_code:, client_id:, nonce:) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/solidus_paypal_commerce_platform/client.rb', line 53 def self.fetch_api_credentials(auth_code:, client_id:, nonce:) client = new(client_id: client_id) access_token = client.execute(AccessTokenAuthorizationRequest.new( environment: client.environment, auth_code: auth_code, nonce: nonce, )).result.access_token client.execute(FetchMerchantCredentialsRequest.new( access_token: access_token, partner_merchant_id: SolidusPaypalCommercePlatform.config.partner_id, )).result end |
Instance Method Details
#execute(request) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/solidus_paypal_commerce_platform/client.rb', line 30 def execute(request) Rails.logger.info "[SolidusPaypalCommercePlatform::Client#execute] #{request.inspect}" @paypal_client.execute(request).tap do |response| Rails.logger.info "[SolidusPaypalCommercePlatform::Client#execute] #{response.inspect}" end rescue PayPalHttp::HttpError => e Rails.logger.error "[SolidusPaypalCommercePlatform::Client#execute] #{e.result.inspect}" Response.new(status_code: 422, error: e.result) end |
#execute_with_response(request, success_message: nil, failure_message: nil) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/solidus_paypal_commerce_platform/client.rb', line 40 def execute_with_response(request, success_message: nil, failure_message: nil) i18n_scope = i18n_scope_for(request) wrap_response( execute(request), success_message: || I18n.t("#{i18n_scope}.success", default: nil), failure_message: || I18n.t("#{i18n_scope}.failure", default: nil) ) end |
#i18n_scope_for(request) ⇒ Object
49 50 51 |
# File 'lib/solidus_paypal_commerce_platform/client.rb', line 49 def i18n_scope_for(request) "solidus_paypal_commerce_platform.responses.#{request.class.name.underscore}" end |
#wrap_response(response, success_message: nil, failure_message: nil) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/solidus_paypal_commerce_platform/client.rb', line 68 def wrap_response(response, success_message: nil, failure_message: nil) if SUCCESS_STATUS_CODES.include? response.status_code ||= "Success." ActiveMerchant::Billing::Response.new( true, , result: response.result, paypal_debug_id: response.headers["paypal-debug-id"] ) else ||= "A problem has occurred with this payment, please try again." ActiveMerchant::Billing::Response.new( false, ) end end |