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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 53c335900fbbe782e6fc7cfa78b36f258b514a9d9b9400cef65f7fb8c7cf69c7
4
- data.tar.gz: 946f4fa867ded14d6c7f494df8d8e21377721b682df09fe7cffd312540268540
3
+ metadata.gz: bd7531190eafee3bb5fe40ac3378269b2e9265e4c66773cf2a447f0c1cef757f
4
+ data.tar.gz: b62014828506b097f6287cfa41ffde2e4638f29e5be241f9746aa28ea23e4e24
5
5
  SHA512:
6
- metadata.gz: 0d1754cdff574bb9bbe6b798663c02686228ec3a0e7fe30281b87dea2264245d2f296e849ad44145282305766678b5956834a8a44f0ed17c38e6a644abeb0719
7
- data.tar.gz: aef76e65dae6694919ddc17f3c290de2a49be957ac913a9a0ba4863bfe0f1a576d7a10c7e7fcce1a658e087c1b48786514f21c329354094024a124b961986801
6
+ metadata.gz: beaf4e7e206f97393da179553fe15e6548734fffcb62dce78b3b6bdb6dc79d344b83bc8f332900525e586d0575fc0b367cfb21e99781c995d5ec14f07496546d
7
+ data.tar.gz: da8a449dd52eeb1d1c864b65db9f1bc84e5be991316d7578f90657f1a3eaba7bb384ca9fd21a48165fc997b18b8a09c8fad748e6be4b2b864069d078e3240c2d
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  1. Add this extension to your Gemfile with this line:
6
6
 
7
- gem 'spree_paypal_api_checkout', '~> 0.1.0'
7
+ gem 'spree_paypal_api_checkout', '~> 0.1.1'
8
8
 
9
9
  2. Install the gem using Bundler:
10
10
 
@@ -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
- pp_response = provider.create_order(order, express_checkout_request_details(order: order, items: items, tax_adjustments: tax_adjustments, promotion_adjustments: promotion_adjustments))
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
- response = provider.capture_payment(params[:number])
38
- order.payments.create!({
39
- source: Spree::PaypalApiCheckout.create({
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
- uri = URI.parse("https://#{preferred_server}/v1/oauth2/token")
25
- request = Net::HTTP::Post.new(uri)
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
- uri = URI.parse("https://#{preferred_server}/v1/identity/generate-token")
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
- uri = URI.parse("https://#{preferred_server}/v2/checkout/orders")
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
- uri = URI.parse("https://#{preferred_server}/v2/checkout/orders/#{order_id}/capture")
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
- uri = URI.parse("https://#{preferred_server}/v2/payments/captures/#{id}/refund")
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
- refund_transaction_response = refund_payment(payment.source.transaction_id, refund_transaction)
82
- if refund_transaction_response['status']=='COMPLETED'
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
- return JSON.parse(response.read_body)
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="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
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 %>&currency=<%= @order.currency %>"></script>
4
4
 
5
5
  <script type="text/javascript">
@@ -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."
@@ -15,6 +15,7 @@ en:
15
15
  refund_unsuccessful: "PayPal refund unsuccessful"
16
16
  actions:
17
17
  refund: "Refund"
18
+ status: Status
18
19
  flash:
19
20
  cancel: "Don't want to use PayPal? No problems."
20
21
  connection_failed: "Could not connect to PayPal."
@@ -15,6 +15,7 @@ es:
15
15
  refund_unsuccessful: "No se pudo rembolsar el pago de PayPal"
16
16
  actions:
17
17
  refund: "Reembolso"
18
+ status: Estado
18
19
  flash:
19
20
  cancel: "No quieres usar PayPal? No hay problema."
20
21
  connection_failed: "No se pudo conectar con PayPal."
@@ -15,6 +15,7 @@ it:
15
15
  refund_unsuccessful: "Rimborso PayPal non completato"
16
16
  actions:
17
17
  refund: "Rimborso"
18
+ status: Stato
18
19
  flash:
19
20
  cancel: "Non vuoi usare PayPal? Nessun problema."
20
21
  connection_failed: "Impossibile connettersi a PayPal."
@@ -15,6 +15,7 @@ pl:
15
15
  refund_unsuccessful: "Błąd podczas wykonywania zwrotu PayPal"
16
16
  actions:
17
17
  refund: "Zwróć Płatność"
18
+ status: Status
18
19
  flash:
19
20
  cancel: "Nie chcesz użyć PayPal? Nie ma problemu."
20
21
  connection_failed: "Nie można było połączyć się z usługą PayPal."
@@ -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,3 +1,3 @@
1
1
  module SpreePaypalApiCheckout
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -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.0"
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]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spree_paypal_api_checkout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Spree Commerce