Skip to content

Commit

Permalink
Generalize PaypalInterface module calls
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorbg committed Apr 19, 2024
1 parent 784f5c6 commit bfb8989
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
38 changes: 28 additions & 10 deletions app/controllers/registrations_controller.rb
Expand Up @@ -749,18 +749,27 @@ def create_paypal_order
registration = registration_from_params
amount = params.require(:amount)

render json: PaypalInterface.create_order(registration, amount)
competition = registration.competition
paypal_integration = competition.payment_account_for(:paypal)

render json: PaypalInterface.create_order(
paypal_integration.paypal_merchant_id,
amount,
competition.currency_code,
)
end

def capture_paypal_payment
return head :forbidden if PaypalInterface.paypal_disabled?

registration = registration_from_params

competition = registration.competition
paypal_integration = competition.payment_account_for(:paypal)

order_id = params.require(:orderID)

response = PaypalInterface.capture_payment(competition, order_id)
response = PaypalInterface.capture_payment(paypal_integration.paypal_merchant_id, order_id)
if response['status'] == 'COMPLETED'

# TODO: Handle the case where there are multiple captures for a payment
Expand Down Expand Up @@ -790,26 +799,35 @@ def capture_paypal_payment
amount,
currency_code,
record, # TODO: Add error handling for the PaypalRecord not being found
registration.user.id,
current_user.id,
)
end

render json: response
end

def refund_paypal_payment
registration = Registration.find(params[:id])
paypal_order = RegistrationPayment.find(params[:payment_id]).receipt
registration = registration_from_params
paypal_integration = registration.competition.payment_account_for(:paypal)

registration_payment = RegistrationPayment.find(params[:payment_id])
paypal_order = registration_payment.receipt

payment_capture_id = paypal_order.capture_id

refund = PaypalInterface.issue_refund(registration, payment_capture_id)
refund = PaypalInterface.issue_refund(
paypal_integration.paypal_merchant_id,
payment_capture_id,
registration_payment.amount_lowest_denomination,
registration_payment.currency_code,
)

registration.record_refund(
refund.amount_in_cents,
refund.currency_code,
registration_payment.amount_lowest_denomination,
registration_payment.currency_code,
refund,
payment_capture_id,
registration.user.id,
registration_payment.id,
current_user.id,
)
end
end
37 changes: 23 additions & 14 deletions lib/paypal_interface.rb
Expand Up @@ -47,24 +47,26 @@ def self.generate_paypal_onboarding_link(competition_id)
end
end

def self.create_order(registration, outstanding_fees)
def self.create_order(merchant_id, amount_iso, currency_code)
url = "/v2/checkout/orders"

fee_currency = registration.competition.currency_code
amount = PaypalRecord.paypal_amount(outstanding_fees, fee_currency)
amount_paypal = PaypalRecord.paypal_amount(amount_iso, currency_code)

payload = {
intent: 'CAPTURE',
purchase_units: [
{
amount: { currency_code: fee_currency, value: amount },
amount: {
currency_code: currency_code,
value: amount_paypal,
},
},
],
}

response = paypal_connection.post(url) do |req|
req.headers['PayPal-Partner-Attribution-Id'] = AppSecrets.PAYPAL_ATTRIBUTION_CODE
req.headers['PayPal-Auth-Assertion'] = paypal_auth_assertion(registration.competition)
req.headers['PayPal-Auth-Assertion'] = paypal_auth_assertion(merchant_id)

req.body = payload
end
Expand All @@ -76,33 +78,40 @@ def self.create_order(registration, outstanding_fees)
record_id: body["id"],
paypal_status: body["status"],
payload: payload,
amount_in_cents: outstanding_fees,
currency_code: fee_currency,
amount_in_cents: amount_iso,
currency_code: currency_code,
)

body
end

# TODO: Update the status of the PaypalRecord object?
def self.capture_payment(competition, order_id)
def self.capture_payment(merchant_id, order_id)
url = "/v2/checkout/orders/#{order_id}/capture"

response = paypal_connection.post(url) do |req|
req.headers['PayPal-Partner-Attribution-Id'] = AppSecrets.PAYPAL_ATTRIBUTION_CODE
req.headers['PayPal-Auth-Assertion'] = paypal_auth_assertion(competition)
req.headers['PayPal-Auth-Assertion'] = paypal_auth_assertion(merchant_id)
end

response.body
end

def self.issue_refund(registration, capture_id)
def self.issue_refund(merchant_id, capture_id, amount_iso, currency_code)
url = "/v2/payments/captures/#{capture_id}/refund"

payload = {}
amount_paypal = PaypalRecord.paypal_amount(amount_iso, currency_code)

payload = {
amount: {
currency_code: currency_code,
value: amount_paypal,
},
}

response = paypal_connection.post(url) do |req|
req.headers['PayPal-Partner-Attribution-Id'] = AppSecrets.PAYPAL_ATTRIBUTION_CODE
req.headers['PayPal-Auth-Assertion'] = paypal_auth_assertion(registration.competition)
req.headers['PayPal-Auth-Assertion'] = paypal_auth_assertion(merchant_id)

req.body = payload
end
Expand Down Expand Up @@ -161,8 +170,8 @@ def self.issue_refund(registration, capture_id)
client.client_credentials.get_token.token
end

private_class_method def self.paypal_auth_assertion(competition)
payload = { "iss" => AppSecrets.PAYPAL_CLIENT_ID, "payer_id" => competition.payment_account_for(:paypal).paypal_merchant_id }
private_class_method def self.paypal_auth_assertion(merchant_id)
payload = { "iss" => AppSecrets.PAYPAL_CLIENT_ID, "payer_id" => merchant_id }
JWT.encode payload, nil, 'none'
end
end

0 comments on commit bfb8989

Please sign in to comment.