Class: Gateway::PayPalCheckout

Inherits:
Gateway
  • Object
show all
Defined in:
app/models/spree/gateway/pay_pal_checkout.rb

Instance Method Summary collapse

Instance Method Details

#api_url(url) ⇒ Object



121
122
123
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 121

def api_url url
  URI.parse("https://#{preferred_server}/#{url}")
end

#auto_capture?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 15

def auto_capture?
  true
end

#capture_payment(order_id) ⇒ Object



36
37
38
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 36

def capture_payment order_id
  post_response(api_url("v2/checkout/orders/#{order_id}/capture"))
end

#create_order(order, body) ⇒ Object



32
33
34
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 32

def create_order order, body
  post_response(api_url('v2/checkout/orders'), body)
end

#generate_access_tokenObject



23
24
25
26
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 23

def generate_access_token
  response = post_response_without_token(api_url('v1/oauth2/token'))
  return response['access_token']
end

#generate_client_tokenObject



28
29
30
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 28

def generate_client_token
  post_response(api_url('v1/identity/generate-token'))
end

#hit_api(request:, body:, uri:) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 107

def hit_api request:, body:, uri:
  request.content_type = 'application/json'
  req_options = { use_ssl: uri.scheme == 'https' }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    response = http.request(request)
  end
  response
end

#json_error(raw_response) ⇒ Object



160
161
162
163
164
165
166
167
168
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 160

def json_error(raw_response)
  msg = 'Invalid response. Please contact team if you continue to receive this message.'
  msg += "  (The raw response returned by the API was #{raw_response.inspect})"
  {
    "error" => {
      "message" => msg
    }
  }
end

#method_typeObject



19
20
21
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 19

def method_type
  'paypal_checkout'
end

#parse_response(response) ⇒ Object



117
118
119
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 117

def parse_response response
  JSON.parse(response.read_body) rescue response
end

#post_response(uri, body = {}) ⇒ Object



91
92
93
94
95
96
97
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 91

def post_response uri, body={}
  request = Net::HTTP::Post.new(uri)
  request['Authorization'] = "Bearer #{generate_access_token}"
  request.body = body.to_json if body.present?

  return hit_api(request: request, body: body, uri: uri)
end

#post_response_without_token(uri, body = {}) ⇒ Object



99
100
101
102
103
104
105
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 99

def post_response_without_token uri, body={}
  request = Net::HTTP::Post.new(uri)
  request.basic_auth("#{preferred_api_key}", "#{preferred_secret_key}")
  request.body = 'grant_type=client_credentials'

  return parse_response(hit_api(request: request, body: body, uri: uri))
end

#providerObject



13
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 13

def provider;end

#provider_classObject



11
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 11

def provider_class;end

#purchase(amount, express_checkout, gateway_options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 44

def purchase(amount, express_checkout, gateway_options={})
  if express_checkout.success?
    Class.new do
      def success?; true; end
      def authorization; nil; end
    end.new
  else
    'No transaction'
  end
end

#refund(payment, amount) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 55

def refund(payment, amount)
  refund_type = payment.amount == amount.to_f ? "Full" : "Partial"
  refund_transaction = {
    transaction_id: payment.source.transaction_id,
    amount: {
      value: payment.order.item_total,
      currency_code: payment.currency
    },
    invoice_id: payment.order.number,
    refund_type: refund_type,
    refund_source: 'any'
  }
  refund_entry = refund_payment(payment.source.transaction_id, refund_transaction)
  refund_transaction_response = parse_response(refund_entry)

  if success_response?(refund_transaction_response)
    payment.source.update({
      :refunded_at => Time.now,
      :refund_transaction_id => refund_transaction_response['id'],
      :state => "refunded",
      :refund_type => refund_type
    })

    refund_payment = payment.class.create!(
      :order => payment.order,
      :source => payment,
      :payment_method => payment.payment_method,
      :amount => amount.to_f.abs * -1,
      :response_code => refund_transaction_response['id'],
      :state => 'completed'
    )
    refund_payment.log_entries.create!(details: refund_entry.to_yaml)
  end
  refund_transaction_response
end

#refund_payment(id, body) ⇒ Object



40
41
42
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 40

def refund_payment id, body
  post_response(api_url("v2/payments/captures/#{id}/refund"), body)
end

#response_error(raw_response) ⇒ Object



152
153
154
155
156
157
158
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 152

def response_error(raw_response)
  begin
    parse(raw_response)
  rescue JSON::ParserError
    json_error(raw_response)
  end
end

#set_payment_records(order, number, payment_method) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 125

def set_payment_records order, number, payment_method
  raw_response = response = nil
  success = false
  begin
    payment_entry = capture_payment(number)
    response = parse_response(payment_entry)
    success = success_response?(response)

    payment = order.payments.create!({
      source: Spree::PaypalApiCheckout.create({
        token: response['id'],
        payer_id: response['payer']['payer_id']
      }),
      amount: order.total,
      payment_method: payment_method
    })
  rescue Exception => e
    raw_response = e.response.body
    response = response_error(raw_response)
  rescue JSON::ParserError
    response = json_error(raw_response)
  end

  payment.log_entries.create!(details: payment_entry.to_yaml)
  response
end

#success_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 170

def success_response? response
  response.key?('status') && response.key?('id') && (response['status'] == 'COMPLETED')
end

#supports?(source) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'app/models/spree/gateway/pay_pal_checkout.rb', line 7

def supports?(source)
  true
end