pay-asaas 0.1.0.pre.alpha → 0.1.1

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: 1b569b26d2eacd591aad245cd92687459f6c419d5182a5514caeee1558f1b32e
4
- data.tar.gz: 0c91f916c6b7d83e633c1a5aa1a9caaec037dbfa00b52097384b1733a1dd2cfa
3
+ metadata.gz: e6fce30ee5fbaee0c63c697c766902af8971425a70c32b03270e227b1eac7085
4
+ data.tar.gz: 278b2b442a0ffc52074ccb4a14acc84640b18a3a56cf6409f54731c021415bac
5
5
  SHA512:
6
- metadata.gz: 160e4949149cdd12370128a59bdca7e4f02e9188bb61bd593053238a69a43c3186e4353863d962a90f78b462d78bb0ae6e11a2974d1fafe4d296d8b940b357a6
7
- data.tar.gz: 33baf9d3695fdd9627fa890a0a2d7ddfc530cfd95798352debd4ab103f9a033207e2cc58569982d1f4571456da7a8b96e8ed9b4744363fc225f937f57f2fca68
6
+ metadata.gz: '018adb7590219b99cf4ce86070c66b46c63b511b790c1db44a09b68360c61058f7b0880f5dbb0f7a6cdf6acf60ecab6bfc3c092e9a77b8d0b79526ecdc7335bd'
7
+ data.tar.gz: 457704afa98597c321b37a1bc447fc5087157e8df34ae84bbc3433f1629eea1d58adaa7fa5aa5e136ed8e0ecb5681857a1d7798c9f9f9f3727cf98a605a5219e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.1] - 2025-06-13
4
+
5
+ - Fixes payment sync in webhooks
6
+ - Does not create charge the not exists inside the webhook processor
7
+
3
8
  ## [0.1.0] - 2024-12-26
4
9
 
5
10
  - Initial release
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  Asaas payment processor for Pay gem (Payments engine for Ruby on Rails);
4
4
 
5
+ > [!WARNING]
5
6
  > This gem is a work in progress and is not ready for production use.
6
7
 
7
8
  > This gem is not affiliated with Asaas.
@@ -11,10 +12,9 @@ The goal for the first implementation is to support the following features:
11
12
  - [x] Customer creation
12
13
  - [x] Make a payment with PIX
13
14
  - [x] Support basic webhooks needed for the payment process
15
+ - [x] Pix QRCode sync
14
16
 
15
17
  Following features will be implemented in the future and I will be happy to receive contributions:
16
-
17
- - [ ] Pix QRCode sync
18
18
  - [ ] Credit Cards
19
19
  - [ ] Subscriptions
20
20
 
@@ -23,7 +23,7 @@ Following features will be implemented in the future and I will be happy to rece
23
23
  Install the gem and add to the application's Gemfile by executing:
24
24
 
25
25
  ```bash
26
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG;
26
+ bundle add pay-asaas;
27
27
  ```
28
28
 
29
29
  > Install pay gem if you haven't already: https://github.com/pay-rails/pay/blob/main/docs/1_installation.md
@@ -37,7 +37,7 @@ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG;
37
37
  ```yml
38
38
  asaas:
39
39
  api_key: xxxx
40
- api_url: https://sandbox.asaas.com/api/v3 # or https://www.asaas.com/api/v3
40
+ api_url: https://api-sandbox.asaas.com/v3 # or https://api.asaas.com/v3
41
41
  webhook_access_key: xxxx
42
42
  ```
43
43
 
@@ -53,12 +53,12 @@ Pay will also check environment variables for API keys:
53
53
 
54
54
  ## Customer
55
55
 
56
- **IMPORTANT**: For this add the document column to users table.
57
-
58
- The customer works the same as the other processors, but with the document additional field.
56
+ **IMPORTANT**: In order to create a customer with pay-asaas, you need to provide a document (cpf or cnpj) to the user. This can be in the form of the following columns, in order of preference:
57
+ - `document`
58
+ - `cpf`
59
+ - `cnpj`
59
60
 
60
- The document is not required to create the customer but is required to process payments. The document can be cpf ou cnpj
61
- without mask.
61
+ In asaas, the document is not required to create the customer but is required to process payments, as such, we choose to make the field mandatory in this gem.
62
62
 
63
63
  ## Charge
64
64
 
@@ -66,20 +66,41 @@ This first version of the gem will only support PIX payments.
66
66
 
67
67
  ```ruby
68
68
  @user.payment_processor.charge(15_00)
69
+
70
+ @user.payment_processor.charge(15_00, { attrs: { order_id: @order.id } })
69
71
  ```
70
72
 
71
73
  ## Webhooks
72
74
 
73
- The gem will provide a controller to handle the webhooks.
75
+ The gem provides a built-in controller to handle webhooks from Asaas. These webhooks are essential for keeping your payment records in sync with Asaas's systems.
76
+
77
+ ### Configuration
74
78
 
75
- To configure webhooks on your payment processor, use the following URLs while replacing example.org with your own
76
- domain:
79
+ To configure webhooks in your Asaas dashboard, use the following URL (replace `example.org` with your domain):
77
80
 
78
- - Asaas: https://example.org/pay/webhooks/asaas
81
+ - **Asaas Webhook URL**: `https://example.org/pay/webhooks/asaas`
82
+
83
+ ### Supported Events
84
+
85
+ The main goal of the webhooks right now is to sync payment statuses.
79
86
 
80
87
  > For now we listen to the events that are important for pix transactions. For more configuration options
81
88
  > see: https://github.com/pay-rails/pay/blob/main/docs/7_webhooks.md
82
89
 
90
+ ### Verify Webhooks
91
+
92
+ All incoming webhooks from Asaas will be verified using the `webhook_access_key` provided in the gem configuration.
93
+
94
+ ## Debugging
95
+
96
+ To debug HTTP requests made by our gem, which uses the httparty library, you can enable logging of all outgoing HTTP calls. Just add the following line to one of your initializers:
97
+
98
+ ```ruby
99
+ HTTParty::Basement.debug_output($stdout)
100
+ ```
101
+
102
+ This will print the full request and response details to the console, making it easier to troubleshoot issues during development.
103
+
83
104
  ## Development
84
105
 
85
106
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
@@ -95,6 +116,10 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERN
95
116
  to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
96
117
  the [code of conduct](https://github.com/[USERNAME]/pay-asaas/blob/master/CODE_OF_CONDUCT.md).
97
118
 
119
+ ### Running tests
120
+
121
+ In order to run the test, first add a .env with fake credentials at the root of the project, and create a database in the `spec/dummy` folder.
122
+
98
123
  ## License
99
124
 
100
125
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,12 @@
1
+ require "pay/asaas/client"
2
+ require "pay/asaas/api"
3
+
4
+ class Pay::Asaas::Api::CreditCard < Pay::Asaas::ApiClient
5
+ def self.resource_key
6
+ "creditCard"
7
+ end
8
+
9
+ def self.tokenize(params:)
10
+ request(:post, "creditCards/tokenize", body: params.to_json)
11
+ end
12
+ end
@@ -5,4 +5,8 @@ class Pay::Asaas::Api::Payment < Pay::Asaas::ApiClient
5
5
  def self.resource_key
6
6
  "payments"
7
7
  end
8
+
9
+ def self.fetch_qr_code(processor_id)
10
+ request(:get, "#{resource_key}/#{processor_id}/pixQrCode")
11
+ end
8
12
  end
@@ -4,32 +4,36 @@ module Pay
4
4
  module Asaas
5
5
  class Charge < Pay::Charge
6
6
  store_accessor :data, :status
7
- store_accessor :data, :qr_code
7
+ store_accessor :data, :pix_code
8
+ after_create :sync_pix_qr_code
8
9
 
9
10
  def self.sync(charge_id, object: nil, try: 0, retries: 1)
10
11
  object ||= Pay::Asaas::Api::Payment.find(id: charge_id)
11
12
 
12
- pay_customer = Pay::Customer.find_by(processor: :asaas, processor_id: object.customer)
13
+ customer_processor_id = get_attribute(object, :customer)
14
+ charge_processor_id = get_attribute(object, :id)
15
+ pay_customer = Pay::Customer.find_by(processor: :asaas, processor_id: customer_processor_id)
16
+
17
+ # rubocop:disable Layout/LineLength
13
18
  if pay_customer.blank?
14
19
  Rails.logger.debug do
15
- "Pay::Customer #{object.customer} is not in the database while syncing Asaas Charge #{object.id}"
20
+ "Pay::Customer #{customer_processor_id} is not in the database while syncing Asaas Charge #{charge_processor_id}"
16
21
  end
17
22
  return
18
23
  end
24
+ # rubocop:enable Layout/LineLength
19
25
 
20
26
  attrs = {
21
27
  amount: get_attribute(object, :value).to_f * 100,
22
28
  status: get_attribute(object, :status),
23
29
  }
24
30
 
25
- # Update or create the charge
26
- if (pay_charge = pay_customer.charges.find_by(processor_id: object.id))
31
+ # Update the charge
32
+ if (pay_charge = pay_customer.charges.find_by(processor_id: charge_processor_id))
27
33
  pay_charge.with_lock do
28
34
  pay_charge.update!(attrs)
29
35
  end
30
36
  pay_charge
31
- else
32
- pay_customer.charges.create!(attrs.merge(processor_id: object.id))
33
37
  end
34
38
  rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
35
39
  try += 1
@@ -55,7 +59,17 @@ module Pay
55
59
  end
56
60
 
57
61
  def refund!(amount_to_refund)
58
- raise NotImplementedError, "Refunding charges is not supported yet by the Asaas fake processor"
62
+ raise NotImplementedError, "Refunding charges is not supported yet by the Asaas processor"
63
+ end
64
+
65
+ def sync_pix_qr_code
66
+ return unless payment_method_type == "pix"
67
+
68
+ response = Pay::Asaas::Api::Payment.fetch_qr_code(processor_id)
69
+
70
+ update!(
71
+ pix_code: response["payload"],
72
+ )
59
73
  end
60
74
  end
61
75
  end
@@ -28,7 +28,6 @@ module Pay::Asaas
28
28
 
29
29
  def configure
30
30
  yield(configuration)
31
- configuration.validate!
32
31
  end
33
32
 
34
33
  def headers
@@ -44,6 +43,7 @@ module Pay::Asaas
44
43
  end
45
44
 
46
45
  def request(method, path, options = {})
46
+ configuration.validate!
47
47
  response = HTTParty.send(
48
48
  method,
49
49
  "#{configuration.base_url}/#{path}",
@@ -9,7 +9,21 @@ module Pay
9
9
  has_one :default_payment_method, -> { where(default: true) }, class_name: "Pay::Asaas::PaymentMethod"
10
10
 
11
11
  def api_record_attributes
12
- { email: email, name: customer_name, cpfCnpj: owner.document, externalReference: owner.id }
12
+ document = if owner.respond_to?(:document)
13
+ owner.document
14
+ elsif owner.respond_to?(:cpf)
15
+ owner.cpf
16
+ elsif owner.respond_to?(:cnpj)
17
+ owner.cnpj
18
+ else
19
+ raise StandardError, "The document attribute, and it's alternatives are not implemented for #{owner.class}"
20
+ end
21
+
22
+ raise StandardError, "The document attribute is required" if document.nil?
23
+
24
+ document = document.gsub(/[^\d]/, "")
25
+
26
+ { email: email, name: customer_name, cpfCnpj: document, externalReference: owner.id }
13
27
  end
14
28
 
15
29
  def api_record
@@ -47,6 +61,8 @@ module Pay
47
61
  payment_method_type: "pix",
48
62
  }
49
63
 
64
+ attrs = attrs.merge(options[:attrs]) if options[:attrs]
65
+
50
66
  charge = charges.find_or_initialize_by(processor_id: transaction["id"])
51
67
  charge.update(attrs)
52
68
  charge
@@ -1,5 +1,5 @@
1
1
  module Pay
2
2
  module Asaas
3
- VERSION = "0.1.0-alpha"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -4,8 +4,6 @@ module Pay
4
4
  class PaymentSync
5
5
  def call(event)
6
6
  Pay::Asaas::Charge.sync(event["payment"]["id"], object: event["payment"])
7
-
8
- # TODO: Add notifications
9
7
  end
10
8
  end
11
9
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pay-asaas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre.alpha
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - PedroAugustoRamalhoDuarte
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2024-12-29 00:00:00.000000000 Z
10
+ date: 2025-06-13 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activesupport
@@ -51,34 +51,6 @@ dependencies:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '8'
54
- - !ruby/object:Gem::Dependency
55
- name: database_cleaner-active_record
56
- requirement: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: '0'
61
- type: :development
62
- prerelease: false
63
- version_requirements: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: '0'
68
- - !ruby/object:Gem::Dependency
69
- name: rspec-rails
70
- requirement: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - "~>"
73
- - !ruby/object:Gem::Version
74
- version: 6.0.0
75
- type: :development
76
- prerelease: false
77
- version_requirements: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: 6.0.0
82
54
  email:
83
55
  - pedroaugustorduarte@gmail.com
84
56
  executables: []
@@ -97,6 +69,7 @@ files:
97
69
  - config/routes.rb
98
70
  - lib/pay/asaas.rb
99
71
  - lib/pay/asaas/api.rb
72
+ - lib/pay/asaas/api/credit_card.rb
100
73
  - lib/pay/asaas/api/customer.rb
101
74
  - lib/pay/asaas/api/payment.rb
102
75
  - lib/pay/asaas/charge.rb