Last active
July 10, 2019 08:35
-
-
Save fflow-io/25dd38f061ff8c995d73e35048ade46e to your computer and use it in GitHub Desktop.
Rspec stubs for goCardless API integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module AddressLookupHelpers | |
def stub_gc_create_subscription_url(amount, subscription_id, day) | |
response_body = { subscriptions: subscription_response_body }.to_json | |
stub_request(:post, "https://api.gocardless.com/subscriptions") | |
.with(body: { subscriptions: subscription_params(amount, subscription_id, day) }) | |
.to_return(status: 200, body: response_body, headers: json_headers) | |
end | |
def stub_gc_cancel_subscription_url(subscription_id) | |
response_body = { subscriptions: cancel_subscription_response_body }.to_json | |
stub_request(:post, "https://api.gocardless.com/subscriptions/#{subscription_id}/actions/cancel") | |
.with(body: { data: {} }) | |
.to_return(status: 200, body: response_body, headers: json_headers) | |
end | |
def stub_gc_create_payment_url(amount, invoice_number) | |
response_body = { payments: payment_response_body }.to_json | |
stub_request(:post, "https://api.gocardless.com/payments") | |
.with(body: { payments: payment_params(amount, invoice_number) }) | |
.to_return(status: 200, body: response_body, headers: json_headers) | |
end | |
def stub_gc_completion_url | |
response_body = { redirect_flows: completion_response_body }.to_json | |
stub_request(:post, "https://api.gocardless.com/redirect_flows/mandate_id/actions/complete") | |
.with(body: {}) | |
.to_return(status: 200, body: response_body, headers: json_headers) | |
end | |
def stub_gc_redirect_url(user, redirect_url) | |
response_body = { redirect_flows: redirect_response_body }.to_json | |
stub_request(:post, "https://api.gocardless.com/redirect_flows") | |
.with(body: { redirect_flows: body_params(user, redirect_url) }) | |
.to_return(status: 200, body: response_body, headers: json_headers) | |
end | |
def stub_gc_cancel_mandate_url(mandate_id) | |
response_body = { mandates: cancel_mandate_response_body }.to_json | |
stub_request(:post, "https://api.gocardless.com/mandates/#{mandate_id}/actions/cancel") | |
.with(body: { data: {} }) | |
.to_return(status: 200, body: response_body, headers: json_headers) | |
end | |
def stub_gc_cancel_mandate_fails_url(mandate_id) | |
response_body = { error: cancel_mandate_fails_body }.to_json | |
stub_request(:post, "https://api.gocardless.com/mandates/#{mandate_id}/actions/cancel") | |
.with(body: { data: {} }) | |
.to_return(status: 404, body: response_body, headers: json_headers) | |
end | |
def json_headers | |
{ 'Content-Type' => 'application/json' } | |
end | |
def payment_response_body | |
{ | |
id: "PM0002", | |
status: "confirmed", | |
created_at: Time.zone.now | |
} | |
end | |
def subscription_response_body | |
{ | |
id: "SB0001", | |
status: "subscription_created", | |
created_at: Time.zone.now | |
} | |
end | |
def cancel_subscription_response_body | |
{ | |
status: "subscription_cancelled", | |
created_at: Time.zone.now | |
} | |
end | |
def cancel_mandate_response_body | |
{ | |
status: "mandate_cancelled", | |
created_at: Time.zone.now | |
} | |
end | |
def cancel_mandate_fails_body | |
{ | |
type: 'invalid_state', | |
code: 404, | |
errors: [ | |
{ | |
message: 'A resource could not be found', | |
reason: 'resource_not_found', | |
links: { | |
} | |
} | |
] | |
} | |
end | |
def completion_response_body | |
{ | |
links: { | |
mandate: "mandate_id" | |
}, | |
confirmation_url: "http://confimation.url" | |
} | |
end | |
def redirect_response_body | |
{ | |
confirmation_url: 'confirmation_url-input', | |
created_at: 'created_at-input', | |
description: 'description-input', | |
id: 'id-input', | |
links: 'links-input', | |
redirect_url: "https://pay.gocardless.com/flow/RE-Dummy", | |
scheme: 'scheme-input', | |
session_token: nil, | |
success_redirect_url: "https://pay.gocardless.com/flow/RE-Dummy" | |
} | |
end | |
def payment_params(amount, invoice_number) | |
{ | |
amount: amount, | |
currency: "GBP", | |
links: { | |
mandate: nil | |
}, | |
metadata: { | |
invoice_number: invoice_number | |
} | |
} | |
end | |
def subscription_params(amount, subscription_id, day) | |
{ | |
amount: amount, | |
currency: "GBP", | |
interval_unit: 'monthly', | |
day_of_month: day, | |
links: { | |
mandate: nil | |
} | |
} | |
end | |
def body_params(user, redirect_url) | |
{ | |
description: "fflow", | |
session_token: nil, | |
success_redirect_url: redirect_url, | |
prefilled_customer: { | |
given_name: user.first_name, | |
family_name: user.last_name, | |
email: user.email | |
} | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment