wompi-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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: eccdc54723ba105e0a434ba266a7fd26ad1e7d9c6bbbe12b77ceb76b19280947
4
+ data.tar.gz: 0d61f01183970ac8c726f2e8772245bf181957340a3664c87a57ad6a1ea29e13
5
+ SHA512:
6
+ metadata.gz: 1c6b67a6ae65f0be6c5942d47e9cd565d954343554a7b09ec1a52f3a8100b5d42fda05f4f3ff28927b4bc368078b303c22c2f787ae83a15e6dd57bf655c480c9
7
+ data.tar.gz: 6c618486813eedec788180b329aa7984d07905ed9e07c3dc3e74a91940f2700f94039715e1ecae049955217ee9ec83fd4c6bc7c13969ca17eae68dde78cccbd6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-12-26
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 wjorellano
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # Wompi::Ruby
2
+
3
+ Una gema de Ruby de **alto nivel** diseñada para integrar la pasarela de pagos **Wompi Colombia** de forma sencilla. Esta gema abstrae la complejidad técnica de la API, encargándose automáticamente de la **firma de integridad**, el manejo de entornos y la tokenización.
4
+
5
+ ## Características
6
+
7
+ * ✅ **Firma de Integridad Automática**: Generación de hash SHA-256 para transacciones seguras.
8
+ * ✅ **Multimedio de Pago**: Soporte completo para Tarjetas, PSE y Nequi.
9
+ * ✅ **Links de Pago**: Creación y gestión de URLs de cobro con parámetros de seguridad.
10
+ * ✅ **Gestión de Transacciones**: Consulta de estados individuales y listado histórico con paginación inteligente.
11
+ * ✅ **Seguridad**: Manejo diferenciado de llaves públicas y privadas según los requisitos de cada endpoint.
12
+
13
+ ---
14
+
15
+ ## Instalación
16
+
17
+ Instala la gema y añádela a tu Gemfile ejecutando:
18
+
19
+ ```bash
20
+ gem 'wompi-ruby'
21
+ ```
22
+
23
+ Configuración
24
+ Configura tus credenciales en un inicializador (ej. config/initializers/wompi.rb en Rails):
25
+
26
+ ```ruby
27
+ Wompi.configure do |config|
28
+ config.public_key = 'pub_test_XXXXX'
29
+ config.private_key = 'prv_test_XXXXX'
30
+ config.environment = :sandbox # O :production
31
+ end
32
+ ```
33
+
34
+ ## Uso
35
+ 1. Requisito Legal: Token de Aceptación
36
+ Wompi requiere que el usuario acepte términos y condiciones. Debes obtener este token antes de procesar cualquier transacción
37
+
38
+ ```ruby
39
+ merchant = Wompi::Merchant.info
40
+ acceptance_token = merchant.dig('data', 'presigned_acceptance', 'acceptance_token')
41
+ ```
42
+ 2. Pagos con Tarjeta de Crédito
43
+ ```ruby
44
+ # 1. Tokenizar la tarjeta (Requiere Llave Pública)
45
+ token_res = Wompi::Token.create_card(
46
+ number: "4242424242424242",
47
+ cvc: "789",
48
+ exp_month: "12",
49
+ exp_year: "30",
50
+ card_holder: "juan perz"
51
+ )
52
+ card_token = token_res.dig('data', 'id')
53
+
54
+ # 2. Crear transacción (Maneja firma SHA-256 automáticamente)
55
+ transaction = Wompi::Transaction.create(
56
+ amount_in_cents: 3000000, # $30.000 COP
57
+ currency: "COP",
58
+ reference: "REF_#{Time.now.to_i}",
59
+ customer_email: "usuario@ejemplo.com",
60
+ payment_method: {
61
+ type: "CARD",
62
+ token: card_token,
63
+ installments: 1
64
+ },
65
+ acceptance_token: acceptance_token,
66
+ )
67
+ ```
68
+
69
+ 3. Pagos con PSE (Transferencia Bancaria)
70
+ ```ruby
71
+ # 1. Listar bancos disponibles
72
+ banks = Wompi::Pse.financial_institutions
73
+
74
+ # 2. Crear transacción
75
+ res = Wompi::Transaction.create(
76
+ amount_in_cents: 5000000,
77
+ currency: "COP",
78
+ reference: "PSE_#{Time.now.to_i}",
79
+ customer_email: "cliente@correo.com",
80
+ payment_method: {
81
+ type: "PSE",
82
+ financial_institution_code: "1022", # Código del banco obtenido del listado
83
+ user_type: 0, # 0: Persona, 1: Empresa
84
+ user_legal_id_type: "CC",
85
+ user_legal_id: "12345678",
86
+ payment_description: "Pago de suscripción"
87
+ },
88
+ redirect_url: "https://tu-tienda.com/pago/resultado",
89
+ acceptance_token: acceptance_token,
90
+ )
91
+
92
+ # El usuario debe ser redirigido a la URL del banco:
93
+ url_pago = res.dig('data', 'payment_method', 'extra', 'async_payment_url')
94
+ ```
95
+
96
+ 4. Pagos con Nequi
97
+ ```ruby
98
+ # 1. Tokenizar número celular (Llave Pública)
99
+ nequi_token = Wompi::Token.create_nequi("3004444444").dig('data', 'id')
100
+
101
+ # 2. Crear transacción
102
+ res = Wompi::Transaction.create(
103
+ amount_in_cents: 2500000,
104
+ currency: "COP",
105
+ reference: "NEQUI_#{Time.now.to_i}",
106
+ customer_email: "usuario@ejemplo.com",
107
+ payment_method: {
108
+ type: "NEQUI",
109
+ token: nequi_token,
110
+ phone_number: "3004444444"
111
+ },
112
+ acceptance_token: acceptance_token,
113
+ )
114
+ # El usuario recibirá una notificación PUSH en su app Nequi para aprobar el pago.
115
+ ```
116
+
117
+ 5. Links de Pago
118
+ Genera una URL compartible para tus clientes sin necesidad de un checkout propio:
119
+ ```ruby
120
+ link = Wompi::PaymentLink.create(
121
+ name: "Producto Digital",
122
+ amount_in_cents: 5000000,
123
+ currency: "COP",
124
+ single_use: true,
125
+ collect_shipping: false,
126
+ reference: "LNK_#{Time.now.to_i}",
127
+ )
128
+ puts "URL de Cobro: [https://checkout.wompi.co/l/#](https://checkout.wompi.co/l/#){link.dig('data', 'id')}"
129
+ ```
130
+
131
+ 6. Historial de Transacciones
132
+ La gema maneja la paginación y filtros obligatorios automáticamente:
133
+ ```ruby
134
+ # Obtener transacciones de los últimos 30 días (Valores por defecto)
135
+ history = Wompi::Transaction.all
136
+
137
+ # Búsqueda con filtros y rango extendido
138
+ history = Wompi::Transaction.all(
139
+ from_date: "2025-01-01T00:00:00",
140
+ until_date: "2025-12-31T23:59:59",
141
+ page_size: 50
142
+ )
143
+ ```
144
+
145
+ Manejo de Errores
146
+ La gema utiliza excepciones específicas para ayudarte a identificar problemas rápidamente:
147
+ ```ruby
148
+ begin
149
+ # Operación de Wompi
150
+ rescue Wompi::InvalidRequestError => e
151
+ # Error 422: Parámetros inválidos o error de validación de la API
152
+ puts "Errores detectados: #{e.messages}"
153
+ rescue Wompi::NotFoundError => e
154
+ # Error 404: No se encontró el recurso (ID inexistente)
155
+ puts "No encontrado: #{e.message}"
156
+ rescue Wompi::ApiError => e
157
+ # Otros errores de la API (500, 401, etc.)
158
+ puts "Error del servidor: #{e.message}"
159
+ end
160
+ ```
161
+
162
+ ## Usage
163
+
164
+ TODO: Write usage instructions here
165
+
166
+ ## Development
167
+
168
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
169
+
170
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
171
+
172
+ ## Contributing
173
+
174
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wjorellano/wompi-ruby.
175
+
176
+ ## License
177
+
178
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,37 @@
1
+ require 'faraday'
2
+ require 'json'
3
+
4
+ module Wompi
5
+ class Client
6
+ def initialize(config = Wompi.configuration)
7
+ @config = config
8
+ end
9
+
10
+ def connection
11
+ Faraday.new(url: @config.base_url) do |f|
12
+ f.request :json
13
+ f.response :json
14
+ f.adapter Faraday.default_adapter
15
+ end
16
+ end
17
+
18
+ def call(method, path, params = {}, auth_type: :private)
19
+ key = auth_type == :private ? @config.private_key : @config.public_key
20
+ response = connection.send(method, path, params) do |req|
21
+ req.headers['Authorization'] = "Bearer #{key}"
22
+ end
23
+ handle_response(response)
24
+ end
25
+
26
+ private
27
+
28
+ def handle_response(response)
29
+ case response.status
30
+ when 200, 201 then response.body
31
+ when 404 then raise NotFoundError, response.body.dig('error', 'reason')
32
+ when 422 then raise InvalidRequestError, response.body.dig('error', 'messages')
33
+ else raise ApiError, "Error: #{response.status}"
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,13 @@
1
+ module Wompi
2
+ class Configuration
3
+ attr_accessor :public_key, :private_key, :integrity_secret, :environment
4
+
5
+ def initialize
6
+ @environment = :sandbox
7
+ end
8
+
9
+ def base_url
10
+ @environment == :production ? "https://production.wompi.co/v1" : "https://sandbox.wompi.co/v1"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module Wompi
2
+ class Error < StandardError; end
3
+ class ApiError < Error; end
4
+
5
+ # Error 404: "La entidad solicitada no existe"
6
+ class NotFoundError < Error; end
7
+
8
+ # Error 422: Validaciones de entrada fallidas
9
+ class InvalidRequestError < Error
10
+ attr_reader :messages
11
+ def initialize(messages)
12
+ @messages = messages
13
+ super("Errores de validación: #{messages}")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ module Wompi
2
+ class Resource
3
+ def self.get(path, params = {}, auth_type: :private)
4
+ Client.new.call(:get, path, params, auth_type: auth_type)
5
+ end
6
+
7
+ def self.post(path, params = {}, auth_type: :private)
8
+ Client.new.call(:post, path, params, auth_type: auth_type)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Wompi
2
+ class Merchant < Resource
3
+ def self.info
4
+ get("merchants/#{Wompi.configuration.public_key}", {}, auth_type: :public)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module Wompi
2
+ class PaymentLink < Resource
3
+ def self.create(params = {})
4
+ secret = params.delete(:integrity_secret) || Wompi.configuration.integrity_secret
5
+
6
+ if secret
7
+ params[:signature] = Security::Signature.generate(
8
+ params[:reference],
9
+ params[:amount_in_cents],
10
+ params[:currency],
11
+ secret
12
+ )
13
+ end
14
+
15
+ post("payment_links", params, auth_type: :private)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ module Wompi
2
+ class Pse < Resource
3
+ def self.financial_institutions
4
+ get("pse/financial_institutions", {}, auth_type: :public)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ module Wompi
2
+ class Token < Resource
3
+ def self.create_card(params = {})
4
+ post("tokens/cards", params, auth_type: :public)
5
+ end
6
+
7
+ def self.create_nequi(phone_number)
8
+ post("tokens/nequi", { phone_number: phone_number }, auth_type: :public)
9
+ end
10
+
11
+ def self.check_nequi_status(token_id)
12
+ get("tokens/nequi/#{token_id}", {}, auth_type: :public)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,55 @@
1
+ require 'date'
2
+
3
+ module Wompi
4
+ class Transaction < Resource
5
+ def self.create(params = {})
6
+ secret = params.delete(:integrity_secret) || Wompi.configuration.integrity_secret
7
+
8
+ if secret
9
+ params[:signature] = Security::Signature.generate(
10
+ params[:reference],
11
+ params[:amount_in_cents],
12
+ params[:currency],
13
+ secret
14
+ )
15
+ end
16
+
17
+ post("transactions", params, auth_type: :private)
18
+ end
19
+
20
+ def self.void(id, params = {})
21
+ post("transactions/#{id}/void", params, auth_type: :private)
22
+ end
23
+
24
+ def self.find(id)
25
+ get("transactions/#{id}", {}, auth_type: :private)
26
+ end
27
+
28
+ def self.all(params = {})
29
+ now = DateTime.now
30
+ defaults = {
31
+ from_date: (now - 30).strftime('%Y-%m-%dT%H:%M:%S'),
32
+ until_date: now.strftime('%Y-%m-%dT%H:%M:%S'),
33
+ page: 1,
34
+ page_size: 20
35
+ }
36
+ get("transactions", defaults.merge(params), auth_type: :private)
37
+ end
38
+
39
+ def self.all_pages(params = {})
40
+ all_records = []
41
+ current_page = 1
42
+
43
+ loop do
44
+ response = all(params.merge(page: current_page))
45
+ records = response['data']
46
+ break if records.empty?
47
+
48
+ all_records.concat(records)
49
+ current_page += 1
50
+ end
51
+
52
+ all_records
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,12 @@
1
+ require 'digest'
2
+
3
+ module Wompi
4
+ module Security
5
+ class Signature
6
+ def self.generate(reference, amount_in_cents, currency, secret)
7
+ raw_string = "#{reference}#{amount_in_cents}#{currency}#{secret}"
8
+ Digest::SHA256.hexdigest(raw_string)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Wompi
4
+ module Ruby
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/wompi-ruby.rb ADDED
@@ -0,0 +1,22 @@
1
+ require_relative "wompi/version"
2
+ require_relative "wompi/configuration"
3
+ require_relative "wompi/error"
4
+ require_relative "wompi/client"
5
+ require_relative "wompi/resource"
6
+ require_relative "wompi/security/signature"
7
+ require_relative "wompi/resources/token"
8
+ require_relative "wompi/resources/transaction"
9
+ require_relative "wompi/resources/merchant"
10
+ require_relative "wompi/resources/pse"
11
+ require_relative "wompi/resources/payment_link"
12
+
13
+ module Wompi
14
+ class << self
15
+ attr_accessor :configuration
16
+ end
17
+
18
+ def self.configure
19
+ self.configuration ||= Configuration.new
20
+ yield(configuration)
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ module Wompi
2
+ module Ruby
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wompi-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - wjorellano
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: faraday
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '2.0'
26
+ description: A lightweight Ruby library to integrate Wompi payments, supporting PSE,
27
+ Credit Cards, Nequi, and Payment Links.
28
+ email:
29
+ - wilmanjunior2001@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - CHANGELOG.md
35
+ - LICENSE.txt
36
+ - README.md
37
+ - Rakefile
38
+ - lib/wompi-ruby.rb
39
+ - lib/wompi/client.rb
40
+ - lib/wompi/configuration.rb
41
+ - lib/wompi/error.rb
42
+ - lib/wompi/resource.rb
43
+ - lib/wompi/resources/merchant.rb
44
+ - lib/wompi/resources/payment_link.rb
45
+ - lib/wompi/resources/pse.rb
46
+ - lib/wompi/resources/token.rb
47
+ - lib/wompi/resources/transaction.rb
48
+ - lib/wompi/security/signature.rb
49
+ - lib/wompi/version.rb
50
+ - sig/wompi/ruby.rbs
51
+ homepage: https://github.com/wjorellano/wompi-ruby
52
+ licenses:
53
+ - MIT
54
+ metadata:
55
+ homepage_uri: https://github.com/wjorellano/wompi-ruby
56
+ source_code_uri: https://github.com/wjorellano/wompi-ruby
57
+ changelog_uri: https://github.com/wjorellano/wompi-ruby/blob/main/CHANGELOG.md
58
+ bug_tracker_uri: https://github.com/wjorellano/wompi-ruby/issues
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.2.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.7.2
74
+ specification_version: 4
75
+ summary: Ruby SDK for Wompi Payment Gateway integration.
76
+ test_files: []