lavenda-pay-ruby 0.0.1 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 574e87e7c3cdf2f4a985cc4269a4691d929153f6e515e304411595b061e94a7a
4
- data.tar.gz: 4cd0279828335208b8a5582c144d9ceb88c5b67ca05f32f66dc842e86d8a1f07
3
+ metadata.gz: 39b5e1f0ee482056108e319e7c1231913d9cc2c34947b884176ee26214df40dd
4
+ data.tar.gz: 14d74a9dfec2e429cdc6f7f18099384b2d19162a1c3a0eb992f5d3ac3b7f9dfa
5
5
  SHA512:
6
- metadata.gz: 23c4df6684ff61b6462c940da2a61c4dbb265ea44bceffa5d7d86e29d4d0b3a4e4689f73b0a22f6496ad7fb072e005285cab609546342e85926329c7551a1cc8
7
- data.tar.gz: a266e6e3e0e4b556af750e0650c954a97496c3bf656c42ebf6062f86e7d6a690ef40b929f45a21f0fbef41fca13b41033a896cf65d6d74581762982d730d411f
6
+ metadata.gz: 4e771c230db541ff2f90b2e1178891a9d4cc341df303e457f8af55a6f80ebc6761a8f3cd9b88385405b0f29b41920c32792bb662daf3b76d73a4b79ec763f4dd
7
+ data.tar.gz: 2962469cbe01f0bd57645dfb997504f482e678ad7c1b02ba9d44281aaeec63bafc665fde208e218fc2829ae076f5e095d63e2c663936b808afbf518c541e193b
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  # Changelog
2
2
 
3
- ## 0.1.0
3
+ ## 0.0.3
4
4
 
5
- - Initial release: customers, orders and webhook handling extracted from the
6
- Catavento and Lavenda Store adapters.
5
+ - Add `LavendaPay::Payments::Confirm` to confirm a pending payment outside production.
6
+
7
+ ## 0.0.2
8
+
9
+ - Read settings from the `lavenda_pay` Rails credentials when the environment variable is not set.
10
+ - Add `LavendaPay::Url.order_path` to build the public order page link.
11
+
12
+ ## 0.0.1
13
+
14
+ - Initial release: customers, orders and webhook handling.
data/README.md CHANGED
@@ -6,25 +6,40 @@ Framework-agnostic (no Rails/ActiveSupport dependency): create customers and ord
6
6
  ## Installation
7
7
 
8
8
  ```ruby
9
- gem "lavenda-pay-ruby", github: "LaVendaSoftware/pay-ruby"
9
+ gem "lavenda-pay-ruby"
10
10
  ```
11
11
 
12
12
  ## Configuration
13
13
 
14
+ Settings are read from `LAVENDA_PAY_*` environment variables and, inside a Rails app, from the `lavenda_pay`
15
+ credentials, so a Rails app needs no initializer:
16
+
17
+ ```yaml
18
+ # bin/rails credentials:edit
19
+ lavenda_pay:
20
+ base_url: https://pay.example.com
21
+ api_token: ...
22
+ webhook_secret: ...
23
+ ```
24
+
25
+ | Setting | Environment variable | Credential |
26
+ | ---------------- | ---------------------------- | ------------------------------ |
27
+ | `base_url` | `LAVENDA_PAY_BASE_URL` | `lavenda_pay.base_url` |
28
+ | `api_token` | `LAVENDA_PAY_API_TOKEN` | `lavenda_pay.api_token` |
29
+ | `webhook_secret` | `LAVENDA_PAY_WEBHOOK_SECRET` | `lavenda_pay.webhook_secret` |
30
+
31
+ `base_url` is the root of the Lavenda Pay installation, without the `/api` suffix. Use the company domain: the API
32
+ accepts any host, but the public order pages are only served on the company's own domain.
33
+
34
+ Values set explicitly win over both:
35
+
14
36
  ```ruby
15
- # config/initializers/lavenda_pay.rb
16
37
  LavendaPay.configure do |config|
17
- config.base_url = ENV.fetch("LAVENDA_PAY_BASE_URL", Rails.application.credentials.dig(:lavenda_pay, :base_url))
18
- config.api_token = ENV.fetch("LAVENDA_PAY_API_TOKEN", Rails.application.credentials.dig(:lavenda_pay, :api_token))
19
- config.webhook_secret = ENV.fetch("LAVENDA_PAY_WEBHOOK_SECRET", Rails.application.credentials.dig(:lavenda_pay, :webhook_secret))
38
+ config.base_url = "https://pay.example.com"
39
+ config.logger = Rails.logger
20
40
  end
21
41
  ```
22
42
 
23
- `base_url` is the root of the Lavenda Pay installation, without the `/api` suffix.
24
-
25
- Outside Rails, set the values directly. Anything left unset falls back to the `LAVENDA_PAY_BASE_URL`,
26
- `LAVENDA_PAY_API_TOKEN` and `LAVENDA_PAY_WEBHOOK_SECRET` environment variables.
27
-
28
43
  Every operation is a class with `.call` that uses this global configuration. For several accounts, build a client and
29
44
  pass it to the operation:
30
45
 
@@ -68,6 +83,22 @@ order.pid # => "order_..."
68
83
  order.status # => "draft"
69
84
 
70
85
  LavendaPay::Orders::Find.call(order.pid)
86
+
87
+ # Public payment page for the order, built from `base_url`.
88
+ LavendaPay::Url.order_path(order.pid) # => "https://pay.example.com/orders/order_..."
89
+ ```
90
+
91
+ ## Payments
92
+
93
+ Sandbox only: confirm a pending payment as if the customer had paid it, to test your payment flow
94
+ end to end. LavendaPay marks it as paid and, when the gateway supports it, confirms it there too.
95
+ The endpoint does not exist in production, where it raises `LavendaPay::NotFoundError`.
96
+
97
+ ```ruby
98
+ # The payment pid comes from the payment outcome webhooks.
99
+ payment = LavendaPay::Payments::Confirm.call("pay_...")
100
+ payment.status # => "paid"
101
+ payment.gateway_confirmation # => "confirmed", "failed" or "unsupported"
71
102
  ```
72
103
 
73
104
  ## Errors
@@ -14,11 +14,11 @@ module LavendaPay
14
14
  end
15
15
 
16
16
  # Root URL of the Lavenda Pay installation, without the `/api` suffix.
17
- def base_url = @base_url || ENV["LAVENDA_PAY_BASE_URL"]
17
+ def base_url = @base_url || setting(:base_url)
18
18
 
19
- def api_token = @api_token || ENV["LAVENDA_PAY_API_TOKEN"]
19
+ def api_token = @api_token || setting(:api_token)
20
20
 
21
- def webhook_secret = @webhook_secret || ENV["LAVENDA_PAY_WEBHOOK_SECRET"]
21
+ def webhook_secret = @webhook_secret || setting(:webhook_secret)
22
22
 
23
23
  def validate!
24
24
  raise ConfigurationError, "LavendaPay base_url is not configured (LAVENDA_PAY_BASE_URL)" if blank?(base_url)
@@ -30,5 +30,18 @@ module LavendaPay
30
30
  private
31
31
 
32
32
  def blank?(value) = value.nil? || value.to_s.strip.empty?
33
+
34
+ # `LAVENDA_PAY_<KEY>` environment variable, then the `lavenda_pay` Rails
35
+ # credentials when running inside a Rails app.
36
+ def setting(key)
37
+ value = ENV["LAVENDA_PAY_#{key.to_s.upcase}"]
38
+ blank?(value) ? rails_credential(key) : value
39
+ end
40
+
41
+ def rails_credential(key)
42
+ return unless defined?(::Rails.application) && ::Rails.application
43
+
44
+ ::Rails.application.credentials.dig(:lavenda_pay, key)
45
+ end
33
46
  end
34
47
  end
@@ -0,0 +1,16 @@
1
+ module LavendaPay
2
+ module Payments
3
+ # Sandbox only: marks a pending payment as paid, and confirms it on the
4
+ # gateway when the gateway supports it. Production answers 404 (NotFoundError).
5
+ #
6
+ # LavendaPay::Payments::Confirm.call("pay_...") # => LavendaPay::Payment
7
+ class Confirm < Operation
8
+ def call(pid)
9
+ body = http.post("/v1/payments/#{escape(pid)}/confirmation")
10
+ gateway_confirmation = body.dig("meta", "gateway_confirmation")
11
+
12
+ Payment.new((body["payment"] || body).merge("gateway_confirmation" => gateway_confirmation))
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ module LavendaPay
2
+ class Payment < Resource
3
+ # Attributes: pid, status, and gateway_confirmation after a sandbox
4
+ # confirmation ("confirmed", "failed" or "unsupported").
5
+ end
6
+ end
@@ -0,0 +1,14 @@
1
+ require "uri"
2
+
3
+ module LavendaPay
4
+ # Links to the public Lavenda Pay pages, built from `base_url`.
5
+ #
6
+ # LavendaPay::Url.order_path("order_...") # => "https://.../orders/order_..."
7
+ module Url
8
+ module_function
9
+
10
+ def order_path(pid, configuration: LavendaPay.configuration)
11
+ "#{configuration.base_url.to_s.chomp("/")}/orders/#{URI.encode_www_form_component(pid.to_s)}"
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module LavendaPay
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/lavenda_pay.rb CHANGED
@@ -5,6 +5,7 @@ require_relative "lavenda_pay/http"
5
5
  require_relative "lavenda_pay/resource"
6
6
  require_relative "lavenda_pay/resources/customer"
7
7
  require_relative "lavenda_pay/resources/order"
8
+ require_relative "lavenda_pay/resources/payment"
8
9
  require_relative "lavenda_pay/client"
9
10
  require_relative "lavenda_pay/operation"
10
11
  require_relative "lavenda_pay/customers/create"
@@ -13,6 +14,8 @@ require_relative "lavenda_pay/customers/list"
13
14
  require_relative "lavenda_pay/customers/find_by"
14
15
  require_relative "lavenda_pay/orders/create"
15
16
  require_relative "lavenda_pay/orders/find"
17
+ require_relative "lavenda_pay/payments/confirm"
18
+ require_relative "lavenda_pay/url"
16
19
  require_relative "lavenda_pay/webhooks"
17
20
  require_relative "lavenda_pay/engine" if defined?(Rails::Engine)
18
21
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lavenda-pay-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - La Venda Software
@@ -118,9 +118,12 @@ files:
118
118
  - lib/lavenda_pay/operation.rb
119
119
  - lib/lavenda_pay/orders/create.rb
120
120
  - lib/lavenda_pay/orders/find.rb
121
+ - lib/lavenda_pay/payments/confirm.rb
121
122
  - lib/lavenda_pay/resource.rb
122
123
  - lib/lavenda_pay/resources/customer.rb
123
124
  - lib/lavenda_pay/resources/order.rb
125
+ - lib/lavenda_pay/resources/payment.rb
126
+ - lib/lavenda_pay/url.rb
124
127
  - lib/lavenda_pay/version.rb
125
128
  - lib/lavenda_pay/webhooks.rb
126
129
  - lib/lavenda_pay/webhooks/construct_event.rb
@@ -150,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
153
  - !ruby/object:Gem::Version
151
154
  version: '0'
152
155
  requirements: []
153
- rubygems_version: 4.0.13
156
+ rubygems_version: 4.0.22
154
157
  specification_version: 4
155
158
  summary: Ruby client for the Lavenda Pay payments API.
156
159
  test_files: []