pay-asaas 0.1.0.pre.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +14 -0
- data/.tool-versions +1 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +105 -0
- data/Rakefile +10 -0
- data/app/controllers/pay/webhooks/asaas_controller.rb +44 -0
- data/config/routes.rb +3 -0
- data/lib/pay/asaas/api/customer.rb +15 -0
- data/lib/pay/asaas/api/payment.rb +8 -0
- data/lib/pay/asaas/api.rb +2 -0
- data/lib/pay/asaas/charge.rb +62 -0
- data/lib/pay/asaas/client.rb +87 -0
- data/lib/pay/asaas/customer.rb +59 -0
- data/lib/pay/asaas/engine.rb +34 -0
- data/lib/pay/asaas/inflections.rb +6 -0
- data/lib/pay/asaas/payment_method.rb +10 -0
- data/lib/pay/asaas/version.rb +5 -0
- data/lib/pay/asaas/webhooks/payment_sync.rb +13 -0
- data/lib/pay/asaas.rb +63 -0
- data/sig/pay/asaas.rbs +6 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1b569b26d2eacd591aad245cd92687459f6c419d5182a5514caeee1558f1b32e
|
4
|
+
data.tar.gz: 0c91f916c6b7d83e633c1a5aa1a9caaec037dbfa00b52097384b1733a1dd2cfa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 160e4949149cdd12370128a59bdca7e4f02e9188bb61bd593053238a69a43c3186e4353863d962a90f78b462d78bb0ae6e11a2974d1fafe4d296d8b940b357a6
|
7
|
+
data.tar.gz: 33baf9d3695fdd9627fa890a0a2d7ddfc530cfd95798352debd4ab103f9a033207e2cc58569982d1f4571456da7a8b96e8ed9b4744363fc225f937f57f2fca68
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/.tool-versions
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby 3.4.1
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 PedroAugustoRamalhoDuarte
|
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,105 @@
|
|
1
|
+
# pay-asaas
|
2
|
+
|
3
|
+
Asaas payment processor for Pay gem (Payments engine for Ruby on Rails);
|
4
|
+
|
5
|
+
> This gem is a work in progress and is not ready for production use.
|
6
|
+
|
7
|
+
> This gem is not affiliated with Asaas.
|
8
|
+
|
9
|
+
The goal for the first implementation is to support the following features:
|
10
|
+
|
11
|
+
- [x] Customer creation
|
12
|
+
- [x] Make a payment with PIX
|
13
|
+
- [x] Support basic webhooks needed for the payment process
|
14
|
+
|
15
|
+
Following features will be implemented in the future and I will be happy to receive contributions:
|
16
|
+
|
17
|
+
- [ ] Pix QRCode sync
|
18
|
+
- [ ] Credit Cards
|
19
|
+
- [ ] Subscriptions
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
|
23
|
+
Install the gem and add to the application's Gemfile by executing:
|
24
|
+
|
25
|
+
```bash
|
26
|
+
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG;
|
27
|
+
```
|
28
|
+
|
29
|
+
> Install pay gem if you haven't already: https://github.com/pay-rails/pay/blob/main/docs/1_installation.md
|
30
|
+
|
31
|
+
## Configuration
|
32
|
+
|
33
|
+
### Rails Credentials
|
34
|
+
|
35
|
+
`rails credentials:edit --environment=development`
|
36
|
+
|
37
|
+
```yml
|
38
|
+
asaas:
|
39
|
+
api_key: xxxx
|
40
|
+
api_url: https://sandbox.asaas.com/api/v3 # or https://www.asaas.com/api/v3
|
41
|
+
webhook_access_key: xxxx
|
42
|
+
```
|
43
|
+
|
44
|
+
### Environment Variables
|
45
|
+
|
46
|
+
Pay will also check environment variables for API keys:
|
47
|
+
|
48
|
+
- `ASAAS_API_KEY`
|
49
|
+
- `ASAAS_API_URL`
|
50
|
+
- `ASAAS_WEBHOOK_ACCESS_KEY`
|
51
|
+
|
52
|
+
> Se other configuration options at: https://github.com/pay-rails/pay/blob/main/docs/2_configuration.md
|
53
|
+
|
54
|
+
## Customer
|
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.
|
59
|
+
|
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.
|
62
|
+
|
63
|
+
## Charge
|
64
|
+
|
65
|
+
This first version of the gem will only support PIX payments.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
@user.payment_processor.charge(15_00)
|
69
|
+
```
|
70
|
+
|
71
|
+
## Webhooks
|
72
|
+
|
73
|
+
The gem will provide a controller to handle the webhooks.
|
74
|
+
|
75
|
+
To configure webhooks on your payment processor, use the following URLs while replacing example.org with your own
|
76
|
+
domain:
|
77
|
+
|
78
|
+
- Asaas: https://example.org/pay/webhooks/asaas
|
79
|
+
|
80
|
+
> For now we listen to the events that are important for pix transactions. For more configuration options
|
81
|
+
> see: https://github.com/pay-rails/pay/blob/main/docs/7_webhooks.md
|
82
|
+
|
83
|
+
## Development
|
84
|
+
|
85
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
86
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
87
|
+
|
88
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
|
89
|
+
version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
|
90
|
+
push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pay-asaas. This project is intended
|
95
|
+
to be a safe, welcoming space for collaboration, and contributors are expected to adhere to
|
96
|
+
the [code of conduct](https://github.com/[USERNAME]/pay-asaas/blob/master/CODE_OF_CONDUCT.md).
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
101
|
+
|
102
|
+
## Code of Conduct
|
103
|
+
|
104
|
+
Everyone interacting in the Pay::Asaas project's codebases, issue trackers, chat rooms and mailing lists is expected to
|
105
|
+
follow the [code of conduct](https://github.com/[USERNAME]/pay-asaas/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
module Pay
|
2
|
+
module Webhooks
|
3
|
+
class AsaasController < Pay::ApplicationController
|
4
|
+
if Rails.application.config.action_controller.default_protect_from_forgery
|
5
|
+
skip_before_action :verify_authenticity_token
|
6
|
+
end
|
7
|
+
|
8
|
+
def create
|
9
|
+
if valid_signature?(request.headers["asaas-access-token"])
|
10
|
+
queue_event(verify_params.as_json)
|
11
|
+
head :ok
|
12
|
+
else
|
13
|
+
head :bad_request
|
14
|
+
end
|
15
|
+
rescue Pay::Asaas::Error => e
|
16
|
+
log_error(e)
|
17
|
+
head :bad_request
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def queue_event(event)
|
23
|
+
event_type = event["event"]&.downcase
|
24
|
+
return unless Pay::Webhooks.delegator.listening?("asaas.#{event_type}")
|
25
|
+
|
26
|
+
record = Pay::Webhook.create!(processor: :asaas, event_type: event_type, event: event)
|
27
|
+
Pay::Webhooks::ProcessJob.perform_later(record)
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid_signature?(signature)
|
31
|
+
signature == Pay::Asaas.webhook_access_key
|
32
|
+
end
|
33
|
+
|
34
|
+
def log_error(e)
|
35
|
+
logger.error e.message
|
36
|
+
e.backtrace.each { |line| logger.error " #{line}" }
|
37
|
+
end
|
38
|
+
|
39
|
+
def verify_params
|
40
|
+
params.except(:action, :controller).permit!
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "pay/asaas/client"
|
2
|
+
require "pay/asaas/api"
|
3
|
+
|
4
|
+
class Pay::Asaas::Api::Customer < Pay::Asaas::ApiClient
|
5
|
+
def self.resource_key
|
6
|
+
"customers"
|
7
|
+
end
|
8
|
+
|
9
|
+
# Set notificationDisabled by default
|
10
|
+
def self.create(params:)
|
11
|
+
params[:notificationDisabled] = true if params[:notificationDisabled].blank?
|
12
|
+
|
13
|
+
super
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "pay/asaas/api/payment"
|
2
|
+
|
3
|
+
module Pay
|
4
|
+
module Asaas
|
5
|
+
class Charge < Pay::Charge
|
6
|
+
store_accessor :data, :status
|
7
|
+
store_accessor :data, :qr_code
|
8
|
+
|
9
|
+
def self.sync(charge_id, object: nil, try: 0, retries: 1)
|
10
|
+
object ||= Pay::Asaas::Api::Payment.find(id: charge_id)
|
11
|
+
|
12
|
+
pay_customer = Pay::Customer.find_by(processor: :asaas, processor_id: object.customer)
|
13
|
+
if pay_customer.blank?
|
14
|
+
Rails.logger.debug do
|
15
|
+
"Pay::Customer #{object.customer} is not in the database while syncing Asaas Charge #{object.id}"
|
16
|
+
end
|
17
|
+
return
|
18
|
+
end
|
19
|
+
|
20
|
+
attrs = {
|
21
|
+
amount: get_attribute(object, :value).to_f * 100,
|
22
|
+
status: get_attribute(object, :status),
|
23
|
+
}
|
24
|
+
|
25
|
+
# Update or create the charge
|
26
|
+
if (pay_charge = pay_customer.charges.find_by(processor_id: object.id))
|
27
|
+
pay_charge.with_lock do
|
28
|
+
pay_charge.update!(attrs)
|
29
|
+
end
|
30
|
+
pay_charge
|
31
|
+
else
|
32
|
+
pay_customer.charges.create!(attrs.merge(processor_id: object.id))
|
33
|
+
end
|
34
|
+
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
|
35
|
+
try += 1
|
36
|
+
if try <= retries
|
37
|
+
sleep 0.1
|
38
|
+
retry
|
39
|
+
else
|
40
|
+
raise
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.get_attribute(obj, attr_name)
|
45
|
+
return obj[attr_name.to_s] if obj.is_a?(Hash)
|
46
|
+
return obj.send(attr_name) if obj.respond_to?(attr_name)
|
47
|
+
|
48
|
+
nil
|
49
|
+
end
|
50
|
+
|
51
|
+
def api_record
|
52
|
+
Pay::Asaas::Api::Payment.find(id: processor_id)
|
53
|
+
rescue ApiClient::ApiError => e
|
54
|
+
raise Pay::Asaas::Error, e
|
55
|
+
end
|
56
|
+
|
57
|
+
def refund!(amount_to_refund)
|
58
|
+
raise NotImplementedError, "Refunding charges is not supported yet by the Asaas fake processor"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "httparty"
|
2
|
+
|
3
|
+
module Pay::Asaas
|
4
|
+
class ApiClient
|
5
|
+
class Error < StandardError; end
|
6
|
+
class ConfigurationError < Error; end
|
7
|
+
class ApiError < Error; end
|
8
|
+
|
9
|
+
class Configuration
|
10
|
+
attr_accessor :api_key, :base_url
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@api_key = ENV["ASAAS_API_KEY"]
|
14
|
+
@base_url = ENV["ASAAS_API_URL"] || "https://sandbox.asaas.com/api/v3"
|
15
|
+
end
|
16
|
+
|
17
|
+
def validate!
|
18
|
+
raise ConfigurationError, "API key is required" unless api_key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
attr_writer :configuration
|
24
|
+
|
25
|
+
def configuration
|
26
|
+
@configuration ||= Configuration.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def configure
|
30
|
+
yield(configuration)
|
31
|
+
configuration.validate!
|
32
|
+
end
|
33
|
+
|
34
|
+
def headers
|
35
|
+
{
|
36
|
+
access_token: configuration.api_key,
|
37
|
+
"content-type": "application/json",
|
38
|
+
accept: "application/json",
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def resource_key
|
43
|
+
raise NotImplementedError, "You must implement the resource_key method"
|
44
|
+
end
|
45
|
+
|
46
|
+
def request(method, path, options = {})
|
47
|
+
response = HTTParty.send(
|
48
|
+
method,
|
49
|
+
"#{configuration.base_url}/#{path}",
|
50
|
+
{ headers: headers }.merge(options),
|
51
|
+
)
|
52
|
+
handle_response(response)
|
53
|
+
end
|
54
|
+
|
55
|
+
def all
|
56
|
+
request(:get, resource_key)
|
57
|
+
end
|
58
|
+
|
59
|
+
def create(params:)
|
60
|
+
request(:post, resource_key, body: params.to_json)
|
61
|
+
end
|
62
|
+
|
63
|
+
def update(id:, params:)
|
64
|
+
request(:put, "#{resource_key}/#{id}", body: params.to_json)
|
65
|
+
end
|
66
|
+
|
67
|
+
def find(id:)
|
68
|
+
request(:get, "#{resource_key}/#{id}")
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
def handle_response(response)
|
74
|
+
return response.parsed_response if response.success?
|
75
|
+
|
76
|
+
error_message = if response.parsed_response && response.parsed_response["errors"]&.first
|
77
|
+
response.parsed_response["errors"].first["description"]
|
78
|
+
else
|
79
|
+
"Request failed with status #{response.code}"
|
80
|
+
end
|
81
|
+
|
82
|
+
Rails.logger.error "Asaas API Error: #{response.code} - #{response.body}"
|
83
|
+
raise ApiError, error_message
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "pay/asaas/api/customer"
|
2
|
+
require "pay/asaas/api/payment"
|
3
|
+
|
4
|
+
module Pay
|
5
|
+
module Asaas
|
6
|
+
class Customer < Pay::Customer
|
7
|
+
has_many :charges, dependent: :destroy, class_name: "Pay::Asaas::Charge"
|
8
|
+
has_many :payment_methods, dependent: :destroy, class_name: "Pay::Asaas::PaymentMethod"
|
9
|
+
has_one :default_payment_method, -> { where(default: true) }, class_name: "Pay::Asaas::PaymentMethod"
|
10
|
+
|
11
|
+
def api_record_attributes
|
12
|
+
{ email: email, name: customer_name, cpfCnpj: owner.document, externalReference: owner.id }
|
13
|
+
end
|
14
|
+
|
15
|
+
def api_record
|
16
|
+
if processor_id?
|
17
|
+
Pay::Asaas::Api::Customer.find(id: processor_id)
|
18
|
+
else
|
19
|
+
customer_response = Pay::Asaas::Api::Customer.create(params: api_record_attributes)
|
20
|
+
update!(processor_id: customer_response["id"])
|
21
|
+
customer_response
|
22
|
+
end
|
23
|
+
rescue ApiClient::ApiError => e
|
24
|
+
Rails.logger.error "[Pay] Error creating Asaas customer: #{e.message}"
|
25
|
+
raise Pay::Asaas::Error, e.message
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_api_record(**attributes)
|
29
|
+
api_record unless processor_id?
|
30
|
+
Pay::Asaas::Api::Customer.update!(id: processor_id, params: api_record_attributes.merge(attributes))
|
31
|
+
end
|
32
|
+
|
33
|
+
# Charges only for pix right now
|
34
|
+
def charge(amount, options = {})
|
35
|
+
# Setup the customer's default payment method
|
36
|
+
params = {
|
37
|
+
billingType: "PIX",
|
38
|
+
dueDate: Time.zone.today.to_s,
|
39
|
+
value: amount / 100.0, # Asaas expects a float
|
40
|
+
}.merge(options)
|
41
|
+
.merge(customer: processor_id || api_record["id"]) # Merge last to not let override customer
|
42
|
+
|
43
|
+
transaction = Pay::Asaas::Api::Payment.create(params: params)
|
44
|
+
|
45
|
+
attrs = {
|
46
|
+
amount: amount,
|
47
|
+
payment_method_type: "pix",
|
48
|
+
}
|
49
|
+
|
50
|
+
charge = charges.find_or_initialize_by(processor_id: transaction["id"])
|
51
|
+
charge.update(attrs)
|
52
|
+
charge
|
53
|
+
rescue ApiClient::ApiError => e
|
54
|
+
Rails.logger.error "[Pay] Error creating Asaas charge: #{e.message}"
|
55
|
+
raise Pay::Asaas::Error, e.message
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "rails/engine"
|
2
|
+
require "action_dispatch"
|
3
|
+
require "rails/engine/configuration"
|
4
|
+
|
5
|
+
module Pay
|
6
|
+
module Asaas
|
7
|
+
class Engine < ::Rails::Engine
|
8
|
+
engine_name "pay_asaas"
|
9
|
+
|
10
|
+
initializer "pay_asaas.processors" do |app|
|
11
|
+
if Pay.automount_routes
|
12
|
+
app.routes.append do
|
13
|
+
mount Pay::Asaas::Engine, at: Pay.routes_path
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
# Include the pay attributes for ActiveRecord models
|
18
|
+
ActiveSupport.on_load(:active_record) do
|
19
|
+
include Pay::Attributes
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Add webhook subscribers before app initializers define extras
|
24
|
+
# This keeps the processing in order so that changes have happened before user-defined webhook processors
|
25
|
+
config.before_initialize do
|
26
|
+
Pay::Asaas.configure_webhooks if Pay::Asaas.enabled?
|
27
|
+
end
|
28
|
+
|
29
|
+
config.to_prepare do
|
30
|
+
Pay::Asaas.setup if Pay::Asaas.enabled?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
module Pay
|
2
|
+
module Asaas
|
3
|
+
# For ASAAS with have 3 options of payments methods: CREDIT_CARD, BOLETO and PIX
|
4
|
+
class PaymentMethod < Pay::PaymentMethod
|
5
|
+
def self.sync(id, object: nil, try: 0, retries: 1)
|
6
|
+
raise NotImplementedError, "Syncing payment methods is not supported yet by the Asaas fake processor"
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/lib/pay/asaas.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require_relative "asaas/version"
|
2
|
+
require_relative "asaas/inflections"
|
3
|
+
require "pay/asaas/engine"
|
4
|
+
require "pay/asaas/client"
|
5
|
+
require "pay/errors"
|
6
|
+
require "pay/env"
|
7
|
+
|
8
|
+
module Pay
|
9
|
+
module Asaas
|
10
|
+
autoload :Customer, "pay/asaas/customer"
|
11
|
+
autoload :Charge, "pay/asaas/charge"
|
12
|
+
autoload :PaymentMethod, "pay/asaas/payment_method"
|
13
|
+
|
14
|
+
class Error < Pay::Error
|
15
|
+
end
|
16
|
+
|
17
|
+
module Webhooks
|
18
|
+
autoload :PaymentSync, "pay/asaas/webhooks/payment_sync"
|
19
|
+
end
|
20
|
+
|
21
|
+
# Setup configuration
|
22
|
+
|
23
|
+
# If users imports this gem its enable by default
|
24
|
+
def self.enabled?
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.setup
|
29
|
+
# Configure api client key in this setup
|
30
|
+
Pay::Asaas::ApiClient.configure do |config|
|
31
|
+
config.api_key = Pay::Asaas.api_key
|
32
|
+
config.base_url = Pay::Asaas.api_url
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
extend Pay::Env
|
37
|
+
|
38
|
+
def self.api_key
|
39
|
+
find_value_by_name(:asaas, :api_key)
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.api_url
|
43
|
+
find_value_by_name(:asaas, :api_url) || "https://sandbox.asaas.com/api/v3"
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.webhook_access_key
|
47
|
+
find_value_by_name(:asaas, :webhook_access_key)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.configure_webhooks
|
51
|
+
# https://docs.asaas.com/docs/webhook-para-cobrancas
|
52
|
+
Pay::Webhooks.configure do |events|
|
53
|
+
events.subscribe "asaas.payment_updated", Pay::Asaas::Webhooks::PaymentSync.new
|
54
|
+
events.subscribe "asaas.payment_confirmed", Pay::Asaas::Webhooks::PaymentSync.new
|
55
|
+
events.subscribe "asaas.payment_received", Pay::Asaas::Webhooks::PaymentSync.new
|
56
|
+
events.subscribe "asaas.payment_overdue", Pay::Asaas::Webhooks::PaymentSync.new
|
57
|
+
events.subscribe "asaas.payment_deleted", Pay::Asaas::Webhooks::PaymentSync.new
|
58
|
+
events.subscribe "asaas.payment_restored", Pay::Asaas::Webhooks::PaymentSync.new
|
59
|
+
events.subscribe "asaas.payment_refunded", Pay::Asaas::Webhooks::PaymentSync.new
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/sig/pay/asaas.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pay-asaas
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.pre.alpha
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- PedroAugustoRamalhoDuarte
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2024-12-29 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: activesupport
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '0'
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: httparty
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: pay
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '8'
|
47
|
+
type: :runtime
|
48
|
+
prerelease: false
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
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
|
+
email:
|
83
|
+
- pedroaugustorduarte@gmail.com
|
84
|
+
executables: []
|
85
|
+
extensions: []
|
86
|
+
extra_rdoc_files: []
|
87
|
+
files:
|
88
|
+
- ".rspec"
|
89
|
+
- ".rubocop.yml"
|
90
|
+
- ".tool-versions"
|
91
|
+
- CHANGELOG.md
|
92
|
+
- CODE_OF_CONDUCT.md
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- app/controllers/pay/webhooks/asaas_controller.rb
|
97
|
+
- config/routes.rb
|
98
|
+
- lib/pay/asaas.rb
|
99
|
+
- lib/pay/asaas/api.rb
|
100
|
+
- lib/pay/asaas/api/customer.rb
|
101
|
+
- lib/pay/asaas/api/payment.rb
|
102
|
+
- lib/pay/asaas/charge.rb
|
103
|
+
- lib/pay/asaas/client.rb
|
104
|
+
- lib/pay/asaas/customer.rb
|
105
|
+
- lib/pay/asaas/engine.rb
|
106
|
+
- lib/pay/asaas/inflections.rb
|
107
|
+
- lib/pay/asaas/payment_method.rb
|
108
|
+
- lib/pay/asaas/version.rb
|
109
|
+
- lib/pay/asaas/webhooks/payment_sync.rb
|
110
|
+
- sig/pay/asaas.rbs
|
111
|
+
homepage: https://github.com/PedroAugustoRamalhoDuarte/pay-asaas
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata:
|
115
|
+
allowed_push_host: https://rubygems.org/
|
116
|
+
homepage_uri: https://github.com/PedroAugustoRamalhoDuarte/pay-asaas
|
117
|
+
source_code_uri: https://github.com/PedroAugustoRamalhoDuarte/pay-asaas
|
118
|
+
changelog_uri: https://github.com/PedroAugustoRamalhoDuarte/pay-asaas
|
119
|
+
github_repo: https://github.com/PedroAugustoRamalhoDuarte/pay-asaas
|
120
|
+
rdoc_options: []
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: 3.0.0
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
requirements: []
|
134
|
+
rubygems_version: 3.6.2
|
135
|
+
specification_version: 4
|
136
|
+
summary: Asaas payment processor for pay gem (Payments engine for Ruby on Rails).
|
137
|
+
test_files: []
|