Class: PaypalAPI::Client

Inherits:
Object
  • Object
show all
Includes:
APIMethods, AccessTokenMethods, EnvironmentMethods, HTTPMethods
Defined in:
lib/paypal-api/client.rb,
lib/paypal-api/client/api_methods.rb,
lib/paypal-api/client/http_methods.rb,
lib/paypal-api/client/environment_methods.rb,
lib/paypal-api/client/access_token_methods.rb

Overview

PaypalAPI Client

Defined Under Namespace

Modules: APIMethods, AccessTokenMethods, EnvironmentMethods, HTTPMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTPMethods

#delete, #get, #patch, #post, #put

Methods included from EnvironmentMethods

#api_url, #live?, #sandbox?, #web_url

Methods included from APIMethods

#authentication, #authorized_payments, #captured_payments, #catalog_products, #disputes, #invoice_templates, #invoices, #orders, #orders_v1, #partner_referrals, #payment_experience_web_profiles, #payment_tokens, #payout_items, #payouts, #referenced_payout_items, #referenced_payouts, #refunds, #setup_tokens, #shipment_tracking, #subscription_plans, #subscriptions, #transaction_search, #user_info, #users, #webhook_events, #webhook_lookups, #webhooks

Methods included from AccessTokenMethods

#access_token, #refresh_access_token

Constructor Details

#initialize(client_id:, client_secret:, live: false, http_opts: nil, retries: nil, cache: nil) ⇒ Client

Initializes Client

Parameters:

  • client_id (String)

    PayPal client id

  • client_secret (String)

    PayPal client secret

  • live (Boolean) (defaults to: false)

    PayPal live/sandbox mode

  • http_opts (Hash) (defaults to: nil)

    Net::Http opts for all requests

  • retries (Hash) (defaults to: nil)

    Retries configuration



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/paypal-api/client.rb', line 37

def initialize(client_id:, client_secret:, live: false, http_opts: nil, retries: nil, cache: nil)
  @env = PaypalAPI::Environment.new(client_id: client_id, client_secret: client_secret, live: live)
  @config = PaypalAPI::Config.new(http_opts: http_opts, retries: retries, cache: cache)

  @callbacks = {
    before: [],
    after_success: [],
    after_fail: [],
    after_network_error: []
  }.freeze

  @access_token = nil
end

Instance Attribute Details

#callbacksHash (readonly)

Registered callbacks

Returns:

  • (Hash)

    Registered callbacks



24
25
26
# File 'lib/paypal-api/client.rb', line 24

def callbacks
  @callbacks
end

#configConfig (readonly)

Gem Configuration

Returns:

  • (Config)

    Gem Configuration



19
20
21
# File 'lib/paypal-api/client.rb', line 19

def config
  @config
end

#envObject (readonly)

Paypal environment



14
15
16
# File 'lib/paypal-api/client.rb', line 14

def env
  @env
end

Instance Method Details

#add_callback(callback_name, &block) ⇒ void

This method returns an undefined value.

Registers callback

Parameters:

  • callback_name (Symbol)

    Callback name. Allowed values: :before, :after_success, :after_fail, :after_network_error

  • block (Proc)

    Block that must be call For ‘:before` callback proc should accept 2 params -

    request [Request], context [Hash]
    

    For ‘:after_success` callback proc should accept 3 params -

    request [Request], context [Hash], response [Response]
    

    For ‘:after_fail` callback proc should accept 3 params -

    request [Request], context [Hash], error [StandardError]
    

    For ‘:after_network_error` callback proc should accept 3 params -

    request [Request], context [Hash], response [Response]
    


70
71
72
# File 'lib/paypal-api/client.rb', line 70

def add_callback(callback_name, &block)
  callbacks.fetch(callback_name) << block
end

#verify_webhook(webhook_id:, headers:, raw_body:) ⇒ Boolean

Verifies Webhook

It requires one-time request to download and cache certificate. If local verification returns false it tries to verify webhook online.

Examples:


class Webhooks::PaypalController < ApplicationController
  def create
    webhook_id = ENV['PAYPAL_WEBHOOK_ID'] # PayPal registered webhook ID for current URL
    headers = request.headers # must respond to #[] to get headers
    body = request.raw_post # must be a raw String body

    webhook_is_valid = PaypalAPI.verify_webhook(webhook_id: webhook_id, headers: headers, body: body)
    webhook_is_valid ? handle_webhook_event(body) : log_error(webhook_id, headers, body)

    head :no_content
  end
end

Parameters:

  • webhook_id (String)

    webhook_id provided by PayPal when webhook was registered

  • headers (Hash, #[])

    webhook request headers

  • raw_body (String)

    webhook request raw body string

Returns:

  • (Boolean)

    webhook event is valid



102
103
104
# File 'lib/paypal-api/client.rb', line 102

def verify_webhook(webhook_id:, headers:, raw_body:)
  WebhookVerifier.new(self).call(webhook_id: webhook_id, headers: headers, raw_body: raw_body)
end