myropay 1.0.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/README.md +63 -0
- data/lib/myropay/api_requestor.rb +52 -0
- data/lib/myropay/charge.rb +49 -0
- data/lib/myropay/errors.rb +21 -0
- data/lib/myropay/version.rb +3 -0
- data/lib/myropay.rb +25 -0
- metadata +48 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 196da98afb50b4e7b7245ba316967188f7c6179716da2f1292c1db32aa14f4a9
|
|
4
|
+
data.tar.gz: 2bda993df588d5d7820b61058b03c28c551d4e1e10cc8db5e3f6ce5c0e0ecc03
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f0dab237670d845ec81d93147977178b23078049cd73c3114bb1eef6ddc92c5d510b7cdee3077f73cd23e252069df9fbba4519f372fe862be62f8cc30e6a0560
|
|
7
|
+
data.tar.gz: e0112b4ab13ad02da5e829f07d341d724536b902ec3bce4b2de7a4c784b3c14e2962073d873ce6c1193b1f87512a92f8dbc11a54d85dcb3ebea09d996eb8f62a
|
data/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# myropay-ruby
|
|
2
|
+
|
|
3
|
+
Official Ruby library for the [MyroPay](https://myropay.com) API. No dependencies beyond the standard library (`net/http`, `json`).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
gem install myropay
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or add to your Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem 'myropay'
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```ruby
|
|
20
|
+
require 'myropay'
|
|
21
|
+
Myropay.api_key = 'sk_test_...'
|
|
22
|
+
|
|
23
|
+
charge = Myropay::Charge.create(
|
|
24
|
+
email: 'customer@email.com',
|
|
25
|
+
amount: 500000, # whole currency units — ₦500,000, not kobo
|
|
26
|
+
currency: 'NGN', # optional, defaults to NGN
|
|
27
|
+
reference: 'ORD-1029', # optional, your own order id
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
redirect_to charge['checkout_url']
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Retrieve a charge later:
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
charge = Myropay::Charge.retrieve('cs_test_...')
|
|
37
|
+
if charge['status'] == 'completed'
|
|
38
|
+
# fulfil the order
|
|
39
|
+
end
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Important
|
|
43
|
+
|
|
44
|
+
- Your API key (`sk_live_...` / `sk_test_...`) must **never** reach a browser — use this library from your own server only. Get your keys from the MyroPay business dashboard under **API & Apps**.
|
|
45
|
+
- Amounts are in the currency's own whole units (e.g. `500000` NGN = ₦500,000), not subunits like kobo or cents.
|
|
46
|
+
- For a client-side "Pay with MyroPay" button/modal that never needs your secret key, use the [Checkout SDK](https://dev.myropay.com#sdk) (`myropay.js`) instead — this gem is for server-side charge creation and verification.
|
|
47
|
+
|
|
48
|
+
## Errors
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
begin
|
|
52
|
+
Myropay::Charge.create(amount: 5000) # missing email
|
|
53
|
+
rescue Myropay::InvalidRequestError => e
|
|
54
|
+
# missing/invalid parameters, no request was sent
|
|
55
|
+
rescue Myropay::APIError => e
|
|
56
|
+
# the API itself returned an error
|
|
57
|
+
puts "#{e.message} (HTTP #{e.http_status})"
|
|
58
|
+
end
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
MIT
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
module Myropay
|
|
2
|
+
module APIRequestor
|
|
3
|
+
module_function
|
|
4
|
+
|
|
5
|
+
def request(method, query: {}, body: nil, api_key: nil)
|
|
6
|
+
key = api_key || Myropay.api_key
|
|
7
|
+
if key.nil? || key.empty?
|
|
8
|
+
raise InvalidRequestError, 'No API key provided. Set Myropay.api_key = "sk_test_..." before making ' \
|
|
9
|
+
'a request, or pass api_key: directly to the call.'
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
uri = URI(Myropay::API_BASE)
|
|
13
|
+
uri.query = URI.encode_www_form(query) unless query.empty?
|
|
14
|
+
|
|
15
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
16
|
+
http.use_ssl = true
|
|
17
|
+
http.read_timeout = 30
|
|
18
|
+
|
|
19
|
+
req = case method
|
|
20
|
+
when :get then Net::HTTP::Get.new(uri)
|
|
21
|
+
when :post then Net::HTTP::Post.new(uri)
|
|
22
|
+
else raise ArgumentError, "Unsupported HTTP method: #{method}"
|
|
23
|
+
end
|
|
24
|
+
req['X-API-Key'] = key
|
|
25
|
+
req['Content-Type'] = 'application/json'
|
|
26
|
+
req.body = body.to_json if body
|
|
27
|
+
|
|
28
|
+
begin
|
|
29
|
+
res = http.request(req)
|
|
30
|
+
rescue StandardError => e
|
|
31
|
+
raise APIError, "Could not reach the MyroPay API: #{e.message}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
data =
|
|
35
|
+
begin
|
|
36
|
+
JSON.parse(res.body || '{}')
|
|
37
|
+
rescue JSON::ParserError
|
|
38
|
+
raise APIError.new('Invalid (non-JSON) response from the MyroPay API', http_status: res.code.to_i)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
status = res.code.to_i
|
|
42
|
+
if status == 401
|
|
43
|
+
raise AuthenticationError.new(data['message'] || 'Invalid API key', http_status: status, response: data)
|
|
44
|
+
end
|
|
45
|
+
if status >= 400 || data['success'] == false
|
|
46
|
+
raise APIError.new(data['message'] || 'MyroPay API request failed', http_status: status, response: data)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
data
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module Myropay
|
|
2
|
+
# A charge — a MyroPay hosted checkout session — for a customer to pay.
|
|
3
|
+
class Charge
|
|
4
|
+
class << self
|
|
5
|
+
# Create a charge.
|
|
6
|
+
#
|
|
7
|
+
# @param email [String] Customer email (required).
|
|
8
|
+
# @param amount [Numeric] Amount in the currency's own whole units —
|
|
9
|
+
# e.g. 500000 = ₦500,000 for NGN — NOT subunits/kobo.
|
|
10
|
+
# @param currency [String] Defaults to 'NGN'.
|
|
11
|
+
# @param reference [String] Your own order reference (optional).
|
|
12
|
+
# @param api_key [String] Overrides Myropay.api_key for this call only.
|
|
13
|
+
# @return [Hash] The created charge, including 'checkout_url' to
|
|
14
|
+
# redirect the customer to.
|
|
15
|
+
def create(email:, amount:, currency: 'NGN', reference: nil, description: nil,
|
|
16
|
+
success_url: nil, cancel_url: nil, metadata: nil, name: nil, api_key: nil)
|
|
17
|
+
raise InvalidRequestError, 'amount is required' if amount.nil? || amount.to_f <= 0
|
|
18
|
+
raise InvalidRequestError, 'email is required' if email.nil? || email.empty?
|
|
19
|
+
|
|
20
|
+
body = {
|
|
21
|
+
amount: amount,
|
|
22
|
+
currency: currency,
|
|
23
|
+
order_ref: reference,
|
|
24
|
+
description: description,
|
|
25
|
+
customer: { email: email, name: name },
|
|
26
|
+
success_url: success_url,
|
|
27
|
+
cancel_url: cancel_url,
|
|
28
|
+
metadata: metadata,
|
|
29
|
+
}
|
|
30
|
+
res = APIRequestor.request(:post, query: { action: 'create' }, body: body, api_key: api_key)
|
|
31
|
+
normalize(res)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Retrieve a charge by id (the session_id returned from create()).
|
|
35
|
+
def retrieve(id, api_key: nil)
|
|
36
|
+
raise InvalidRequestError, 'id is required' if id.nil? || id.empty?
|
|
37
|
+
res = APIRequestor.request(:get, query: { session_id: id }, api_key: api_key)
|
|
38
|
+
normalize(res['session'] || res)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def normalize(raw)
|
|
44
|
+
raw['id'] = raw['session_id']
|
|
45
|
+
raw
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Myropay
|
|
2
|
+
class MyropayError < StandardError
|
|
3
|
+
attr_reader :http_status, :response
|
|
4
|
+
|
|
5
|
+
def initialize(message = nil, http_status: nil, response: nil)
|
|
6
|
+
super(message)
|
|
7
|
+
@http_status = http_status
|
|
8
|
+
@response = response
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Raised before a request is even sent — e.g. no api_key configured,
|
|
13
|
+
# or a required parameter (amount, email) is missing.
|
|
14
|
+
class InvalidRequestError < MyropayError; end
|
|
15
|
+
|
|
16
|
+
# Raised when the MyroPay API itself returns an error response.
|
|
17
|
+
class APIError < MyropayError; end
|
|
18
|
+
|
|
19
|
+
# Raised on a 401 — invalid or missing API key.
|
|
20
|
+
class AuthenticationError < APIError; end
|
|
21
|
+
end
|
data/lib/myropay.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require 'net/http'
|
|
2
|
+
require 'json'
|
|
3
|
+
require 'uri'
|
|
4
|
+
|
|
5
|
+
require 'myropay/version'
|
|
6
|
+
require 'myropay/errors'
|
|
7
|
+
require 'myropay/api_requestor'
|
|
8
|
+
require 'myropay/charge'
|
|
9
|
+
|
|
10
|
+
# Official Ruby library for the MyroPay API.
|
|
11
|
+
#
|
|
12
|
+
# require 'myropay'
|
|
13
|
+
# Myropay.api_key = 'sk_test_...'
|
|
14
|
+
#
|
|
15
|
+
# charge = Myropay::Charge.create(
|
|
16
|
+
# email: 'customer@email.com',
|
|
17
|
+
# amount: 500000,
|
|
18
|
+
# )
|
|
19
|
+
module Myropay
|
|
20
|
+
API_BASE = 'https://checkout.myropay.com/api/sessions.php'.freeze
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
attr_accessor :api_key
|
|
24
|
+
end
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: myropay
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- MyroPay
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Create and verify MyroPay checkout charges from Ruby. No dependencies
|
|
13
|
+
beyond the standard library.
|
|
14
|
+
executables: []
|
|
15
|
+
extensions: []
|
|
16
|
+
extra_rdoc_files: []
|
|
17
|
+
files:
|
|
18
|
+
- README.md
|
|
19
|
+
- lib/myropay.rb
|
|
20
|
+
- lib/myropay/api_requestor.rb
|
|
21
|
+
- lib/myropay/charge.rb
|
|
22
|
+
- lib/myropay/errors.rb
|
|
23
|
+
- lib/myropay/version.rb
|
|
24
|
+
homepage: https://dev.myropay.com
|
|
25
|
+
licenses:
|
|
26
|
+
- MIT
|
|
27
|
+
metadata:
|
|
28
|
+
source_code_uri: https://github.com/myropayme/myropay-ruby
|
|
29
|
+
documentation_uri: https://dev.myropay.com
|
|
30
|
+
rubygems_mfa_required: 'true'
|
|
31
|
+
rdoc_options: []
|
|
32
|
+
require_paths:
|
|
33
|
+
- lib
|
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '2.6'
|
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
requirements: []
|
|
45
|
+
rubygems_version: 4.0.20
|
|
46
|
+
specification_version: 4
|
|
47
|
+
summary: Official Ruby library for the MyroPay API
|
|
48
|
+
test_files: []
|