Class: Payu::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/payu/client.rb

Overview

Stateless/reentrant facade over PayU's hosted-checkout + verify_payment API. Construct once with credentials and reuse across requests.

Instance Method Summary collapse

Constructor Details

#initialize(key:, salt:, test_mode: false) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
# File 'lib/payu/client.rb', line 5

def initialize(key:, salt:, test_mode: false)
  @config = Configuration.new
  @config.key = key
  @config.salt = salt
  @config.test_mode = test_mode
  @config.validate!
end

Instance Method Details

#build_payment(txnid:, amount:, productinfo:, firstname:, email:, phone:, surl:, furl:, notify_url: nil, udf1: "", udf2: "", udf3: "", udf4: "", udf5: "") ⇒ Object

Builds the signed hosted-checkout form. No network call is made — only #verify_payment hits the network.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/payu/client.rb', line 15

def build_payment(txnid:, amount:, productinfo:, firstname:, email:, phone:,
                  surl:, furl:, notify_url: nil,
                  udf1: "", udf2: "", udf3: "", udf4: "", udf5: "")
  validate_build_payment!(txnid: txnid, amount: amount, email: email)

  amt = format("%.2f", amount)

  hash = Payu::Hash.request(
    key: @config.key, txnid: txnid, amount: amt,
    productinfo: productinfo, firstname: firstname, email: email,
    udf1: udf1, udf2: udf2, udf3: udf3, udf4: udf4, udf5: udf5,
    salt: @config.salt
  )

  fields = {
    key:         @config.key,
    txnid:       txnid,
    amount:      amt,
    productinfo: productinfo,
    firstname:   firstname,
    email:       email,
    phone:       phone.to_s,
    surl:        surl,
    furl:        furl,
    udf1:        udf1.to_s,
    udf2:        udf2.to_s,
    udf3:        udf3.to_s,
    udf4:        udf4.to_s,
    udf5:        udf5.to_s,
    hash:        hash
  }
  fields[:notify_url] = notify_url if notify_url && !notify_url.empty?

  PaymentForm.new(payment_url: @config.payment_url, fields: fields)
end

#verify_callback(params) ⇒ Object

Verifies an inbound callback/redirect's hash. Never raises on a bad hash — the host decides how to record tampering via #valid?.

PayU's reported status here is informational only — never trust it for final state; always follow up with #verify_payment.



56
57
58
59
# File 'lib/payu/client.rb', line 56

def verify_callback(params)
  valid = Payu::Hash.valid_response?(params: params, salt: @config.salt)
  CallbackResult.new(params: params, valid: valid)
end

#verify_payment(txnid:) ⇒ Object

Server-to-server reconciliation — the source of truth for transaction state.



63
64
65
66
67
# File 'lib/payu/client.rb', line 63

def verify_payment(txnid:)
  hash = Payu::Hash.api(key: @config.key, command: "verify_payment", var1: txnid, salt: @config.salt)
  raw = Payu::Http.post_form(@config.api_url, command: "verify_payment", var1: txnid, hash: hash, key: @config.key)
  VerificationResult.new(txnid: txnid, raw: raw)
end