Class: PscbIntegration::Client

Inherits:
Object
  • Object
show all
Includes:
Fear::Either::Mixin
Defined in:
lib/pscb_integration/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(explicit_config = nil) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pscb_integration/client.rb', line 15

def initialize(explicit_config = nil)
  @config = explicit_config || PscbIntegration.config

  @client = Faraday.new(url: config.host) do |faraday|
    faraday.request  :json                    # form-encode POST params
    faraday.response :json

    if defined?(Rails)
      faraday.response :logger, Rails.logger, bodies: true
    end

    faraday.adapter Faraday.default_adapter  # make requests with Net::HTTP
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/pscb_integration/client.rb', line 13

def config
  @config
end

Instance Method Details

#build_payment_url(message) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pscb_integration/client.rb', line 30

def build_payment_url(message)
  json = message.to_json

  params = {
    marketPlace: config.market_place,
    message: Base64.urlsafe_encode64(json),
    signature: signature(json),
  }

  @client.build_url('pay', params).to_s
end

#decrypt(encrypted, demo: false) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/pscb_integration/client.rb', line 79

def decrypt(encrypted, demo: false)
  secret_key = demo ? config.demo_secret_key : config.secret_key

  decipher = OpenSSL::Cipher::AES.new(128, :ECB)
  decipher.decrypt
  decipher.key = Digest::MD5.digest(secret_key.to_s)

  plain = decipher.update(encrypted) + decipher.final
  plain.force_encoding('utf-8')
end

#encrypt(plain) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/pscb_integration/client.rb', line 90

def encrypt(plain)
  cipher = OpenSSL::Cipher::AES.new(128, :ECB)
  cipher.encrypt
  cipher.key = Digest::MD5.digest(config.secret_key.to_s)

  cipher.update(plain) + cipher.final
end

#pull_order_status(order_uid) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/pscb_integration/client.rb', line 56

def pull_order_status(order_uid)
  body = {
    orderId: order_uid,
    marketPlace: config.market_place,
  }.to_json

  handle_response(
    post('merchantApi/checkPayment', body)
  )
end

#recurring_payment(prev_order_uid:, new_order_uid:, token:, amount:) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pscb_integration/client.rb', line 42

def recurring_payment(prev_order_uid:, new_order_uid:, token:, amount:)
  body = {
    orderId: prev_order_uid,
    newOrderId: new_order_uid,
    marketPlace: config.market_place,
    token: token,
    amount: amount,
  }.to_json

  handle_response(
    post('merchantApi/payRecurrent', body)
  )
end

#refund_order(order_uid) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pscb_integration/client.rb', line 67

def refund_order(order_uid)
  body = {
    orderId: order_uid,
    marketPlace: config.market_place,
    partialRefund: false,
  }.to_json

  handle_response(
    post('merchantApi/refundPayment', body)
  )
end