Created
March 21, 2014 01:05
-
-
Save auser/9677501 to your computer and use it in GitHub Desktop.
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
require 'rubygems' | |
require 'httparty' | |
require 'pp' | |
# Config | |
backend = "http://localhost:3000/" # FILL IN YOUR OWN SPREE ENDPOINT | |
apiKey = '' # FILL IN A USER's APIKEY | |
key = '' # FILL IN YOUR OWN STRIPE publishable key | |
cardNum = '4242424242424242' # | |
exp_month = '1' | |
exp_year = '2016' | |
cvc = '123' | |
method = 'POST' | |
## Helper | |
def print_resp(msg, resp) | |
puts "========== #{msg} ============" | |
pp resp | |
end | |
# Get the products | |
params = { | |
'q[meta_keywords_cont]' => 'edu', | |
'token' => apiKey | |
} | |
resp = HTTParty.get(backend + '/store/api/products', { query: params }) | |
products = resp['products'] | |
first_product = products[0] | |
# 1. Create an order | |
resp = HTTParty.post(backend + '/store/api/checkouts', { | |
query: { token: apiKey }, | |
body: { | |
order: { | |
line_items: { | |
"0" => { | |
variant_id: first_product['id'], | |
quantity: 1 | |
} | |
} | |
} | |
} | |
}) | |
print_resp("Creating an order", resp) | |
order_number = resp['number'] | |
# 2. Get available payment methods | |
resp = HTTParty.get(backend + '/store/api/orders/' + order_number + '/payments/new', { | |
query: { token: apiKey } | |
}) | |
print_resp("Getting all available payment methods", resp) | |
methods = resp['payment_methods'] | |
## Move to payment state | |
resp = HTTParty.put(backend + '/store/api/checkouts/' + order_number + '/next', { | |
query: { token: apiKey }, | |
body: { | |
amount: '10' | |
} | |
}) | |
print_resp("Moved to payment step", resp) | |
stripe = methods[0] | |
## CALL OUT TO STRIPE [Fake CC info] | |
stripeResp = HTTParty.post('https://api.stripe.com/v1/tokens', { | |
query: { | |
'card[number]' => cardNum, | |
'card[exp_month]' => exp_month, | |
'card[exp_year]' => exp_year, | |
'card[cvc]' => cvc, | |
'key' => key | |
} | |
}) | |
print_resp("Getting stripe token", stripeResp) | |
token = stripeResp['id'] | |
# 3. Create a new payment | |
resp = HTTParty.put(backend + '/store/api/checkouts/' + order_number, { | |
query: { token: apiKey }, | |
body: { | |
order: { | |
payments_attributes: { | |
payment_method_id: stripe['id'] | |
}, | |
payment_source: { | |
"#{stripe['id']}" => { | |
gateway_payment_profile_id: token, | |
first_name: 'Fred', | |
last_name: 'Wilson', | |
year: exp_year, | |
month: exp_month, | |
cc_type: stripeResp['card']['type'].downcase, | |
last_digits: stripeResp['card']['last4'] | |
} | |
} | |
} | |
} | |
}) | |
print_resp("Creating a new payment", resp) | |
# 4. move to confirmation step | |
resp = HTTParty.put(backend + '/store/api/checkouts/' + order_number, { | |
query: { | |
token: apiKey | |
} | |
}) | |
print_resp("Moving to confirmation step", resp) | |
resp = HTTParty.put(backend + '/store/api/checkouts/' + order_number, { | |
query: { | |
token: apiKey | |
} | |
}) | |
print_resp("Completed flow", resp) | |
## Rails console | |
puts <<-EOE | |
num = '#{order_number}' | |
order = Spree::Order.find_by(number: num) | |
payment = order.payments.first | |
EOE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment