class ScbEasyApi::Client

API Client of SCB easy Ruby

@client ||= ScbEasyApi::Client.new do |config|
   config.api_key = ENV["scb_api_key"]
   config.api_secret = ENV["scb_api_secret"]
   config.biller_id = ENV["scb_biller_id"]
   config.merchant_id = ENV["scb_merchant_id"]
   config.terminal_id = ENV["scb_terminal_id"]
   config.reference_prefix = ENV["scb_reference_prefix"]
   config.language = ENV["scb_language"] || 'EN'
   config.is_sandbox = ENV["scb_is_sandbox"] || true
end

Attributes

api_key[RW]

@return [String]

api_secret[RW]

@return [String]

biller_id[RW]

@return [String]

is_sandbox[RW]

@return [String]

language[RW]

@return [String]

merchant_id[RW]

@return [String]

reference_prefix[RW]

@return [String]

terminal_id[RW]

@return [String]

Public Class Methods

new(options = {}) { |self| ... } click to toggle source

Initialize a new client.

@param options [Hash] @return [ScbEasyApi::Client]

# File lib/scb_easy_api/client.rb, line 34
def initialize(options = {})
  options.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
  yield(self) if block_given?
end

Public Instance Methods

cancel_payment_alipay(company_id, out_trade_no) click to toggle source
# File lib/scb_easy_api/client.rb, line 108
def cancel_payment_alipay(company_id, out_trade_no)
  payload = payload_cancel_ewallets(true, company_id, out_trade_no)
  rest_client_api("#{endpoint}#{$PAYMENT_EWALLETS_CANCEL_PATH​}", "post", headers, payload)
end
cancel_payment_we_chat_pay(company_id, out_trade_no) click to toggle source
# File lib/scb_easy_api/client.rb, line 113
def cancel_payment_we_chat_pay(company_id, out_trade_no)
  payload = payload_cancel_ewallets(false, company_id, out_trade_no)
  rest_client_api("#{endpoint}#{$PAYMENT_EWALLETS_CANCEL_PATH​}", "post", headers, payload)
end
create_paymemnt(amount) click to toggle source
# File lib/scb_easy_api/client.rb, line 84
def create_paymemnt(amount)
  rest_client_api("#{endpoint}#{$DEEPLINK_TRANSATIONS_PATH}", "post", headers, payload_transactions(amount))
end
create_qrcode_alipay(company_id, amount) click to toggle source
# File lib/scb_easy_api/client.rb, line 92
def create_qrcode_alipay(company_id, amount)
  payload = payload_qrcode_ewallets(true, company_id, amount)
  resp = rest_client_api("#{endpoint}#{$PAYMENT_EWALLETS_CREATE_PATH​}", "post", headers, payload)
  resp['outTradeNo'] = payload[:outTradeNo]

  resp
end
create_qrcode_paymemnt(amount) click to toggle source
# File lib/scb_easy_api/client.rb, line 88
def create_qrcode_paymemnt(amount)
  rest_client_api("#{endpoint}#{$QRCODE_PAYMENT_PATH}", "post", headers, payload_qrcode_payment(amount))
end
create_qrcode_we_chat_pay(company_id, amount) click to toggle source
# File lib/scb_easy_api/client.rb, line 100
def create_qrcode_we_chat_pay(company_id, amount)
  payload = payload_qrcode_ewallets(false, company_id, amount)
  resp = rest_client_api("#{endpoint}#{$PAYMENT_EWALLETS_CREATE_PATH​}", "post", headers, payload)
  resp['outTradeNo'] = payload[:outTradeNo]

  resp
end
endpoint() click to toggle source
# File lib/scb_easy_api/client.rb, line 41
def endpoint
  if is_sandbox.nil? || is_sandbox == true
    @endpoint = "https://api-sandbox.partners.scb/partners/sandbox/"
  else
    @endpoint = "https://api.partners.scb/partners/"
  end
end
headers() click to toggle source
# File lib/scb_easy_api/client.rb, line 60
def headers
  api_key_required

  {
    Authorization: "Bearer #{oauth_token['data']['accessToken']}",
    content_type: 'application/json', 
    resourceOwnerId: api_key,
    requestUId: SecureRandom.uuid,
    channel: 'scbeasy',
    accept_language: language || 'EN'
  }
end
oauth_token() click to toggle source
# File lib/scb_easy_api/client.rb, line 73
def oauth_token
  api_key_required

  rest_client_api("#{endpoint}#{$OAUTH_PATH}", "post", {
    content_type: 'application/json', 
    resourceOwnerId: api_key,
    requestUId: SecureRandom.uuid,
    accept_language: language || 'EN'
  }, payload_token)
end
pay_with_alipay(company_id, amount, buyer_identity_code) click to toggle source
# File lib/scb_easy_api/client.rb, line 118
def pay_with_alipay(company_id, amount, buyer_identity_code)
  payload = payload_ewallets_pay(true, company_id, amount, buyer_identity_code)
  resp = rest_client_api("#{endpoint}#{$PAYMENT_EWALLETS_CREATE_PATH​}", "post", headers, payload)
  resp['outTradeNo'] = payload[:outTradeNo]

  resp
end
pay_with_we_chat_pay(company_id, amount, buyer_identity_code) click to toggle source
# File lib/scb_easy_api/client.rb, line 126
def pay_with_we_chat_pay(company_id, amount, buyer_identity_code)
  payload = payload_ewallets_pay(false, company_id, amount, buyer_identity_code)
  resp = rest_client_api("#{endpoint}#{$PAYMENT_EWALLETS_CREATE_PATH​}", "post", headers, payload)
  resp['outTradeNo'] = payload[:outTradeNo]

  resp
end
payload_cancel_ewallets(is_alipay, company_id, out_trade_no) click to toggle source
# File lib/scb_easy_api/client.rb, line 225
def payload_cancel_ewallets(is_alipay, company_id, out_trade_no)
  terminal_id_required
  reference_prefix_required

  {
    tranType: is_alipay ? 'A' : 'W',
    companyId: company_id,
    terminalId: terminal_id,
    outTradeNo: out_trade_no
  }
end
payload_ewallets_pay(is_alipay, company_id, amount, buyer_identity_code) click to toggle source
# File lib/scb_easy_api/client.rb, line 237
def payload_ewallets_pay(is_alipay, company_id, amount, buyer_identity_code)
  terminal_id_required
  reference_prefix_required

  {
    tranType: is_alipay ? 'A' : 'W',
    companyId: company_id,
    terminalId: terminal_id,
    outTradeNo: "SCB#{rand(1000000..999999999)}",
    totalFee: amount,
    buyerIdentityCode: buyer_identity_code
  }
end
payload_qrcode_ewallets(is_alipay, company_id, amount) click to toggle source
# File lib/scb_easy_api/client.rb, line 212
def payload_qrcode_ewallets(is_alipay, company_id, amount)
  terminal_id_required
  reference_prefix_required

  {
    tranType: is_alipay ? 'A' : 'W',
    companyId: company_id,
    terminalId: terminal_id,
    outTradeNo: "SCB#{rand(1000000..999999999)}",
    totalFee: amount
  }
end
payload_qrcode_payment(amount) click to toggle source
# File lib/scb_easy_api/client.rb, line 191
def payload_qrcode_payment(amount)
  biller_id_required
  merchant_id_required
  terminal_id_required
  reference_prefix_required

  {
    qrType: "PPCS",
    ppType: "BILLERID",
    ppId: biller_id,
    amount: amount,
      ref1: "#{reference_prefix}#{rand(1000000..999999999)}",
      ref2: "#{reference_prefix}#{rand(1000000..999999999)}",
      ref3: "SCB",
    merchantId: merchant_id,
    terminalId: terminal_id,
    invoice: "INVOICE",
    csExtExpiryTime: "60"
  }
end
payload_token(authorization_code = nil) click to toggle source
# File lib/scb_easy_api/client.rb, line 134
def payload_token(authorization_code = nil)
  api_key_required
  api_secret_required

  {
    applicationKey: api_key,
    applicationSecret: api_secret,
    authCode: authorization_code
  }
end
payload_transactions(amount) click to toggle source
# File lib/scb_easy_api/client.rb, line 145
def payload_transactions(amount)
  biller_id_required
  merchant_id_required
  terminal_id_required
  reference_prefix_required

  {
    transactionType: "PURCHASE",
    transactionSubType: ["BP", "CCFA"],
    sessionValidityPeriod: 60,
    sesisionValidUntil: "",
    billPayment: {
      paymentAmount: amount,
      accountTo: biller_id,
      ref1: "#{reference_prefix}#{rand(1000000..999999999)}",
      ref2: "#{reference_prefix}#{rand(1000000..999999999)}",
      ref3: reference_prefix
    },
    creditCardFullAmount: {
      merchantId: merchant_id,
      terminalId: terminal_id,
      orderReference: reference_prefix,
      paymentAmount: amount
    },
    merchantMetaData: {
      callbackUrl: "",
      extraData: {},
      paymentInfo: [
        {
          type: "TEXT_WITH_IMAGE",
          title: "",
          header: "",
          description: "",
          imageUrl: ""
        },
        {
          type: "TEXT",
          title: "",
          header: "",
          description: ""
        }
      ]
    }
  }
end
rest_client_api(url, method, headers, payload) click to toggle source
# File lib/scb_easy_api/client.rb, line 49
def rest_client_api(url, method, headers, payload)
  response = RestClient::Request.new({
    url: url,
    method: method.to_sym,
    headers: headers,
    payload: payload.to_json
  }).execute do |response, request, result|
    return JSON.parse(response.to_str)
  end
end

Private Instance Methods

api_key_required() click to toggle source
# File lib/scb_easy_api/client.rb, line 253
def api_key_required
  raise ArgumentError, '`api_key` is not configured' unless api_key
end
api_secret_required() click to toggle source
# File lib/scb_easy_api/client.rb, line 257
def api_secret_required
  raise ArgumentError, '`api_secret` is not configured' unless api_secret
end
biller_id_required() click to toggle source
# File lib/scb_easy_api/client.rb, line 261
def biller_id_required
  raise ArgumentError, '`biller_id` is not configured' unless biller_id
end
merchant_id_required() click to toggle source
# File lib/scb_easy_api/client.rb, line 265
def merchant_id_required
  raise ArgumentError, '`merchant_id` is not configured' unless merchant_id
end
reference_prefix_required() click to toggle source
# File lib/scb_easy_api/client.rb, line 273
def reference_prefix_required
  raise ArgumentError, '`reference_prefix` is not configured' unless reference_prefix
end
terminal_id_required() click to toggle source
# File lib/scb_easy_api/client.rb, line 269
def terminal_id_required
  raise ArgumentError, '`terminal_id` is not configured' unless terminal_id
end