spree_paypal_api_checkout 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 53c335900fbbe782e6fc7cfa78b36f258b514a9d9b9400cef65f7fb8c7cf69c7
|
4
|
+
data.tar.gz: 946f4fa867ded14d6c7f494df8d8e21377721b682df09fe7cffd312540268540
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d1754cdff574bb9bbe6b798663c02686228ec3a0e7fe30281b87dea2264245d2f296e849ad44145282305766678b5956834a8a44f0ed17c38e6a644abeb0719
|
7
|
+
data.tar.gz: aef76e65dae6694919ddc17f3c290de2a49be957ac913a9a0ba4863bfe0f1a576d7a10c7e7fcce1a658e087c1b48786514f21c329354094024a124b961986801
|
data/README.md
CHANGED
@@ -8,25 +8,26 @@ module Spree
|
|
8
8
|
additional_adjustments = order.all_adjustments.additional
|
9
9
|
tax_adjustments = additional_adjustments.tax
|
10
10
|
shipping_adjustments = additional_adjustments.shipping
|
11
|
+
promotion_adjustments = additional_adjustments.promotion
|
11
12
|
|
12
13
|
additional_adjustments.eligible.each do |adjustment|
|
13
14
|
# Because PayPal doesn't accept $0 items at all. See #10
|
14
15
|
# https://cms.paypal.com/uk/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_ECCustomizing
|
15
16
|
# "It can be a positive or negative value but not zero."
|
16
17
|
next if adjustment.amount.zero?
|
17
|
-
next if tax_adjustments.include?(adjustment) || shipping_adjustments.include?(adjustment)
|
18
|
+
next if tax_adjustments.include?(adjustment) || shipping_adjustments.include?(adjustment) || promotion_adjustments.include?(adjustment)
|
18
19
|
|
19
20
|
items << {
|
20
21
|
name: adjustment.label,
|
21
22
|
quantity: 1,
|
22
|
-
|
23
|
+
unit_amount: {
|
23
24
|
currency_code: order.currency,
|
24
25
|
value: adjustment.amount
|
25
26
|
}
|
26
27
|
}
|
27
28
|
end
|
28
29
|
|
29
|
-
pp_response = provider.create_order(order, express_checkout_request_details(order, items))
|
30
|
+
pp_response = provider.create_order(order, express_checkout_request_details(order: order, items: items, tax_adjustments: tax_adjustments, promotion_adjustments: promotion_adjustments))
|
30
31
|
|
31
32
|
render json: pp_response
|
32
33
|
end
|
@@ -48,15 +49,16 @@ module Spree
|
|
48
49
|
def proceed
|
49
50
|
order = current_order || raise(ActiveRecord::RecordNotFound)
|
50
51
|
order.payments.last.source.update(transaction_id: params[:transaction_id])
|
52
|
+
order.payments.last.update(response_code: params[:transaction_id])
|
51
53
|
order.next
|
54
|
+
path = checkout_state_path(order.state)
|
52
55
|
if order.complete?
|
53
56
|
flash.notice = Spree.t(:order_processed_successfully)
|
54
57
|
flash[:order_completed] = true
|
55
58
|
session[:order_id] = nil
|
56
|
-
|
57
|
-
else
|
58
|
-
redirect_to checkout_state_path(order.state)
|
59
|
+
path = completion_route(order)
|
59
60
|
end
|
61
|
+
render json: { redirect_to: path }
|
60
62
|
end
|
61
63
|
|
62
64
|
def cancel
|
@@ -70,31 +72,68 @@ module Spree
|
|
70
72
|
def line_item(item)
|
71
73
|
{
|
72
74
|
name: item.product.name,
|
73
|
-
|
75
|
+
sku: item.variant.sku,
|
74
76
|
quantity: item.quantity,
|
75
|
-
|
77
|
+
description: item.product.meta_description,
|
78
|
+
unit_amount: {
|
76
79
|
currency_code: item.order.currency,
|
77
80
|
value: item.price
|
78
81
|
},
|
79
|
-
|
82
|
+
item_category: "Physical"
|
80
83
|
}
|
81
84
|
end
|
82
85
|
|
83
|
-
def express_checkout_request_details order
|
86
|
+
def express_checkout_request_details order:, items:, tax_adjustments:, promotion_adjustments:
|
84
87
|
{
|
85
88
|
intent: 'CAPTURE',
|
86
89
|
purchase_units: [
|
87
90
|
{
|
88
91
|
amount: {
|
89
92
|
currency_code: current_order.currency,
|
90
|
-
value: order.total
|
93
|
+
value: order.total,
|
94
|
+
breakdown: {
|
95
|
+
item_total: {
|
96
|
+
currency_code: current_order.currency,
|
97
|
+
value: items.sum{|r| (r[:unit_amount][:value] * r[:quantity]) }
|
98
|
+
},
|
99
|
+
shipping: {
|
100
|
+
currency_code: current_order.currency,
|
101
|
+
value: current_order.shipments.sum(:cost)
|
102
|
+
},
|
103
|
+
tax_total: {
|
104
|
+
currency_code: current_order.currency,
|
105
|
+
value: tax_adjustments.sum(:amount)
|
106
|
+
},
|
107
|
+
discount: {
|
108
|
+
currency_code: current_order.currency,
|
109
|
+
value: promotion_adjustments.sum(:amount).abs
|
110
|
+
}
|
111
|
+
}
|
91
112
|
},
|
92
|
-
|
113
|
+
items: items,
|
114
|
+
shipping: address_options
|
93
115
|
},
|
94
116
|
]
|
95
117
|
}
|
96
118
|
end
|
97
119
|
|
120
|
+
def address_options
|
121
|
+
address = current_order.ship_address
|
122
|
+
{
|
123
|
+
name: { full_name: address.try(:full_name) },
|
124
|
+
address: {
|
125
|
+
address_line_1: address.address1,
|
126
|
+
address_line_2: address.address2,
|
127
|
+
# phone: address.phone,
|
128
|
+
admin_area_1: address.state_text,
|
129
|
+
admin_area_2: address.city,
|
130
|
+
country_code: address.country.iso,
|
131
|
+
postal_code: address.zipcode
|
132
|
+
},
|
133
|
+
type: 'SHIPPING'
|
134
|
+
}
|
135
|
+
end
|
136
|
+
|
98
137
|
def payment_method
|
99
138
|
Spree::PaymentMethod.find(params[:payment_method_id])
|
100
139
|
end
|
@@ -20,28 +20,14 @@ module Spree
|
|
20
20
|
'paypal_checkout'
|
21
21
|
end
|
22
22
|
|
23
|
-
def generate_client_token
|
24
|
-
uri = URI.parse("https://#{preferred_server}/v1/identity/generate-token")
|
25
|
-
request = Net::HTTP::Post.new(uri)
|
26
|
-
request['Authorization'] = "Bearer #{generate_access_token}"
|
27
|
-
request.content_type = "application/json"
|
28
|
-
|
29
|
-
req_options = { use_ssl: uri.scheme == "https" }
|
30
|
-
|
31
|
-
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
32
|
-
response = http.request(request)
|
33
|
-
end
|
34
|
-
return response
|
35
|
-
end
|
36
|
-
|
37
23
|
def generate_access_token
|
38
24
|
uri = URI.parse("https://#{preferred_server}/v1/oauth2/token")
|
39
25
|
request = Net::HTTP::Post.new(uri)
|
40
26
|
request.basic_auth("#{preferred_api_key}", "#{preferred_secret_key}")
|
41
|
-
request.content_type =
|
42
|
-
request.body =
|
27
|
+
request.content_type = 'application/json'
|
28
|
+
request.body = 'grant_type=client_credentials'
|
43
29
|
|
44
|
-
req_options = { use_ssl: uri.scheme ==
|
30
|
+
req_options = { use_ssl: uri.scheme == 'https' }
|
45
31
|
|
46
32
|
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
47
33
|
response = http.request(request)
|
@@ -49,46 +35,24 @@ module Spree
|
|
49
35
|
return JSON.parse(response.read_body)['access_token']
|
50
36
|
end
|
51
37
|
|
38
|
+
def generate_client_token
|
39
|
+
uri = URI.parse("https://#{preferred_server}/v1/identity/generate-token")
|
40
|
+
return post_response(uri)
|
41
|
+
end
|
42
|
+
|
52
43
|
def create_order order, body
|
53
44
|
uri = URI.parse("https://#{preferred_server}/v2/checkout/orders")
|
54
|
-
|
55
|
-
request['Authorization'] = "Bearer #{generate_access_token}"
|
56
|
-
request.content_type = "application/json"
|
57
|
-
|
58
|
-
request.body = body.to_json
|
59
|
-
req_options = { use_ssl: uri.scheme == "https" }
|
60
|
-
|
61
|
-
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
62
|
-
response = http.request(request)
|
63
|
-
end
|
64
|
-
return JSON.parse(response.read_body)
|
45
|
+
return post_response(uri, body)
|
65
46
|
end
|
66
47
|
|
67
48
|
def capture_payment order_id
|
68
49
|
uri = URI.parse("https://#{preferred_server}/v2/checkout/orders/#{order_id}/capture")
|
69
|
-
|
70
|
-
request['Authorization'] = "Bearer #{generate_access_token}"
|
71
|
-
request.content_type = "application/json"
|
72
|
-
req_options = { use_ssl: uri.scheme == "https" }
|
73
|
-
|
74
|
-
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
75
|
-
response = http.request(request)
|
76
|
-
end
|
77
|
-
return JSON.parse(response.read_body)
|
50
|
+
return post_response(uri)
|
78
51
|
end
|
79
52
|
|
80
53
|
def refund_payment id, body
|
81
54
|
uri = URI.parse("https://#{preferred_server}/v2/payments/captures/#{id}/refund")
|
82
|
-
|
83
|
-
request['Authorization'] = "Bearer #{generate_access_token}"
|
84
|
-
request.body = body.to_json
|
85
|
-
request.content_type = "application/json"
|
86
|
-
req_options = { use_ssl: uri.scheme == "https" }
|
87
|
-
|
88
|
-
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
89
|
-
response = http.request(request)
|
90
|
-
end
|
91
|
-
return JSON.parse(response.read_body)
|
55
|
+
return post_response(uri, body)
|
92
56
|
end
|
93
57
|
|
94
58
|
def purchase(amount, express_checkout, gateway_options={})
|
@@ -134,6 +98,19 @@ module Spree
|
|
134
98
|
end
|
135
99
|
refund_transaction_response
|
136
100
|
end
|
101
|
+
|
102
|
+
def post_response uri, body={}
|
103
|
+
request = Net::HTTP::Post.new(uri)
|
104
|
+
request['Authorization'] = "Bearer #{generate_access_token}"
|
105
|
+
request.content_type = 'application/json'
|
106
|
+
req_options = { use_ssl: uri.scheme == 'https' }
|
107
|
+
|
108
|
+
request.body = body.to_json if body.present?
|
109
|
+
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
|
110
|
+
response = http.request(request)
|
111
|
+
end
|
112
|
+
return JSON.parse(response.read_body)
|
113
|
+
end
|
137
114
|
end
|
138
115
|
end
|
139
116
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "spree_paypal_api_checkout".freeze
|
3
|
-
s.version = "0.0
|
3
|
+
s.version = "0.1.0"
|
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]
|