payriff 0.1.2 → 0.3.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 +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +32 -42
- data/lib/payriff/client.rb +18 -8
- data/lib/payriff/services.rb +8 -31
- data/lib/payriff/transport.rb +3 -3
- data/lib/payriff/version.rb +1 -1
- data/lib/payriff.rb +0 -1
- metadata +4 -5
- data/lib/payriff/card.rb +0 -96
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: c68de2e053c3092eca6c39cdc9258d11bc3d3c743bcff2fe9c71d9be76b9894c
|
|
4
|
+
data.tar.gz: b501bddb457708d16bcb7e8c386d50f706206462c3e073d0f46945d657ac00fc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a4948b275dd610f30c9b9ae60ddc7e094a7cd26227411597d965c03d84426f6adf4abba5330876003c6b77485fb109d21ee34748406dc814c4a1def08029a9e4
|
|
7
|
+
data.tar.gz: ae5667709e81dd7b4de38040d14aac9be4a2aac28a6a1394b429090cf019be24645b8c510f07bd1adc845da099bfa8e98a84c0d2fe4c41ffbee606fb5e911147
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.3.0 (2026-12-04)
|
|
4
|
+
|
|
5
|
+
- Documented `client_secret` on the create-order response, set when the application has an active
|
|
6
|
+
publishable key. The value was already returned.
|
|
7
|
+
- The app key is now called the secret key: `Payriff::Client.new(secret_key: ...)`. `app_key:` still
|
|
8
|
+
works but prints a deprecation warning.
|
|
9
|
+
- Added the `:ksa` region for Saudi Arabia. `:saudi` still works but is deprecated.
|
|
10
|
+
|
|
11
|
+
## 0.2.0 (unreleased)
|
|
12
|
+
|
|
13
|
+
- Added: Saudi Arabia support via `Payriff::Client.new(..., region: :saudi)`. It uses
|
|
14
|
+
`https://api.gccpay.sa` and defaults the currency to `SAR`. Azerbaijan (`AZN`) stays the default.
|
|
15
|
+
- Removed: direct (host-to-host) card payments: `payments.direct_pay`, `Payriff::Card` and the
|
|
16
|
+
`card_encryption_key:` option. Use the hosted payment page (`orders.create`) instead.
|
|
17
|
+
- Removed `Payriff::Client::PRODUCTION_URL`; see `Payriff::Client::REGIONS`.
|
|
18
|
+
|
|
3
19
|
## 0.1.2 (2026-10-15)
|
|
4
20
|
|
|
5
21
|
- First RubyGems release: install with `gem "payriff"`.
|
data/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# Payriff Ruby SDK
|
|
2
2
|
|
|
3
|
-
Ruby client for the Payriff merchant API: orders,
|
|
3
|
+
Ruby client for the Payriff merchant API: orders, saved-card payments, saved cards,
|
|
4
4
|
transactions, payouts and invoices.
|
|
5
5
|
|
|
6
6
|
- Ruby 3.1 or newer
|
|
7
|
-
- No runtime dependencies (standard library `net/http
|
|
8
|
-
- Talks to `https://api.payriff.com`
|
|
7
|
+
- No runtime dependencies (standard library `net/http` and `json` only)
|
|
8
|
+
- Talks to `https://api.payriff.com` (Azerbaijan) or `https://api.gccpay.sa` (Saudi Arabia)
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
@@ -21,7 +21,7 @@ Then run `bundle install`, or install it directly with `gem install payriff`.
|
|
|
21
21
|
```ruby
|
|
22
22
|
require "payriff"
|
|
23
23
|
|
|
24
|
-
payriff = Payriff::Client.new(
|
|
24
|
+
payriff = Payriff::Client.new(secret_key: ENV.fetch("PAYRIFF_SECRET_KEY"))
|
|
25
25
|
|
|
26
26
|
order = payriff.orders.create(
|
|
27
27
|
amount: "10.00",
|
|
@@ -43,20 +43,37 @@ Create one `Payriff::Client` and reuse it.
|
|
|
43
43
|
|
|
44
44
|
| Argument | Required | Default | Notes |
|
|
45
45
|
|---|---|---|---|
|
|
46
|
-
| `
|
|
46
|
+
| `secret_key:` | yes | — | Secret key from the Payriff dashboard. |
|
|
47
47
|
| `merchant_id:` | for payouts and invoices | — | Merchant ID (e.g. `ES1000000`). |
|
|
48
48
|
| `timeout:` | no | `60` | Read/write timeout in seconds. Bank operations can take tens of seconds. |
|
|
49
49
|
| `open_timeout:` | no | `10` | Connection timeout in seconds. |
|
|
50
|
-
| `
|
|
50
|
+
| `region:` | no | `:azerbaijan` | API region: `:azerbaijan` or `:ksa`. Sets the base URL and the default currency. |
|
|
51
|
+
| `base_url:` | no | from `region:` | Override the API URL, e.g. for testing. The currency still comes from `region:`. |
|
|
51
52
|
|
|
52
|
-
Keep the
|
|
53
|
+
Keep the secret key in an environment variable or secret store. Never commit it.
|
|
54
|
+
Versions before 0.3.0 called it the app key; `app_key:` still works but prints a deprecation warning.
|
|
55
|
+
|
|
56
|
+
### Saudi Arabia (KSA)
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
payriff = Payriff::Client.new(secret_key: ENV.fetch("PAYRIFF_SECRET_KEY"), region: :ksa)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
| Region | API | Default currency |
|
|
63
|
+
|---|---|---|
|
|
64
|
+
| `:azerbaijan` | `https://api.payriff.com` | `AZN` |
|
|
65
|
+
| `:ksa` | `https://api.gccpay.sa` | `SAR` |
|
|
66
|
+
|
|
67
|
+
Use the secret key from the dashboard of the same region (`dashboard.gccpay.sa` for KSA).
|
|
68
|
+
`:saudi` still works but is deprecated; use `:ksa`.
|
|
69
|
+
A `currency:` passed on a request always wins over the region default.
|
|
53
70
|
|
|
54
71
|
## Orders
|
|
55
72
|
|
|
56
73
|
```ruby
|
|
57
74
|
created = payriff.orders.create(
|
|
58
75
|
amount: "25.00",
|
|
59
|
-
currency: "AZN", # default
|
|
76
|
+
currency: "AZN", # default: the region currency
|
|
60
77
|
operation: "PRE_AUTH", # default PURCHASE
|
|
61
78
|
language: "AZ",
|
|
62
79
|
description: "Booking #77",
|
|
@@ -74,43 +91,16 @@ payriff.orders.expire(created.order_id) # cancel an unpaid or
|
|
|
74
91
|
receipt_pdf = payriff.orders.download_receipt(created.order_id)
|
|
75
92
|
```
|
|
76
93
|
|
|
94
|
+
`created.client_secret` is set only when the application has an active publishable key; otherwise it
|
|
95
|
+
is `nil`. Pass it with the order ID to the Payriff web or mobile SDK, which takes the payment in the
|
|
96
|
+
customer's browser or app. Treat it as a secret: don't log it or put it in URLs.
|
|
97
|
+
|
|
77
98
|
`payment_status` is a string such as `"APPROVED"`, `"DECLINED"`, `"PREAUTH_APPROVED"` or `"REFUNDED"`.
|
|
78
99
|
Enum arguments also accept symbols (`language: :az`).
|
|
79
100
|
|
|
80
101
|
Amounts can be passed as numeric strings (`"10.50"`), integers, floats or `BigDecimal`s.
|
|
81
102
|
|
|
82
|
-
##
|
|
83
|
-
|
|
84
|
-
You collect the card details yourself, and the SDK encrypts them before sending
|
|
85
|
-
(AES-256-GCM + RSA-OAEP). Raw card data never leaves your server in clear text, but your
|
|
86
|
-
systems still handle card data, so PCI DSS requirements apply to you.
|
|
87
|
-
|
|
88
|
-
```ruby
|
|
89
|
-
result = payriff.payments.direct_pay(
|
|
90
|
-
amount: "1.00",
|
|
91
|
-
description: "Order #1002",
|
|
92
|
-
callback_url: "https://shop.example/payriff/callback",
|
|
93
|
-
request_rrn: "order-1002",
|
|
94
|
-
card: Payriff::Card.new(
|
|
95
|
-
pan: "4169 7413 3015 1979",
|
|
96
|
-
card_holder: "JOHN DOE",
|
|
97
|
-
expiry_month: "11",
|
|
98
|
-
expiry_year: "27",
|
|
99
|
-
cvv: "123"
|
|
100
|
-
)
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
if result.redirect
|
|
104
|
-
# 3-D Secure: send the customer's browser to result.redirect_url.
|
|
105
|
-
# The final status arrives via your callback or payriff.orders.get(result.order_id).
|
|
106
|
-
end
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
`Payriff::Card` strips spaces and dashes, pads the month (`1` → `01`), expands a 2-digit year
|
|
110
|
-
(`27` → `2027`) and rejects malformed values before any network call. It is frozen, prints and logs
|
|
111
|
-
with a masked number and no CVV, and cannot be serialized with `Marshal`.
|
|
112
|
-
|
|
113
|
-
### Charging a saved card
|
|
103
|
+
## Charging a saved card
|
|
114
104
|
|
|
115
105
|
```ruby
|
|
116
106
|
charge = payriff.payments.auto_pay(
|
|
@@ -159,7 +149,7 @@ page.content.each { |tx| puts "#{tx.order_id} #{tx.amount}" }
|
|
|
159
149
|
Payouts require `merchant_id` on the client.
|
|
160
150
|
|
|
161
151
|
```ruby
|
|
162
|
-
payriff = Payriff::Client.new(
|
|
152
|
+
payriff = Payriff::Client.new(secret_key: ENV.fetch("PAYRIFF_SECRET_KEY"), merchant_id: ENV.fetch("PAYRIFF_MERCHANT_ID"))
|
|
163
153
|
|
|
164
154
|
masked_name = payriff.payouts.check_cardholder("4169741330151979") # e.g. "J*** D**"
|
|
165
155
|
|
|
@@ -224,7 +214,7 @@ and `response_id` (quote it when contacting support).
|
|
|
224
214
|
|
|
225
215
|
| Exception | When |
|
|
226
216
|
|---|---|
|
|
227
|
-
| `Payriff::AuthenticationError` |
|
|
217
|
+
| `Payriff::AuthenticationError` | Secret key rejected (`14010`, `14013`, `14014`, `14015`) |
|
|
228
218
|
| `Payriff::ValidationError` | Invalid request (`15400` or HTTP 400) |
|
|
229
219
|
| `Payriff::RequestRejectedError` | Business refusal (`01000`), e.g. application under review |
|
|
230
220
|
| `Payriff::InsufficientBalanceError` | Not enough wallet balance for a payout (`01200`) |
|
data/lib/payriff/client.rb
CHANGED
|
@@ -2,23 +2,33 @@
|
|
|
2
2
|
|
|
3
3
|
module Payriff
|
|
4
4
|
class Client
|
|
5
|
-
|
|
5
|
+
REGIONS = {
|
|
6
|
+
azerbaijan: { base_url: "https://api.payriff.com", currency: "AZN" }.freeze,
|
|
7
|
+
ksa: { base_url: "https://api.gccpay.sa", currency: "SAR" }.freeze,
|
|
8
|
+
saudi: { base_url: "https://api.gccpay.sa", currency: "SAR" }.freeze
|
|
9
|
+
}.freeze
|
|
6
10
|
|
|
7
11
|
attr_reader :orders, :payments, :cards, :transactions, :payouts, :invoices
|
|
8
12
|
|
|
9
|
-
def initialize(
|
|
10
|
-
|
|
13
|
+
def initialize(secret_key: nil, merchant_id: nil, timeout: 60, open_timeout: 10, region: :azerbaijan, base_url: nil,
|
|
14
|
+
app_key: nil)
|
|
15
|
+
unless app_key.nil?
|
|
16
|
+
warn("Payriff: app_key is deprecated; use secret_key", uplevel: 1)
|
|
17
|
+
secret_key ||= app_key
|
|
18
|
+
end
|
|
19
|
+
raise ArgumentError, "secret_key is required" if secret_key.nil? || secret_key.to_s.strip.empty?
|
|
11
20
|
raise ArgumentError, "timeouts must be positive" unless timeout.to_f.positive? && open_timeout.to_f.positive?
|
|
12
21
|
|
|
13
|
-
|
|
22
|
+
config = REGIONS[region.to_s.to_sym] or raise ArgumentError, "unknown region: #{region}"
|
|
23
|
+
|
|
24
|
+
transport = Transport.new(base_url: base_url || config[:base_url], secret_key: secret_key, merchant_id: merchant_id,
|
|
14
25
|
timeout: timeout, open_timeout: open_timeout)
|
|
15
|
-
|
|
16
|
-
@
|
|
17
|
-
@payments = Services::Payments.new(transport, key)
|
|
26
|
+
@orders = Services::Orders.new(transport, config[:currency])
|
|
27
|
+
@payments = Services::Payments.new(transport, config[:currency])
|
|
18
28
|
@cards = Services::Cards.new(transport)
|
|
19
29
|
@transactions = Services::Transactions.new(transport)
|
|
20
30
|
@payouts = Services::Payouts.new(transport)
|
|
21
|
-
@invoices = Services::Invoices.new(transport)
|
|
31
|
+
@invoices = Services::Invoices.new(transport, config[:currency])
|
|
22
32
|
end
|
|
23
33
|
|
|
24
34
|
def inspect
|
data/lib/payriff/services.rb
CHANGED
|
@@ -5,8 +5,9 @@ module Payriff
|
|
|
5
5
|
class Base
|
|
6
6
|
include Util
|
|
7
7
|
|
|
8
|
-
def initialize(transport)
|
|
8
|
+
def initialize(transport, default_currency = nil)
|
|
9
9
|
@transport = transport
|
|
10
|
+
@default_currency = default_currency
|
|
10
11
|
end
|
|
11
12
|
|
|
12
13
|
def inspect
|
|
@@ -15,12 +16,12 @@ module Payriff
|
|
|
15
16
|
end
|
|
16
17
|
|
|
17
18
|
class Orders < Base
|
|
18
|
-
def create(amount:, currency:
|
|
19
|
+
def create(amount:, currency: nil, language: nil, operation: "PURCHASE", description: nil, callback_url: nil,
|
|
19
20
|
redirect_url: nil, card_save: nil, three_ds: nil, auto_payment_type: nil, installment: nil,
|
|
20
21
|
full_name: nil, phone_number: nil, metadata: nil, fields: nil, request_rrn: nil)
|
|
21
22
|
body = compact(
|
|
22
23
|
"amount" => to_amount(required(amount, "amount"), "amount"),
|
|
23
|
-
"currency" => stringify(currency),
|
|
24
|
+
"currency" => stringify(currency || @default_currency),
|
|
24
25
|
"language" => stringify(language),
|
|
25
26
|
"operation" => stringify(operation),
|
|
26
27
|
"description" => description,
|
|
@@ -76,37 +77,13 @@ module Payriff
|
|
|
76
77
|
end
|
|
77
78
|
|
|
78
79
|
class Payments < Base
|
|
79
|
-
def
|
|
80
|
-
super(transport)
|
|
81
|
-
@public_key = public_key
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def direct_pay(amount:, description:, card:, operation: "PURCHASE", currency: "AZN", callback_url: nil,
|
|
85
|
-
three_ds: nil, custom_fields: nil, card_save: false, request_rrn: nil)
|
|
86
|
-
body = compact(
|
|
87
|
-
"amount" => to_amount(required(amount, "amount"), "amount"),
|
|
88
|
-
"operation" => stringify(operation),
|
|
89
|
-
"currency" => stringify(currency),
|
|
90
|
-
"description" => required(description, "description"),
|
|
91
|
-
"callbackUrl" => callback_url,
|
|
92
|
-
"threeDS" => three_ds,
|
|
93
|
-
"customFields" => custom_fields
|
|
94
|
-
)
|
|
95
|
-
raise ArgumentError, "card must be a Payriff::Card" unless card.is_a?(Card)
|
|
96
|
-
|
|
97
|
-
encrypted = card.encrypted_payload(@public_key)
|
|
98
|
-
body["paymentData"] = { "paymentWay" => "DIRECT", "encryptedMessage" => encrypted[:message], "cardSave" => card_save ? true : false }
|
|
99
|
-
@transport.execute("POST", "/api/v3/directPay",
|
|
100
|
-
headers: { "X-REQUEST-RRN" => request_rrn, "x-secret-key" => encrypted[:secret_key] }, body: body)
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
def auto_pay(card_uuid:, amount:, description:, operation: "PURCHASE", currency: "AZN", callback_url: nil,
|
|
80
|
+
def auto_pay(card_uuid:, amount:, description:, operation: "PURCHASE", currency: nil, callback_url: nil,
|
|
104
81
|
three_ds: nil, one_click_payment: nil, request_rrn: nil)
|
|
105
82
|
body = compact(
|
|
106
83
|
"cardUuid" => required(card_uuid, "card_uuid"),
|
|
107
84
|
"amount" => to_amount(required(amount, "amount"), "amount"),
|
|
108
85
|
"operation" => stringify(operation),
|
|
109
|
-
"currency" => stringify(currency),
|
|
86
|
+
"currency" => stringify(currency || @default_currency),
|
|
110
87
|
"description" => required(description, "description"),
|
|
111
88
|
"callbackUrl" => callback_url,
|
|
112
89
|
"threeDS" => three_ds,
|
|
@@ -207,7 +184,7 @@ module Payriff
|
|
|
207
184
|
end
|
|
208
185
|
|
|
209
186
|
class Invoices < Base
|
|
210
|
-
def create(amount: nil, amount_dynamic: nil, currency:
|
|
187
|
+
def create(amount: nil, amount_dynamic: nil, currency: nil, language: nil, full_name: nil, email: nil,
|
|
211
188
|
phone_number: nil, description: nil, custom_message: nil, expire_date: nil, approve_url: nil,
|
|
212
189
|
cancel_url: nil, decline_url: nil, redirect_url: nil, installment_product_type: nil,
|
|
213
190
|
installment_period: nil, direct_pay: nil, send_sms: nil, send_whatsapp: nil, send_email: nil,
|
|
@@ -217,7 +194,7 @@ module Payriff
|
|
|
217
194
|
body = compact(
|
|
218
195
|
"amount" => to_amount(amount, "amount"),
|
|
219
196
|
"amountDynamic" => amount_dynamic,
|
|
220
|
-
"currencyType" => stringify(currency),
|
|
197
|
+
"currencyType" => stringify(currency || @default_currency),
|
|
221
198
|
"languageType" => stringify(language),
|
|
222
199
|
"fullName" => full_name,
|
|
223
200
|
"email" => email,
|
data/lib/payriff/transport.rb
CHANGED
|
@@ -11,9 +11,9 @@ module Payriff
|
|
|
11
11
|
IOError, SystemCallError, SocketError, Timeout::Error, OpenSSL::SSL::SSLError, Net::HTTPBadResponse, EOFError
|
|
12
12
|
].freeze
|
|
13
13
|
|
|
14
|
-
def initialize(base_url:,
|
|
14
|
+
def initialize(base_url:, secret_key:, merchant_id:, timeout:, open_timeout:)
|
|
15
15
|
@base_uri = URI(base_url.to_s.chomp("/"))
|
|
16
|
-
@
|
|
16
|
+
@secret_key = secret_key
|
|
17
17
|
@merchant_id = merchant_id
|
|
18
18
|
@timeout = timeout
|
|
19
19
|
@open_timeout = open_timeout
|
|
@@ -40,7 +40,7 @@ module Payriff
|
|
|
40
40
|
request = Net::HTTPGenericRequest.new(method, !payload.nil?, true, uri)
|
|
41
41
|
request["Accept"] = accept
|
|
42
42
|
request["User-Agent"] = "payriff-ruby/#{VERSION}"
|
|
43
|
-
request["Authorization"] = @
|
|
43
|
+
request["Authorization"] = @secret_key
|
|
44
44
|
(headers || {}).each { |name, value| request[name] = value.to_s unless value.nil? || value.to_s.empty? }
|
|
45
45
|
if payload
|
|
46
46
|
request["Content-Type"] = "application/json"
|
data/lib/payriff/version.rb
CHANGED
data/lib/payriff.rb
CHANGED
|
@@ -5,7 +5,6 @@ require_relative "payriff/version"
|
|
|
5
5
|
require_relative "payriff/errors"
|
|
6
6
|
require_relative "payriff/util"
|
|
7
7
|
require_relative "payriff/payriff_object"
|
|
8
|
-
require_relative "payriff/card"
|
|
9
8
|
require_relative "payriff/transport"
|
|
10
9
|
require_relative "payriff/services"
|
|
11
10
|
require_relative "payriff/client"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: payriff
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Payriff
|
|
@@ -9,8 +9,8 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
|
-
description: Orders,
|
|
13
|
-
|
|
12
|
+
description: Orders, saved-card payments, saved cards, transactions, payouts and invoices
|
|
13
|
+
for Payriff merchants.
|
|
14
14
|
executables: []
|
|
15
15
|
extensions: []
|
|
16
16
|
extra_rdoc_files: []
|
|
@@ -19,7 +19,6 @@ files:
|
|
|
19
19
|
- LICENSE
|
|
20
20
|
- README.md
|
|
21
21
|
- lib/payriff.rb
|
|
22
|
-
- lib/payriff/card.rb
|
|
23
22
|
- lib/payriff/client.rb
|
|
24
23
|
- lib/payriff/errors.rb
|
|
25
24
|
- lib/payriff/payriff_object.rb
|
|
@@ -47,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
47
46
|
- !ruby/object:Gem::Version
|
|
48
47
|
version: '0'
|
|
49
48
|
requirements: []
|
|
50
|
-
rubygems_version:
|
|
49
|
+
rubygems_version: 3.6.9
|
|
51
50
|
specification_version: 4
|
|
52
51
|
summary: Ruby client for the Payriff merchant API
|
|
53
52
|
test_files: []
|
data/lib/payriff/card.rb
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "json"
|
|
4
|
-
require "openssl"
|
|
5
|
-
|
|
6
|
-
module Payriff
|
|
7
|
-
class Card
|
|
8
|
-
def initialize(pan:, card_holder:, expiry_month:, expiry_year:, cvv:)
|
|
9
|
-
pan = digits(pan, "pan")
|
|
10
|
-
raise ArgumentError, "pan must contain 12-19 digits" unless pan.length.between?(12, 19)
|
|
11
|
-
|
|
12
|
-
month = digits(expiry_month, "expiry_month").to_i
|
|
13
|
-
raise ArgumentError, "expiry_month must be 1-12" unless month.between?(1, 12)
|
|
14
|
-
|
|
15
|
-
year = digits(expiry_year, "expiry_year")
|
|
16
|
-
year = "20#{year}" if year.length == 2
|
|
17
|
-
raise ArgumentError, "expiry_year must have 2 or 4 digits" unless year.length == 4
|
|
18
|
-
|
|
19
|
-
cvv = digits(cvv, "cvv")
|
|
20
|
-
raise ArgumentError, "cvv must contain 3-4 digits" unless cvv.length.between?(3, 4)
|
|
21
|
-
raise ArgumentError, "card_holder is required" if card_holder.nil?
|
|
22
|
-
|
|
23
|
-
@payload = JSON.generate(
|
|
24
|
-
"pan" => pan,
|
|
25
|
-
"cardHolder" => card_holder.to_s.strip,
|
|
26
|
-
"expiryYear" => year,
|
|
27
|
-
"expiryMonth" => format("%02d", month),
|
|
28
|
-
"cvv" => cvv
|
|
29
|
-
).freeze
|
|
30
|
-
@masked = "#{pan[0, 6]}******#{pan[-4, 4]}"
|
|
31
|
-
@expiry = "#{format('%02d', month)}/#{year}"
|
|
32
|
-
freeze
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def to_s
|
|
36
|
-
"#<Payriff::Card #{@masked} #{@expiry}>"
|
|
37
|
-
end
|
|
38
|
-
alias inspect to_s
|
|
39
|
-
|
|
40
|
-
def to_json(*args)
|
|
41
|
-
to_s.to_json(*args)
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def marshal_dump
|
|
45
|
-
raise TypeError, "Payriff::Card cannot be serialized"
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def encrypted_payload(public_key)
|
|
49
|
-
CardEncryptor.encrypt(public_key, @payload)
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
private
|
|
53
|
-
|
|
54
|
-
def digits(value, field)
|
|
55
|
-
raise ArgumentError, "#{field} must contain digits only" if value.nil?
|
|
56
|
-
|
|
57
|
-
v = value.to_s.gsub(/[\s-]/, "")
|
|
58
|
-
raise ArgumentError, "#{field} must contain digits only" unless v.match?(/\A[0-9]+\z/)
|
|
59
|
-
|
|
60
|
-
v
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
module CardEncryptor
|
|
65
|
-
PRODUCTION_KEY =
|
|
66
|
-
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxRq5a+44T6Dac60XmVRQ/7cpPyFsBnamXbJlRVJk8CnES5Re5tVMohyD0hZr" \
|
|
67
|
-
"3zcQj+bxodYB4zZpQTPlrXvFBC3zz+rXnGlevxBQ6W2d3QC9q8vWH8p3ZOwTO3qVvDSHH9o+hMMRNbJ7kueq/KZlX/F+bjZ23CZw7iXE" \
|
|
68
|
-
"GQT3HYVYnnHsvpaguYDteWBag2sPPLLsVjeB3zhTfQ7OsWp5XTkDuRwLugHPvs6RHLcwGCnodukWyvwUaEUQR/kMGC+RbMsAIVkcLMP5" \
|
|
69
|
-
"csfR3Xo7Gi98+i44iLN00f7gE8QvEmvv8xDspyTAjDEL1a5gK7TijJ3yLG/Bwa1rr1uskYy7lwIDAQAB"
|
|
70
|
-
|
|
71
|
-
OAEP = { rsa_padding_mode: "oaep", rsa_oaep_md: "sha256", rsa_mgf1_md: "sha256" }.freeze
|
|
72
|
-
|
|
73
|
-
module_function
|
|
74
|
-
|
|
75
|
-
def parse_key(base64_or_pem)
|
|
76
|
-
body = base64_or_pem.to_s.gsub(/-----(BEGIN|END) PUBLIC KEY-----|\s/, "")
|
|
77
|
-
key = OpenSSL::PKey.read(body.unpack1("m0"))
|
|
78
|
-
raise ArgumentError, "Invalid RSA public key" unless key.is_a?(OpenSSL::PKey::RSA) && !key.private?
|
|
79
|
-
|
|
80
|
-
key
|
|
81
|
-
rescue OpenSSL::PKey::PKeyError, ArgumentError
|
|
82
|
-
raise ArgumentError, "Invalid RSA public key"
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
def encrypt(public_key, payload)
|
|
86
|
-
aes_key = OpenSSL::Random.random_bytes(32)
|
|
87
|
-
iv = OpenSSL::Random.random_bytes(12)
|
|
88
|
-
cipher = OpenSSL::Cipher.new("aes-256-gcm").encrypt
|
|
89
|
-
cipher.key = aes_key
|
|
90
|
-
cipher.iv = iv
|
|
91
|
-
ciphertext = cipher.update(payload) + cipher.final + cipher.auth_tag(16)
|
|
92
|
-
secret = public_key.encrypt(aes_key + iv, OAEP)
|
|
93
|
-
{ message: [ciphertext].pack("m0"), secret_key: [secret].pack("m0") }
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
end
|