spree_paypal_api_checkout 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/app/controllers/spree/paypal_checkout_controller.rb +6 -10
- data/app/models/spree/gateway/pay_pal_checkout.rb +84 -25
- data/app/views/spree/checkout/payment/_paypal_checkout.html.erb +1 -1
- data/config/locales/de.yml +1 -0
- data/config/locales/en.yml +1 -0
- data/config/locales/es.yml +1 -0
- data/config/locales/it.yml +1 -0
- data/config/locales/pl.yml +1 -0
- data/config/locales/pt.yml +1 -0
- data/lib/spree_paypal_api_checkout/version.rb +1 -1
- data/spree_paypal_api_checkout.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bd7531190eafee3bb5fe40ac3378269b2e9265e4c66773cf2a447f0c1cef757f
|
4
|
+
data.tar.gz: b62014828506b097f6287cfa41ffde2e4638f29e5be241f9746aa28ea23e4e24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: beaf4e7e206f97393da179553fe15e6548734fffcb62dce78b3b6bdb6dc79d344b83bc8f332900525e586d0575fc0b367cfb21e99781c995d5ec14f07496546d
|
7
|
+
data.tar.gz: da8a449dd52eeb1d1c864b65db9f1bc84e5be991316d7578f90657f1a3eaba7bb384ca9fd21a48165fc997b18b8a09c8fad748e6be4b2b864069d078e3240c2d
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Spree
|
2
2
|
class PaypalCheckoutController < StoreController
|
3
3
|
skip_before_action :verify_authenticity_token
|
4
|
+
|
4
5
|
def express
|
5
6
|
order = current_order || raise(ActiveRecord::RecordNotFound)
|
6
7
|
items = order.line_items.map(&method(:line_item))
|
@@ -27,22 +28,17 @@ module Spree
|
|
27
28
|
}
|
28
29
|
end
|
29
30
|
|
30
|
-
|
31
|
+
details = express_checkout_request_details(order: order, items: items, tax_adjustments: tax_adjustments, promotion_adjustments: promotion_adjustments)
|
32
|
+
pp_response = provider.parse_response(provider.create_order(order, details))
|
31
33
|
|
32
34
|
render json: pp_response
|
33
35
|
end
|
34
36
|
|
35
37
|
def confirm
|
36
38
|
order = current_order || raise(ActiveRecord::RecordNotFound)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
token: response['id'],
|
41
|
-
payer_id: response['payer']['payer_id']
|
42
|
-
}),
|
43
|
-
amount: order.total,
|
44
|
-
payment_method: payment_method
|
45
|
-
})
|
39
|
+
|
40
|
+
response = provider.set_payment_records(order, params[:number], payment_method)
|
41
|
+
|
46
42
|
render json: response
|
47
43
|
end
|
48
44
|
|
@@ -21,38 +21,24 @@ module Spree
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def generate_access_token
|
24
|
-
|
25
|
-
|
26
|
-
request.basic_auth("#{preferred_api_key}", "#{preferred_secret_key}")
|
27
|
-
request.content_type = 'application/json'
|
28
|
-
request.body = 'grant_type=client_credentials'
|
29
|
-
|
30
|
-
req_options = { use_ssl: uri.scheme == 'https' }
|
31
|
-
|
32
|
-
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
33
|
-
response = http.request(request)
|
34
|
-
end
|
35
|
-
return JSON.parse(response.read_body)['access_token']
|
24
|
+
response = post_response_without_token(api_url('v1/oauth2/token'))
|
25
|
+
return response['access_token']
|
36
26
|
end
|
37
27
|
|
38
28
|
def generate_client_token
|
39
|
-
|
40
|
-
return post_response(uri)
|
29
|
+
post_response(api_url('v1/identity/generate-token'))
|
41
30
|
end
|
42
31
|
|
43
32
|
def create_order order, body
|
44
|
-
|
45
|
-
return post_response(uri, body)
|
33
|
+
post_response(api_url('v2/checkout/orders'), body)
|
46
34
|
end
|
47
35
|
|
48
36
|
def capture_payment order_id
|
49
|
-
|
50
|
-
return post_response(uri)
|
37
|
+
post_response(api_url("v2/checkout/orders/#{order_id}/capture"))
|
51
38
|
end
|
52
39
|
|
53
40
|
def refund_payment id, body
|
54
|
-
|
55
|
-
return post_response(uri, body)
|
41
|
+
post_response(api_url("v2/payments/captures/#{id}/refund"), body)
|
56
42
|
end
|
57
43
|
|
58
44
|
def purchase(amount, express_checkout, gateway_options={})
|
@@ -78,8 +64,10 @@ module Spree
|
|
78
64
|
refund_type: refund_type,
|
79
65
|
refund_source: 'any'
|
80
66
|
}
|
81
|
-
|
82
|
-
|
67
|
+
refund_entry = refund_payment(payment.source.transaction_id, refund_transaction)
|
68
|
+
refund_transaction_response = parse_response(refund_entry)
|
69
|
+
|
70
|
+
if success_response?(refund_transaction_response)
|
83
71
|
payment.source.update({
|
84
72
|
:refunded_at => Time.now,
|
85
73
|
:refund_transaction_id => refund_transaction_response['id'],
|
@@ -87,7 +75,7 @@ module Spree
|
|
87
75
|
:refund_type => refund_type
|
88
76
|
})
|
89
77
|
|
90
|
-
payment.class.create!(
|
78
|
+
refund_payment = payment.class.create!(
|
91
79
|
:order => payment.order,
|
92
80
|
:source => payment,
|
93
81
|
:payment_method => payment.payment_method,
|
@@ -95,6 +83,7 @@ module Spree
|
|
95
83
|
:response_code => refund_transaction_response['id'],
|
96
84
|
:state => 'completed'
|
97
85
|
)
|
86
|
+
refund_payment.log_entries.create!(details: refund_entry.to_yaml)
|
98
87
|
end
|
99
88
|
refund_transaction_response
|
100
89
|
end
|
@@ -102,14 +91,84 @@ module Spree
|
|
102
91
|
def post_response uri, body={}
|
103
92
|
request = Net::HTTP::Post.new(uri)
|
104
93
|
request['Authorization'] = "Bearer #{generate_access_token}"
|
94
|
+
request.body = body.to_json if body.present?
|
95
|
+
|
96
|
+
return hit_api(request: request, body: body, uri: uri)
|
97
|
+
end
|
98
|
+
|
99
|
+
def post_response_without_token uri, body={}
|
100
|
+
request = Net::HTTP::Post.new(uri)
|
101
|
+
request.basic_auth("#{preferred_api_key}", "#{preferred_secret_key}")
|
102
|
+
request.body = 'grant_type=client_credentials'
|
103
|
+
|
104
|
+
return parse_response(hit_api(request: request, body: body, uri: uri))
|
105
|
+
end
|
106
|
+
|
107
|
+
def hit_api request:, body:, uri:
|
105
108
|
request.content_type = 'application/json'
|
106
109
|
req_options = { use_ssl: uri.scheme == 'https' }
|
107
110
|
|
108
|
-
request.body = body.to_json if body.present?
|
109
111
|
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
110
112
|
response = http.request(request)
|
111
113
|
end
|
112
|
-
|
114
|
+
response
|
115
|
+
end
|
116
|
+
|
117
|
+
def parse_response response
|
118
|
+
JSON.parse(response.read_body) rescue response
|
119
|
+
end
|
120
|
+
|
121
|
+
def api_url url
|
122
|
+
URI.parse("https://#{preferred_server}/#{url}")
|
123
|
+
end
|
124
|
+
|
125
|
+
def set_payment_records order, number, payment_method
|
126
|
+
raw_response = response = nil
|
127
|
+
success = false
|
128
|
+
begin
|
129
|
+
payment_entry = capture_payment(number)
|
130
|
+
response = parse_response(payment_entry)
|
131
|
+
success = success_response?(response)
|
132
|
+
|
133
|
+
payment = order.payments.create!({
|
134
|
+
source: Spree::PaypalApiCheckout.create({
|
135
|
+
token: response['id'],
|
136
|
+
payer_id: response['payer']['payer_id']
|
137
|
+
}),
|
138
|
+
amount: order.total,
|
139
|
+
payment_method: payment_method
|
140
|
+
})
|
141
|
+
rescue Exception => e
|
142
|
+
raw_response = e.response.body
|
143
|
+
response = response_error(raw_response)
|
144
|
+
rescue JSON::ParserError
|
145
|
+
response = json_error(raw_response)
|
146
|
+
end
|
147
|
+
|
148
|
+
payment.log_entries.create!(details: payment_entry.to_yaml)
|
149
|
+
response
|
150
|
+
end
|
151
|
+
|
152
|
+
def response_error(raw_response)
|
153
|
+
begin
|
154
|
+
parse(raw_response)
|
155
|
+
rescue JSON::ParserError
|
156
|
+
json_error(raw_response)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def json_error(raw_response)
|
161
|
+
msg = 'Invalid response. Please contact team if you continue to receive this message.'
|
162
|
+
msg += " (The raw response returned by the API was #{raw_response.inspect})"
|
163
|
+
{
|
164
|
+
"error" => {
|
165
|
+
"message" => msg
|
166
|
+
}
|
167
|
+
}
|
168
|
+
end
|
169
|
+
|
170
|
+
def success_response? response
|
171
|
+
response.key?('status') && response.key?('id') && (response['status'] == 'COMPLETED')
|
113
172
|
end
|
114
173
|
end
|
115
174
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
<div id="paypal-button-container" class="paypal-button-container"></div>
|
2
|
-
<script src="
|
2
|
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
|
3
3
|
<script type="text/javascript" src="https://www.paypal.com/sdk/js?client-id=<%= payment_method.preferred_api_key %>¤cy=<%= @order.currency %>"></script>
|
4
4
|
|
5
5
|
<script type="text/javascript">
|
data/config/locales/de.yml
CHANGED
@@ -15,6 +15,7 @@ de:
|
|
15
15
|
refund_unsuccessful: "PayPal Erstattung nicht erfolgreich"
|
16
16
|
actions:
|
17
17
|
refund: "Erstatten"
|
18
|
+
status: 'Toestand'
|
18
19
|
flash:
|
19
20
|
cancel: "Sie wollen PayPal doch nicht benutzen? Kein Problem."
|
20
21
|
connection_failed: "Verbindung zu Paypal nicht erfolgreich."
|
data/config/locales/en.yml
CHANGED
data/config/locales/es.yml
CHANGED
data/config/locales/it.yml
CHANGED
data/config/locales/pl.yml
CHANGED
data/config/locales/pt.yml
CHANGED
@@ -15,6 +15,7 @@ pt:
|
|
15
15
|
refund_unsuccessful: "Não foi possível reembolsar o pagamento de PayPal"
|
16
16
|
actions:
|
17
17
|
refund: "Reembolso"
|
18
|
+
status: Status
|
18
19
|
flash:
|
19
20
|
cancel: "Não pretende usar PayPal? Não há problema."
|
20
21
|
connection_failed: "Não foi possível estabelecer ligação com o PayPal."
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "spree_paypal_api_checkout".freeze
|
3
|
-
s.version = "0.1.
|
3
|
+
s.version = "0.1.1"
|
4
4
|
|
5
5
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
6
6
|
s.require_paths = ["lib".freeze]
|