Skip to content

Instantly share code, notes, and snippets.

@JulienSansot
Last active August 11, 2020 15:08
Show Gist options
  • Save JulienSansot/419dbe299790d6edaec3b5bed3c391f1 to your computer and use it in GitHub Desktop.
Save JulienSansot/419dbe299790d6edaec3b5bed3c391f1 to your computer and use it in GitHub Desktop.
Checkout.com tests
# You need a .env file in the same folder:
# PUBLIC_KEY=....
# SECRET_KEY=....
#
# And then run with ruby test_checkout.rb
#
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'checkout_sdk', '~> 0.2.1'
gem 'dotenv'
end
require 'dotenv'
Dotenv.load('.env')
CheckoutSdk.configure do |config|
config.secret_key = ENV.fetch('SECRET_KEY')
config.public_key = ENV.fetch('PUBLIC_KEY')
config.base_url = 'https://api.sandbox.checkout.com'
end
def request_token(card_expiry_year = 2030)
params = CheckoutSdk::RequestToken.new
params.type = 'card'
params.card_number = '5436031030606378'
params.card_expiry_month = '6'
params.card_expiry_year = card_expiry_year
params.card_cvv = '257'
params.card_name = 'Elmo and Friends'
params.billing_address_line1 = 'Sesame street 1'
params.billing_address_line2 = ''
params.billing_city = 'Sesame city'
params.billing_state = 'Sesame state'
params.billing_zip = '12345'
params.billing_country = 'US'
params.phone_country_code = '+1'
params.phone_number = '415 555 2671'
response = CheckoutSdk::ApiResource.new.request_token(params)
result = JSON.parse(response.body)
pp result unless response.status == 201
result['token']
end
def request_payment(token, customer_id = nil)
payment_request_source = CheckoutSdk::PaymentRequestSource.new
payment_request_source.type = 'token'
payment_request_source.token = token
payment_request_source.amount = 1000
payment_request_source.currency = 'USD'
payment_request_source.customer_id = customer_id
response = CheckoutSdk::ApiResource.new.request_payment(payment_request_source)
result = JSON.parse(response.body)
pp result unless response.status == 201
{
id: result['id'],
source_id: result['source']['id'],
fingerprint: result['source']['fingerprint'],
customer_id: result['customer']['id'],
}
end
p '------------'
use_case = 'Same Card genererates different tokens:'
p use_case
token1 = request_token
token2 = request_token
p token1
p token2
raise "Nope: #{use_case}" if token1 == token2
p '------------'
use_case = 'Same Card with different expiry date genererates different tokens:'
p use_case
token1 = request_token(2030)
token2 = request_token(2031)
p token1
p token2
raise "Nope: #{use_case}" if token1 == token2
p '------------'
use_case = 'Same card with no customer_id genererates different source IDs'
p use_case
token1 = request_token
token2 = request_token
payment1 = request_payment(token1)
payment2 = request_payment(token2)
p payment1[:source_id]
p payment2[:source_id]
raise "Nope: #{use_case}" if payment1[:source_id] == payment2[:source_id]
p '------------'
use_case = 'Same card with no customer_id genererates same fingerprint'
p use_case
token1 = request_token
token2 = request_token
payment1 = request_payment(token1)
payment2 = request_payment(token2)
p payment1[:fingerprint]
p payment2[:fingerprint]
raise "Nope: #{use_case}" unless payment1[:fingerprint] == payment2[:fingerprint]
p '------------'
use_case = 'Same card with same customer_id genererates same source IDs'
p use_case
token1 = request_token
token2 = request_token
payment1 = request_payment(token1)
payment2 = request_payment(token2, payment1[:customer_id])
p payment1[:source_id]
p payment2[:source_id]
raise "Nope: #{use_case}" unless payment1[:source_id] == payment2[:source_id]
p '------------'
use_case = 'Same Card with different expiry date but same customer_id genererates different source IDs:'
p use_case
token1 = request_token(2030)
token2 = request_token(2031)
payment1 = request_payment(token1)
payment2 = request_payment(token2, payment1[:customer_id])
p payment1[:source_id]
p payment2[:source_id]
raise "Nope: #{use_case}" if payment1[:source_id] == payment2[:source_id]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment