facebook_commerce 0.1.0
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/CHANGELOG.md +5 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +33 -0
- data/Rakefile +8 -0
- data/lib/facebook_commerce/version.rb +5 -0
- data/lib/facebook_commerce.rb +187 -0
- data/sig/facebook_commerce.rbs +4 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 20696062e7f6acdd971a438426a5fca169814aa90aa0182dee07e88c5cf77a2f
|
4
|
+
data.tar.gz: 56837aeacd2b316bb7bcc7ce16351f4dee3fadb91fb766cb13b60ed014ced976
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90425ba85eaf12f79e96ae2db351ac45483e3bcda6260d4cb04ba1d1af2fa9ff36a1bcf9c52d77125a178e95c85deb9403e9eb649eeeef171d4de8b73ed240b3
|
7
|
+
data.tar.gz: 26afb26eb116a583e12d0515fdfa457729fcf6f568b831a77ffcd70097251627adc64bee59178a87fd5d55ac4cf3746f89f4e0f228362ab2b85acb9b0454b94c
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Piers Chambers
|
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,33 @@
|
|
1
|
+
# FacebookCommerce
|
2
|
+
|
3
|
+
Unofficial Ruby wrapper for the Facebook Commerce API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
TODO: Write usage instructions here
|
20
|
+
|
21
|
+
## Development
|
22
|
+
|
23
|
+
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.
|
24
|
+
|
25
|
+
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).
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/varyonic/facebook_commerce.
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
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,187 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "facebook_commerce/version"
|
4
|
+
|
5
|
+
require 'benchmark'
|
6
|
+
require 'json'
|
7
|
+
require 'logger'
|
8
|
+
require 'securerandom'
|
9
|
+
|
10
|
+
module FacebookCommerce
|
11
|
+
BASE_URL = 'https://graph.facebook.com/'
|
12
|
+
class Error < StandardError; end
|
13
|
+
|
14
|
+
def self.configure(&block)
|
15
|
+
yield @config
|
16
|
+
end
|
17
|
+
|
18
|
+
def config
|
19
|
+
@config
|
20
|
+
end
|
21
|
+
|
22
|
+
class Api
|
23
|
+
class UnexpectedHttpResponse < StandardError
|
24
|
+
def initialize(response)
|
25
|
+
super response.message || response.code
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_reader :access_token # See https://business.facebook.com/commerce_permission_wizard
|
30
|
+
attr_reader :cms_id
|
31
|
+
attr_accessor :logger
|
32
|
+
|
33
|
+
# config[:cms_id]
|
34
|
+
# config[:access_token]
|
35
|
+
def initialize(config = super.config)
|
36
|
+
@cms_id = config.fetch(:cms_id)
|
37
|
+
@access_token = config.fetch(:access_token)
|
38
|
+
@logger = config[:logger] || Logger.new(STDOUT)
|
39
|
+
end
|
40
|
+
|
41
|
+
protected
|
42
|
+
|
43
|
+
def get(path, data = {})
|
44
|
+
path = "#{path}?#{URI.encode_www_form(data.merge(access_token: access_token))}"
|
45
|
+
JSON.parse(send_request('GET', BASE_URL + path))
|
46
|
+
end
|
47
|
+
|
48
|
+
def post(path, data = {})
|
49
|
+
data.merge!(access_token: access_token)
|
50
|
+
JSON.parse(send_request('POST', BASE_URL + path, data))
|
51
|
+
end
|
52
|
+
|
53
|
+
# Accepts hash of fields to send.
|
54
|
+
# Returns response body if successful else raises exception.
|
55
|
+
def send_request(method, path, fields = {})
|
56
|
+
uri = URI(path)
|
57
|
+
connection = https(uri)
|
58
|
+
data = fields.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&")
|
59
|
+
response = log_request_response(method, uri, data) do |m, u, d|
|
60
|
+
connection.send_request(m, u.to_s, d)
|
61
|
+
end
|
62
|
+
fail_unless_expected_response response, Net::HTTPSuccess
|
63
|
+
response.body
|
64
|
+
end
|
65
|
+
|
66
|
+
def https(uri)
|
67
|
+
Net::HTTP.new(uri.host, uri.port).tap do |http|
|
68
|
+
http.use_ssl = true
|
69
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Log method, URI, data
|
74
|
+
# Start timer.
|
75
|
+
# Yield method, URI, data.
|
76
|
+
# Log response and time taken.
|
77
|
+
def log_request_response(method, uri, data = nil)
|
78
|
+
logger.info "[#{self.class.name}] request = #{method} #{uri}#{data ? '?' + data : ''}"
|
79
|
+
response = nil
|
80
|
+
tms = Benchmark.measure do
|
81
|
+
response = yield method, uri, data
|
82
|
+
end
|
83
|
+
logger.info("[#{self.class.name}] response (#{(tms.real*1000).round(3)}ms): #{response.inspect} #{response.body}")
|
84
|
+
response
|
85
|
+
end
|
86
|
+
|
87
|
+
def fail_unless_expected_response(response, *allowed_responses)
|
88
|
+
unless allowed_responses.any? { |allowed| response.is_a?(allowed) }
|
89
|
+
logger.error "#{response.inspect}: #{response.body}"
|
90
|
+
raise UnexpectedHttpResponse, response
|
91
|
+
end
|
92
|
+
response
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# https://developers.facebook.com/docs/commerce-platform/order-management/order-api
|
97
|
+
class OrderApi < Api
|
98
|
+
# By default the list_orders method will return orders in the CREATED state.
|
99
|
+
# @option params updated_before [String] Unix timestamp
|
100
|
+
# @option params updated_after [String] Unix timestamp
|
101
|
+
# state The state of the order. The default is CREATED. FB_PROCESSING, IN_PROGRESS, COMPLETED
|
102
|
+
def list_orders(params = {})
|
103
|
+
get("#{cms_id}/commerce_orders", params).fetch('data')
|
104
|
+
end
|
105
|
+
|
106
|
+
# @param order_id [String]
|
107
|
+
# @option params fields [String] Comma-separated list of fields to include in the response
|
108
|
+
def get_order_details(order_id, params = {})
|
109
|
+
get(order_id, params)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# https://developers.facebook.com/docs/commerce-platform/order-management/acknowledgement-api
|
114
|
+
class AcknowledgementApi < Api
|
115
|
+
def associate_app
|
116
|
+
post("#{cms_id}/order_management_apps")
|
117
|
+
end
|
118
|
+
|
119
|
+
def acknowledge_order(order_id, merchant_order_reference = nil)
|
120
|
+
data = { idempotency_key: SecureRandom.uuid }
|
121
|
+
data[:merchant_order_reference] = merchant_order_reference if merchant_order_reference
|
122
|
+
post("#{order_id}/acknowledge_order", data)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# https://developers.facebook.com/docs/commerce-platform/order-management/fulfillment-api
|
127
|
+
class FulfillmentApi < Api
|
128
|
+
# @param order_id [String] Facebook order ID
|
129
|
+
# @param items [Array<Hash>] Array of item hashes (retailer_id|product_id, quantity)
|
130
|
+
# @param tracking_info [Hash] Tracking information (carrier, tracking_number, shipping_method_name)
|
131
|
+
def attach_shipment(order_id, items, tracking_info, external_shipment_id = nil)
|
132
|
+
data = {
|
133
|
+
items: items.to_json,
|
134
|
+
tracking_info: tracking_info.to_json,
|
135
|
+
idempotency_key: SecureRandom.uuid
|
136
|
+
}
|
137
|
+
data[:external_shipment_id] = external_shipment_id if external_shipment_id
|
138
|
+
|
139
|
+
post("#{order_id}/shipments", data)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
class CancellationRefundApi < Api
|
144
|
+
# @param order_id [String] Facebook order ID
|
145
|
+
# @return [Hash] Cancellation response, eg. { success: true}
|
146
|
+
def cancel_order(order_id)
|
147
|
+
data = { idempotency_key: SecureRandom.uuid }
|
148
|
+
post("#{order_id}/cancellations", data)
|
149
|
+
end
|
150
|
+
|
151
|
+
# @param order_id [String] Facebook order ID
|
152
|
+
# @param reason_code [String] Reason code for the refund, eg. 'REFUND_REASON_OTHER'
|
153
|
+
# @param items [Array<Hash>] Item hashes (retailer_id|product_id, quantity), required if partial refund
|
154
|
+
# @return [Hash] Refund response, eg. { success: true}
|
155
|
+
def refund_order(order_id, reason_code, items = nil)
|
156
|
+
data = { reason_code: reason_code, idempotency_key: SecureRandom.uuid }
|
157
|
+
post("#{order_id}/refunds", data)
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
class ReturnApi < Api
|
162
|
+
# @param order_id [String] Facebook order ID
|
163
|
+
# @param items [Array<Hash>] Array of item hashes (item_id|retailer_id, quantity, reason)
|
164
|
+
# @param return_status [String] Reason code for the return,
|
165
|
+
# eg. 'REQUESTED', 'APPROVED', 'DISAPPROVED', 'REFUNDED', 'MERCHANT_MARKED_COMPLETED'
|
166
|
+
# @return [Hash] Return response, eg. { id: '1234567890' }
|
167
|
+
def create_return(order_id, items, return_status, return_message, merchant_return_id)
|
168
|
+
data = {
|
169
|
+
items: items.to_json,
|
170
|
+
return_status: return_status,
|
171
|
+
return_message: return_message,
|
172
|
+
merchant_return_id: merchant_return_id
|
173
|
+
}
|
174
|
+
post("#{order_id}/returns", data)
|
175
|
+
end
|
176
|
+
|
177
|
+
# @param return_id [String] Facebook return ID
|
178
|
+
# @param update_event [String] Reason code for the return, 'ACCEPT_RETURN' or 'CLOSE_RETURN'
|
179
|
+
# @option options [String] notes Notes for the return
|
180
|
+
# @option options [String] merchant_return_id Merchant return ID
|
181
|
+
# @option options [Array<Hash>] return_shipping_labels Array of shipping label hashes
|
182
|
+
# (carrier, service_name, tracking_number, file_handle, cost)
|
183
|
+
def update_return(return_id, update_event, options = {})
|
184
|
+
post("#{return_id}/update_return", options.merge(update_event: update_event))
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: facebook_commerce
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Piers Chambers
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-08-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
- piers@varyonic.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".rspec"
|
21
|
+
- CHANGELOG.md
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/facebook_commerce.rb
|
27
|
+
- lib/facebook_commerce/version.rb
|
28
|
+
- sig/facebook_commerce.rbs
|
29
|
+
homepage: https://github.com/varyonic/facebook_commerce
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata:
|
33
|
+
homepage_uri: https://github.com/varyonic/facebook_commerce
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 2.6.0
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubygems_version: 3.3.26
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: Unofficial Ruby wrapper for the Facebook Commerce API.
|
53
|
+
test_files: []
|