Class: Workarea::Paypal::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/workarea/paypal/gateway.rb

Defined Under Namespace

Classes: RequestError

Instance Method Summary collapse

Instance Method Details

#capture(order_id) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/workarea/paypal/gateway.rb', line 60

def capture(order_id)
  request = PayPalCheckoutSdk::Orders::OrdersCaptureRequest.new(order_id)
  request.prefer("return=representation")

  if ENV['PAYPAL_MOCK_RESPONSE'].present? && Rails.env.in?(%w(test development))
    request.headers['PayPal-Mock-Response'] = ENV['PAYPAL_MOCK_RESPONSE']
  end

  handle_transaction_errors do
    response = send_request(request)
    result = response.result
    capture = result&.purchase_units&.first&.payments&.captures&.last
    success = response.status_code == 201 && capture&.status != 'DECLINED'

    ActiveMerchant::Billing::Response.new(
      success,
      "PayPal capture #{success ? 'succeeded' : 'failed'}",
      Paypal.transform_values(success ? capture : result)
    )
  end
end

#clientObject



14
15
16
# File 'lib/workarea/paypal/gateway.rb', line 14

def client
  PayPal::PayPalHttpClient.new(environment)
end

#client_idObject



150
151
152
153
154
# File 'lib/workarea/paypal/gateway.rb', line 150

def client_id
  ENV['WORKAREA_PAYPAL_CLIENT_ID'].presence ||
    Rails.application.credentials.paypal.try(:[], :client_id) ||
    Workarea.config.paypal_client_id
end

#configured?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/workarea/paypal/gateway.rb', line 146

def configured?
  client_id.present? && client_secret.present?
end

#create_order(body:) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/workarea/paypal/gateway.rb', line 40

def create_order(body:)
  request = PayPalCheckoutSdk::Orders::OrdersCreateRequest.new
  request.request_body(body)

  handle_connection_errors do
    response = send_request(request)
    response.result
  end
end

#create_webhook(url:, event_types:) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/workarea/paypal/gateway.rb', line 117

def create_webhook(url:, event_types:)
  request = Workarea::Paypal::Requests::CreateWebhook.new
  request.request_body(
    url: url,
    event_types: Array.wrap(event_types).map { |type| { name: type } }
  )

  response = handle_connection_errors { send_request(request) }

  throw_request_error(response) unless response.status_code == 201
  response
end

#delete_webhook(webhook_id) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/workarea/paypal/gateway.rb', line 130

def delete_webhook(webhook_id)
  request = Workarea::Paypal::Requests::DeleteWebhook.new(webhook_id)
  response = handle_connection_errors { send_request(request) }

  throw_request_error(response) unless response.status_code == 204
  response
end

#environmentObject



6
7
8
9
10
11
12
# File 'lib/workarea/paypal/gateway.rb', line 6

def environment
  @environment =
    Workarea.config.paypal_environment.constantize.new(
      client_id,
      client_secret
    )
end

#generate_token(user: nil) ⇒ Object

This gets a token required to render hosted fields. Not used for smart payment buttons.



27
28
29
30
31
32
33
# File 'lib/workarea/paypal/gateway.rb', line 27

def generate_token(user: nil)
  request = Workarea::Paypal::Requests::GenerateToken.new
  id = user&.id.to_s.last(22) # length limit
  request.request_body(customer_id: id) if user.present?

  handle_connection_errors { send_request(request) }
end

#get_order(order_id) ⇒ Object



35
36
37
38
# File 'lib/workarea/paypal/gateway.rb', line 35

def get_order(order_id)
  request = PayPalCheckoutSdk::Orders::OrdersGetRequest.new(order_id)
  handle_connection_errors { send_request(request) }
end

#list_webhooksObject



138
139
140
141
142
143
144
# File 'lib/workarea/paypal/gateway.rb', line 138

def list_webhooks
  request = Workarea::Paypal::Requests::ListWebhooks.new
  response = handle_connection_errors { send_request(request) }

  throw_request_error(response) unless response.status_code == 200
  response
end

#refund(capture_id, amount: nil) ⇒ Object

No body means refunding the entire captured amount, otherwise an amount object needs to be supplied.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/workarea/paypal/gateway.rb', line 85

def refund(capture_id, amount: nil)
  request = PayPalCheckoutSdk::Payments::CapturesRefundRequest.new(capture_id)
  request.prefer("return=representation")

  if amount.present?
    request.request_body(
      amount: {
        value: amount.to_s,
        currency_code: amount.currency.iso_code
      }
    )
  end

  handle_transaction_errors do
    response = send_request(request)
    refund = response.result
    success = response.status_code == 201 && refund.status != 'CANCELLED'

    ActiveMerchant::Billing::Response.new(
      success,
      "PayPal refund #{success ? 'succeeded' : 'failed'}",
      {
        id: refund.id,
        status: refund.status,
        status_details: refund.status_details.to_h,
        capture_id: capture_id,
        amount: amount.to_s
      }
    )
  end
end

#send_request(request) ⇒ Object



18
19
20
21
22
23
# File 'lib/workarea/paypal/gateway.rb', line 18

def send_request(request)
  # Do not change this
  request.headers["PayPal-Partner-Attribution-Id"] = 'Workarea_SP'

  client.execute(request)
end

#update_order(order_id, body: {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/workarea/paypal/gateway.rb', line 50

def update_order(order_id, body: {})
  request = PayPalCheckoutSdk::Orders::OrdersPatchRequest.new(order_id)
  request.request_body(body)

  handle_connection_errors do
    response = send_request(request)
    response.result
  end
end