class Payoneer::Client

Attributes

configuration[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/payoneer/client.rb, line 5
def initialize(configuration)
  @configuration = configuration
end

Public Instance Methods

expanded_payout(payee_id:, client_reference_id:, amount:, currency:, description:, payout_date: Time.now, seller_id:, seller_name:, seller_url:, seller_type: 'ECOMMERCE', orders_type: 'url', path:, credentials:) click to toggle source

Includes additional items as needed to be Payoneer SAFE compliant

# File lib/payoneer/client.rb, line 43
def expanded_payout(payee_id:, client_reference_id:, amount:, currency:, description:, payout_date: Time.now, seller_id:, seller_name:, seller_url:, seller_type: 'ECOMMERCE', orders_type: 'url', path:, credentials:)
  params = {
    payee_id: payee_id,
    client_reference_id: client_reference_id,
    amount: amount,
    currency: currency,
    description: description,
    payout_date: payout_date.strftime('%Y-%m-%d'),
    orders_report: {
      merchant: {
        id: seller_id,
        store: {
          name: seller_name,
          url: seller_url,
          type: seller_type
        }
      },
      orders: {
        type: orders_type,
        path: path,
        credentials: credentials
      }
    }
  }

  begin
    response = RestClient::Request.execute(
      method: :post,
      url: "#{configuration.json_base_uri}/payouts",
      payload: params.to_json,
      headers: {
        content_type: 'application/json',
        accept: :json,
        Authorization: 'Basic ' + Base64.encode64("#{configuration.username}:#{configuration.api_password}").chomp
      },
      **configuration.http_client_options
    )

    hash = JSON.parse(response.body)
    hash['PaymentID'] = hash['payout_id'] # Keep consistent with the normal payout response body

    create_response(hash)
  rescue RestClient::Exception => e
    if e.http_body
      hash = JSON.parse(e.http_body)
      create_response(hash, e.http_code)
    end
  end
end
payee_details(payee_id) click to toggle source
# File lib/payoneer/client.rb, line 23
def payee_details(payee_id)
  post('GetPayeeDetails', p4: payee_id, p10: true) do |response|
    response['CompanyDetails'].present? ? response['Payee'].merge(response['CompanyDetails']) : response['Payee']
  end
end
payee_signup_url(payee_id, redirect_url: nil, redirect_time: nil) click to toggle source
# File lib/payoneer/client.rb, line 17
def payee_signup_url(payee_id, redirect_url: nil, redirect_time: nil)
  post('GetToken', p4: payee_id, p6: redirect_url, p8: redirect_time, p9: configuration.auto_approve_sandbox_accounts, p10: true) do |response|
    response['Token']
  end
end
payout(program_id:, payment_id:, payee_id:, amount:, description:, payment_date: Time.now, currency: 'USD') click to toggle source
# File lib/payoneer/client.rb, line 29
def payout(program_id:, payment_id:, payee_id:, amount:, description:, payment_date: Time.now, currency: 'USD')
  post(
    'PerformPayoutPayment',
    p4: program_id,
    p5: payment_id,
    p6: payee_id,
    p7: format('%.2f', amount),
    p8: description,
    p9: payment_date.strftime('%m/%d/%Y %H:%M:%S'),
    Currency: currency
  )
end
payout_details(payee_id:, payment_id:) click to toggle source
# File lib/payoneer/client.rb, line 93
def payout_details(payee_id:, payment_id:)
  post('GetPaymentStatus', p4: payee_id, p5: payment_id)
end
status() click to toggle source
# File lib/payoneer/client.rb, line 9
def status
  post('Echo')
end
version() click to toggle source
# File lib/payoneer/client.rb, line 13
def version
  post('GetVersion')
end

Private Instance Methods

create_response(hash, http_code = Response::OK_STATUS_CODE) { |hash| ... } click to toggle source
# File lib/payoneer/client.rb, line 125
def create_response(hash, http_code = Response::OK_STATUS_CODE)
  if hash.key?('Code')
    Response.new(hash['Code'], hash['Description'])
  else
    hash = block_given? ? yield(hash) : hash
    Response.new(http_code, hash)
  end
end
post(method_name, params = {}) { |hash| ... } click to toggle source
# File lib/payoneer/client.rb, line 99
def post(method_name, params = {})
  response = RestClient::Request.execute(
    method: :post,
    url: configuration.xml_base_uri,
    payload: {
      mname: method_name,
      p1: configuration.username,
      p2: configuration.api_password,
      p3: configuration.partner_id
    }.merge(params),
    **configuration.http_client_options
  )

  raise ResponseError.new(code: response.code, body: response.body) if response.code != 200

  # @TODO: Validate that the response is XML?
  hash = Hash.from_xml(response.body).values.first

  if hash.key?('Code')
    Response.new(hash['Code'], hash['Description'])
  else
    hash = block_given? ? yield(hash) : hash
    Response.new(Response::OK_STATUS_CODE, hash)
  end
end