payu-ruby 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +87 -0
- data/lib/payu/callback_result.rb +31 -0
- data/lib/payu/client.rb +77 -0
- data/lib/payu/configuration.rb +25 -0
- data/lib/payu/errors.rb +26 -0
- data/lib/payu/hash.rb +53 -0
- data/lib/payu/http.rb +46 -0
- data/lib/payu/payment_form.rb +12 -0
- data/lib/payu/refund.rb +38 -0
- data/lib/payu/verification_result.rb +29 -0
- data/lib/payu/version.rb +3 -0
- data/lib/payu.rb +50 -0
- metadata +83 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 7174c74c8e761c3551f5cc8630ac654b924487f3b864946cc2ba6eb0e217d348
|
|
4
|
+
data.tar.gz: 6e03c092a17af45084ba92dc68c8704df3754560906615a7704c87406cb64cbb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 6d8c2bce20f3b640a9f5d37920eb99f6a0d805e75d5cd9b260d3fd01ebe6e26a833a86ffb650f87cd5206f598728764e621b6326b4ea77cd6767c36baa21e46c
|
|
7
|
+
data.tar.gz: 58e87cb538219d113cc1e44c32156f89245d8bacb603c6a04fa6cb4539c0d7ba8efb9a7c86898fdafb187889b34667ceab3f79a2a852bcf3b066d44155aef92f
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vishwajeetsingh Desurkar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# payu-ruby
|
|
2
|
+
|
|
3
|
+
Ruby client for the PayU India payment gateway — hosted-checkout form
|
|
4
|
+
building, callback/reverse-hash verification, server-to-server
|
|
5
|
+
`verify_payment` reconciliation, and refunds. Framework-agnostic; no Rails
|
|
6
|
+
dependency.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem "payu-ruby"
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
client = Payu::Client.new(
|
|
18
|
+
key: "your_merchant_key",
|
|
19
|
+
salt: "your_merchant_salt",
|
|
20
|
+
test_mode: true # use test.payu.in
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# 1. Build the hosted-checkout redirect form (no network call)
|
|
24
|
+
form = client.build_payment(
|
|
25
|
+
txnid: "ORDER-123",
|
|
26
|
+
amount: 499.00,
|
|
27
|
+
productinfo: "Tickets for RubyConf",
|
|
28
|
+
firstname: "Jane",
|
|
29
|
+
email: "jane@example.com",
|
|
30
|
+
phone: "9876543210",
|
|
31
|
+
surl: "https://app.example.com/payu/callback",
|
|
32
|
+
furl: "https://app.example.com/payu/callback",
|
|
33
|
+
notify_url: "https://app.example.com/webhooks/payu"
|
|
34
|
+
)
|
|
35
|
+
form.payment_url # => URL to POST form.fields to (form submit or WebView)
|
|
36
|
+
form.fields # => Hash of all form fields including :hash
|
|
37
|
+
|
|
38
|
+
# 2. Verify an inbound callback/redirect's hash.
|
|
39
|
+
# Never trust this alone for final state — see below.
|
|
40
|
+
callback = client.verify_callback(params)
|
|
41
|
+
callback.valid? # => true/false
|
|
42
|
+
|
|
43
|
+
# 3. Server-to-server reconciliation — the source of truth
|
|
44
|
+
result = client.verify_payment(txnid: "ORDER-123")
|
|
45
|
+
result.success? # / .pending? / .failed?
|
|
46
|
+
result.mihpayid
|
|
47
|
+
result.raw # full parsed JSON, for audit logging
|
|
48
|
+
|
|
49
|
+
# 4. Refunds
|
|
50
|
+
refund = Payu::Refund.new(key: "your_merchant_key", salt: "your_merchant_salt", test_mode: true)
|
|
51
|
+
refund.initiate(mihpayid: result.mihpayid, amount: 499.00)
|
|
52
|
+
refund.status(result.mihpayid)
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**Security note**: `verify_callback` never raises on a bad hash — it returns
|
|
56
|
+
`valid?: false` so you decide how to record tampering. The callback's
|
|
57
|
+
reported status is informational only; always follow up with
|
|
58
|
+
`verify_payment` before treating a transaction as successful.
|
|
59
|
+
|
|
60
|
+
For Rails, expose `GET /checkout/:id/pay` that renders an auto-submit form
|
|
61
|
+
page. For mobile, return `{ payment_url:, params: }` as JSON and have the
|
|
62
|
+
app open `payment_url` in a WebView, POSTing the params.
|
|
63
|
+
|
|
64
|
+
## Error handling
|
|
65
|
+
|
|
66
|
+
All errors inherit from `Payu::PayuError`.
|
|
67
|
+
|
|
68
|
+
- `Payu::ConfigurationError` — missing `key`/`salt`.
|
|
69
|
+
- `Payu::ValidationError` — missing/blank required fields passed to
|
|
70
|
+
`build_payment`.
|
|
71
|
+
- `Payu::ApiError` — base class for network/API failures; carries `#status`
|
|
72
|
+
and `#body` when available.
|
|
73
|
+
- `Payu::NetworkError` — no HTTP response was received (timeout,
|
|
74
|
+
connection failure).
|
|
75
|
+
- `Payu::ResponseError` — a response was received, but it was a non-200
|
|
76
|
+
status or an unparseable body.
|
|
77
|
+
|
|
78
|
+
## Development
|
|
79
|
+
|
|
80
|
+
```
|
|
81
|
+
bundle install
|
|
82
|
+
bundle exec rspec
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## License
|
|
86
|
+
|
|
87
|
+
MIT
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module Payu
|
|
2
|
+
# Return value of Client#verify_callback. Never raised on a bad hash —
|
|
3
|
+
# #valid? reports the outcome so the host decides how to record tampering.
|
|
4
|
+
#
|
|
5
|
+
# This is informational only: PayU's reported status here must not be
|
|
6
|
+
# trusted for final state. Always follow up with Client#verify_payment.
|
|
7
|
+
class CallbackResult
|
|
8
|
+
attr_reader :params, :valid
|
|
9
|
+
|
|
10
|
+
def initialize(params:, valid:)
|
|
11
|
+
@params = params
|
|
12
|
+
@valid = valid
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def valid? = @valid
|
|
16
|
+
|
|
17
|
+
def txnid = @params[:txnid]
|
|
18
|
+
def status = @params[:status]
|
|
19
|
+
def email = @params[:email]
|
|
20
|
+
def firstname = @params[:firstname]
|
|
21
|
+
def productinfo = @params[:productinfo]
|
|
22
|
+
def amount = @params[:amount]
|
|
23
|
+
def mihpayid = @params[:mihpayid]
|
|
24
|
+
|
|
25
|
+
def udf1 = @params[:udf1]
|
|
26
|
+
def udf2 = @params[:udf2]
|
|
27
|
+
def udf3 = @params[:udf3]
|
|
28
|
+
def udf4 = @params[:udf4]
|
|
29
|
+
def udf5 = @params[:udf5]
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/payu/client.rb
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
module Payu
|
|
2
|
+
# Stateless/reentrant facade over PayU's hosted-checkout + verify_payment
|
|
3
|
+
# API. Construct once with credentials and reuse across requests.
|
|
4
|
+
class Client
|
|
5
|
+
def initialize(key:, salt:, test_mode: false)
|
|
6
|
+
@config = Configuration.new
|
|
7
|
+
@config.key = key
|
|
8
|
+
@config.salt = salt
|
|
9
|
+
@config.test_mode = test_mode
|
|
10
|
+
@config.validate!
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Builds the signed hosted-checkout form. No network call is made —
|
|
14
|
+
# only #verify_payment hits the network.
|
|
15
|
+
def build_payment(txnid:, amount:, productinfo:, firstname:, email:, phone:,
|
|
16
|
+
surl:, furl:, notify_url: nil,
|
|
17
|
+
udf1: "", udf2: "", udf3: "", udf4: "", udf5: "")
|
|
18
|
+
validate_build_payment!(txnid: txnid, amount: amount, email: email)
|
|
19
|
+
|
|
20
|
+
amt = format("%.2f", amount)
|
|
21
|
+
|
|
22
|
+
hash = Payu::Hash.request(
|
|
23
|
+
key: @config.key, txnid: txnid, amount: amt,
|
|
24
|
+
productinfo: productinfo, firstname: firstname, email: email,
|
|
25
|
+
udf1: udf1, udf2: udf2, udf3: udf3, udf4: udf4, udf5: udf5,
|
|
26
|
+
salt: @config.salt
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
fields = {
|
|
30
|
+
key: @config.key,
|
|
31
|
+
txnid: txnid,
|
|
32
|
+
amount: amt,
|
|
33
|
+
productinfo: productinfo,
|
|
34
|
+
firstname: firstname,
|
|
35
|
+
email: email,
|
|
36
|
+
phone: phone.to_s,
|
|
37
|
+
surl: surl,
|
|
38
|
+
furl: furl,
|
|
39
|
+
udf1: udf1.to_s,
|
|
40
|
+
udf2: udf2.to_s,
|
|
41
|
+
udf3: udf3.to_s,
|
|
42
|
+
udf4: udf4.to_s,
|
|
43
|
+
udf5: udf5.to_s,
|
|
44
|
+
hash: hash
|
|
45
|
+
}
|
|
46
|
+
fields[:notify_url] = notify_url if notify_url && !notify_url.empty?
|
|
47
|
+
|
|
48
|
+
PaymentForm.new(payment_url: @config.payment_url, fields: fields)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Verifies an inbound callback/redirect's hash. Never raises on a bad
|
|
52
|
+
# hash — the host decides how to record tampering via #valid?.
|
|
53
|
+
#
|
|
54
|
+
# PayU's reported status here is informational only — never trust it
|
|
55
|
+
# for final state; always follow up with #verify_payment.
|
|
56
|
+
def verify_callback(params)
|
|
57
|
+
valid = Payu::Hash.valid_response?(params: params, salt: @config.salt)
|
|
58
|
+
CallbackResult.new(params: params, valid: valid)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Server-to-server reconciliation — the source of truth for transaction
|
|
62
|
+
# state.
|
|
63
|
+
def verify_payment(txnid:)
|
|
64
|
+
hash = Payu::Hash.api(key: @config.key, command: "verify_payment", var1: txnid, salt: @config.salt)
|
|
65
|
+
raw = Payu::Http.post_form(@config.api_url, command: "verify_payment", var1: txnid, hash: hash, key: @config.key)
|
|
66
|
+
VerificationResult.new(txnid: txnid, raw: raw)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
def validate_build_payment!(txnid:, amount:, email:)
|
|
72
|
+
raise Payu::ValidationError, "txnid is required" if txnid.to_s.strip.empty?
|
|
73
|
+
raise Payu::ValidationError, "amount is required" if amount.nil?
|
|
74
|
+
raise Payu::ValidationError, "email is required" if email.to_s.strip.empty?
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
module Payu
|
|
2
|
+
class Configuration
|
|
3
|
+
attr_accessor :key, :salt, :test_mode
|
|
4
|
+
|
|
5
|
+
# Production endpoints (docs.payu.in, confirmed May 2026)
|
|
6
|
+
PAYMENT_URL = "https://secure.payu.in/_payment"
|
|
7
|
+
API_URL = "https://secure.payu.in/merchant/postservice?form=2"
|
|
8
|
+
|
|
9
|
+
# Sandbox endpoints
|
|
10
|
+
TEST_PAYMENT_URL = "https://test.payu.in/_payment"
|
|
11
|
+
TEST_API_URL = "https://test.payu.in/merchant/postservice?form=2"
|
|
12
|
+
|
|
13
|
+
def initialize
|
|
14
|
+
@test_mode = false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def payment_url = test_mode ? TEST_PAYMENT_URL : PAYMENT_URL
|
|
18
|
+
def api_url = test_mode ? TEST_API_URL : API_URL
|
|
19
|
+
|
|
20
|
+
def validate!
|
|
21
|
+
raise ConfigurationError, "Payu key is required" if key.nil? || key.empty?
|
|
22
|
+
raise ConfigurationError, "Payu salt is required" if salt.nil? || salt.empty?
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/payu/errors.rb
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Payu
|
|
2
|
+
class PayuError < StandardError; end
|
|
3
|
+
class ConfigurationError < PayuError; end
|
|
4
|
+
class HashMismatchError < PayuError; end
|
|
5
|
+
class PaymentFailedError < PayuError; end
|
|
6
|
+
class ValidationError < PayuError; end
|
|
7
|
+
|
|
8
|
+
# Raised by Payu::Http. Carries the HTTP status and raw body when
|
|
9
|
+
# available so callers can distinguish failure modes instead of pattern
|
|
10
|
+
# matching on the message.
|
|
11
|
+
class ApiError < PayuError
|
|
12
|
+
attr_reader :status, :body
|
|
13
|
+
|
|
14
|
+
def initialize(msg = nil, status: nil, body: nil)
|
|
15
|
+
super(msg)
|
|
16
|
+
@status = status
|
|
17
|
+
@body = body
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# No HTTP response was received at all (timeout, connection refused, DNS, ...).
|
|
22
|
+
class NetworkError < ApiError; end
|
|
23
|
+
|
|
24
|
+
# A response was received, but it was a non-200 status or an unparseable body.
|
|
25
|
+
class ResponseError < ApiError; end
|
|
26
|
+
end
|
data/lib/payu/hash.rb
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require "digest"
|
|
2
|
+
|
|
3
|
+
module Payu
|
|
4
|
+
module Hash
|
|
5
|
+
# Request hash — confirmed formula from docs.payu.in (May 2026):
|
|
6
|
+
# sha512(key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5||||||SALT)
|
|
7
|
+
# 17 pipe-delimited values: 6 mandatory + 5 UDF + 5 empty reserved slots + salt
|
|
8
|
+
def self.request(key:, txnid:, amount:, productinfo:, firstname:, email:,
|
|
9
|
+
udf1: "", udf2: "", udf3: "", udf4: "", udf5: "", salt:)
|
|
10
|
+
parts = [
|
|
11
|
+
key, txnid, amount, productinfo, firstname, email,
|
|
12
|
+
udf1.to_s, udf2.to_s, udf3.to_s, udf4.to_s, udf5.to_s,
|
|
13
|
+
"", "", "", "", "", # reserved udf6-udf10 slots
|
|
14
|
+
salt
|
|
15
|
+
]
|
|
16
|
+
Digest::SHA512.hexdigest(parts.join("|"))
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Response hash — field order is REVERSED vs request (confirmed from docs.payu.in):
|
|
20
|
+
# sha512(SALT|status||||||udf5|udf4|udf3|udf2|udf1|email|firstname|productinfo|amount|txnid|key)
|
|
21
|
+
#
|
|
22
|
+
# Variant: when PayU adds additional_charges (platform/convenience fee), it is prepended:
|
|
23
|
+
# sha512(additional_charges|SALT|status|...|key)
|
|
24
|
+
def self.response(params:, salt:)
|
|
25
|
+
p = params
|
|
26
|
+
core = [
|
|
27
|
+
salt,
|
|
28
|
+
p[:status].to_s,
|
|
29
|
+
"", "", "", "", "", # reserved slots (reversed)
|
|
30
|
+
p[:udf5].to_s, p[:udf4].to_s, p[:udf3].to_s, p[:udf2].to_s, p[:udf1].to_s,
|
|
31
|
+
p[:email].to_s, p[:firstname].to_s, p[:productinfo].to_s,
|
|
32
|
+
p[:amount].to_s, p[:txnid].to_s, p[:key].to_s
|
|
33
|
+
]
|
|
34
|
+
additional = p[:additional_charges].to_s
|
|
35
|
+
parts = additional.empty? ? core : [additional] + core
|
|
36
|
+
Digest::SHA512.hexdigest(parts.join("|"))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Constant-time comparison to prevent timing attacks
|
|
40
|
+
def self.valid_response?(params:, salt:)
|
|
41
|
+
expected = response(params: params, salt: salt)
|
|
42
|
+
received = params[:hash].to_s
|
|
43
|
+
return false if expected.bytesize != received.bytesize
|
|
44
|
+
|
|
45
|
+
expected.bytes.zip(received.bytes).reduce(0) { |acc, (a, b)| acc | (a ^ b) }.zero?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Hash for server-to-server API commands: sha512(key|command|var1|salt)
|
|
49
|
+
def self.api(key:, command:, var1:, salt:)
|
|
50
|
+
Digest::SHA512.hexdigest("#{key}|#{command}|#{var1}|#{salt}")
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
data/lib/payu/http.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "json"
|
|
3
|
+
require "uri"
|
|
4
|
+
|
|
5
|
+
module Payu
|
|
6
|
+
# Thin internal POST wrapper shared by Client#verify_payment and Refund.
|
|
7
|
+
# Not part of the public API.
|
|
8
|
+
class Http
|
|
9
|
+
USER_AGENT = "payu-ruby/#{Payu::VERSION}; Ruby/#{RUBY_VERSION}".freeze
|
|
10
|
+
|
|
11
|
+
def self.post_form(url, params)
|
|
12
|
+
uri = URI(url)
|
|
13
|
+
body = URI.encode_www_form(params)
|
|
14
|
+
|
|
15
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
16
|
+
http.use_ssl = uri.scheme == "https"
|
|
17
|
+
http.open_timeout = 10
|
|
18
|
+
http.read_timeout = 30
|
|
19
|
+
|
|
20
|
+
req = Net::HTTP::Post.new(uri)
|
|
21
|
+
req["Content-Type"] = "application/x-www-form-urlencoded"
|
|
22
|
+
req["User-Agent"] = USER_AGENT
|
|
23
|
+
req.body = body
|
|
24
|
+
|
|
25
|
+
resp = http.request(req)
|
|
26
|
+
|
|
27
|
+
unless resp.code.to_i == 200
|
|
28
|
+
raise Payu::ResponseError.new(
|
|
29
|
+
"PayU returned HTTP #{resp.code}", status: resp.code.to_i, body: resp.body
|
|
30
|
+
)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
begin
|
|
34
|
+
JSON.parse(resp.body)
|
|
35
|
+
rescue JSON::ParserError => e
|
|
36
|
+
raise Payu::ResponseError.new(
|
|
37
|
+
"PayU returned invalid JSON: #{e.message}", status: resp.code.to_i, body: resp.body
|
|
38
|
+
)
|
|
39
|
+
end
|
|
40
|
+
rescue Payu::ApiError
|
|
41
|
+
raise
|
|
42
|
+
rescue => e
|
|
43
|
+
raise Payu::NetworkError, "PayU API call failed: #{e.message}"
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module Payu
|
|
2
|
+
# Return value of Client#build_payment. No network call is made to
|
|
3
|
+
# produce this — it's just the signed field set and the URL to post it to.
|
|
4
|
+
class PaymentForm
|
|
5
|
+
attr_reader :payment_url, :fields
|
|
6
|
+
|
|
7
|
+
def initialize(payment_url:, fields:)
|
|
8
|
+
@payment_url = payment_url
|
|
9
|
+
@fields = fields
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
data/lib/payu/refund.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
module Payu
|
|
2
|
+
class Refund
|
|
3
|
+
def initialize(key:, salt:, test_mode: false)
|
|
4
|
+
@config = Configuration.new
|
|
5
|
+
@config.key = key
|
|
6
|
+
@config.salt = salt
|
|
7
|
+
@config.test_mode = test_mode
|
|
8
|
+
@config.validate!
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Initiate a full or partial refund.
|
|
12
|
+
# mihpayid: PayU's payment ID (returned in callback as :mihpayid)
|
|
13
|
+
# amount: amount to refund (can be partial)
|
|
14
|
+
def initiate(mihpayid:, amount:)
|
|
15
|
+
hash = Payu::Hash.api(
|
|
16
|
+
key: @config.key, command: "cancel_refund_transaction",
|
|
17
|
+
var1: mihpayid, salt: @config.salt
|
|
18
|
+
)
|
|
19
|
+
Payu::Http.post_form(
|
|
20
|
+
@config.api_url,
|
|
21
|
+
command: "cancel_refund_transaction", var1: mihpayid, var2: format("%.2f", amount),
|
|
22
|
+
hash: hash, key: @config.key
|
|
23
|
+
)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Check refund status by mihpayid.
|
|
27
|
+
def status(mihpayid)
|
|
28
|
+
hash = Payu::Hash.api(
|
|
29
|
+
key: @config.key, command: "get_refund_details",
|
|
30
|
+
var1: mihpayid, salt: @config.salt
|
|
31
|
+
)
|
|
32
|
+
Payu::Http.post_form(
|
|
33
|
+
@config.api_url,
|
|
34
|
+
command: "get_refund_details", var1: mihpayid, hash: hash, key: @config.key
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module Payu
|
|
2
|
+
# Return value of Client#verify_payment — the source of truth for
|
|
3
|
+
# transaction state (never trust the callback/redirect alone).
|
|
4
|
+
class VerificationResult
|
|
5
|
+
attr_reader :raw, :txnid
|
|
6
|
+
|
|
7
|
+
def initialize(txnid:, raw:)
|
|
8
|
+
@txnid = txnid
|
|
9
|
+
@raw = raw
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def success? = transaction_status == "success"
|
|
13
|
+
def pending? = transaction_status == "pending"
|
|
14
|
+
def failed? = !success? && !pending?
|
|
15
|
+
|
|
16
|
+
def mihpayid = transaction_details["mihpayid"]
|
|
17
|
+
def bank_ref_num = transaction_details["bank_ref_num"]
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def transaction_status
|
|
22
|
+
transaction_details["status"]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def transaction_details
|
|
26
|
+
@raw.dig("transaction_details", @txnid) || {}
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/payu/version.rb
ADDED
data/lib/payu.rb
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
require "payu/version"
|
|
2
|
+
require "payu/errors"
|
|
3
|
+
require "payu/http"
|
|
4
|
+
require "payu/configuration"
|
|
5
|
+
require "payu/hash"
|
|
6
|
+
require "payu/payment_form"
|
|
7
|
+
require "payu/callback_result"
|
|
8
|
+
require "payu/verification_result"
|
|
9
|
+
require "payu/client"
|
|
10
|
+
require "payu/refund"
|
|
11
|
+
|
|
12
|
+
# Payu — Ruby client for PayU India payment gateway.
|
|
13
|
+
#
|
|
14
|
+
# Usage:
|
|
15
|
+
# client = Payu::Client.new(
|
|
16
|
+
# key: "your_merchant_key",
|
|
17
|
+
# salt: "your_merchant_salt",
|
|
18
|
+
# test_mode: true # use test.payu.in
|
|
19
|
+
# )
|
|
20
|
+
#
|
|
21
|
+
# # 1. Build the hosted-checkout redirect form (no network call)
|
|
22
|
+
# form = client.build_payment(
|
|
23
|
+
# txnid: "ORDER-123",
|
|
24
|
+
# amount: 499.00,
|
|
25
|
+
# productinfo: "Tickets for RubyConf",
|
|
26
|
+
# firstname: "Jane",
|
|
27
|
+
# email: "jane@example.com",
|
|
28
|
+
# phone: "9876543210",
|
|
29
|
+
# surl: "https://app.example.com/payu/callback",
|
|
30
|
+
# furl: "https://app.example.com/payu/callback",
|
|
31
|
+
# notify_url: "https://app.example.com/webhooks/payu"
|
|
32
|
+
# )
|
|
33
|
+
# form.payment_url # => URL to POST form.fields to (form submit or WebView)
|
|
34
|
+
# form.fields # => Hash of all form fields including :hash
|
|
35
|
+
#
|
|
36
|
+
# # 2. Verify an inbound callback/redirect's hash (never trust it for final state)
|
|
37
|
+
# callback = client.verify_callback(params)
|
|
38
|
+
# callback.valid? # => true/false
|
|
39
|
+
#
|
|
40
|
+
# # 3. Server-to-server reconciliation — the source of truth
|
|
41
|
+
# result = client.verify_payment(txnid: "ORDER-123")
|
|
42
|
+
# result.success? / result.pending? / result.failed?
|
|
43
|
+
# result.mihpayid
|
|
44
|
+
# result.raw # full parsed JSON, for audit logging
|
|
45
|
+
#
|
|
46
|
+
# For Rails, expose GET /checkout/:id/pay that renders an auto-submit form page.
|
|
47
|
+
# For mobile, return { payment_url:, params: } as JSON; the app opens payment_url
|
|
48
|
+
# in a WebView posting the params.
|
|
49
|
+
module Payu
|
|
50
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: payu-ruby
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Vishwajeetsingh Desurkar
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: rspec
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '3.13'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '3.13'
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: webmock
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '3.23'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '3.23'
|
|
40
|
+
description: Hash generation, payment initiation params, server-to-server verification,
|
|
41
|
+
and refund support for PayU India. Framework-agnostic — works with Rails, Sinatra,
|
|
42
|
+
or plain Ruby.
|
|
43
|
+
email:
|
|
44
|
+
- vishwajeetsinghd@gmail.com
|
|
45
|
+
executables: []
|
|
46
|
+
extensions: []
|
|
47
|
+
extra_rdoc_files: []
|
|
48
|
+
files:
|
|
49
|
+
- LICENSE.txt
|
|
50
|
+
- README.md
|
|
51
|
+
- lib/payu.rb
|
|
52
|
+
- lib/payu/callback_result.rb
|
|
53
|
+
- lib/payu/client.rb
|
|
54
|
+
- lib/payu/configuration.rb
|
|
55
|
+
- lib/payu/errors.rb
|
|
56
|
+
- lib/payu/hash.rb
|
|
57
|
+
- lib/payu/http.rb
|
|
58
|
+
- lib/payu/payment_form.rb
|
|
59
|
+
- lib/payu/refund.rb
|
|
60
|
+
- lib/payu/verification_result.rb
|
|
61
|
+
- lib/payu/version.rb
|
|
62
|
+
homepage: https://github.com/Selectus2/payu-rb
|
|
63
|
+
licenses:
|
|
64
|
+
- MIT
|
|
65
|
+
metadata: {}
|
|
66
|
+
rdoc_options: []
|
|
67
|
+
require_paths:
|
|
68
|
+
- lib
|
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - ">="
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '3.1'
|
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
|
+
requirements:
|
|
76
|
+
- - ">="
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: '0'
|
|
79
|
+
requirements: []
|
|
80
|
+
rubygems_version: 4.0.3
|
|
81
|
+
specification_version: 4
|
|
82
|
+
summary: Ruby client for PayU India payment gateway
|
|
83
|
+
test_files: []
|