You can do this via the Rye API
Python example
Make sure you replace the API key values and the address information
import requests
# Constants from console.rye.com, replace all the dummy values below.
RYE_API_KEY = 'Basic UllFLL3VjYTlxMU0NjI1YzQwNzZiMWI1Og==' # Change me
RYE_SHOPPER_IP = '181.12.22.41' # Change me
RYE_CARD_TOKENIZATION_SECRET = 'Basic UW9nRjRoeHxMydGl6Y2NoMjCxRLNmI5RSWp5MGxmO221lJVRldKF0ZMGFsdvDF3ZdjRVrdk5CVTNJSDWecDhB2M0dXYWFBaDNjxdjJXaDF3dTU0eXE1Y200d24xcUU0bXFIeEpWNllUWXE=' # Change me
RYE_API_URL = 'https://graphql.api.rye.com/v1/query'
SPREEDLY_API_URL = 'https://core.spreedly.com/v1/payment_methods.json'
def create_cart(product_id, quantity, buyer_info):
"""
Create a cart with the specified product and buyer information.
Args:
product_id (str): The ID of the product to add to the cart.
quantity (int): The quantity of the product to add to the cart.
buyer_info (dict): A dictionary containing buyer information.
Returns:
dict: The response from the Rye API containing the cart ID and any errors.
"""
query = """
mutation ($input: CartCreateInput!) {
createCart(input: $input) {
cart {
id
}
errors {
code
message
}
}
}
"""
variables = {
"input": {
"items": {
"amazonCartItemsInput": [{
"quantity": quantity,
"productId": product_id
}]
},
"buyerIdentity": buyer_info
}
}
response = requests.post(
RYE_API_URL,
headers={
'Content-Type': 'application/json',
'Authorization': RYE_API_KEY,
'Rye-Shopper-IP': RYE_SHOPPER_IP
},
json={
'query': query,
'variables': variables
}
)
return response.json()
def tokenize_card(card_info):
"""
Tokenize a credit card using the Spreedly API.
Args:
card_info (dict): A dictionary containing credit card information.
Returns:
dict: The response from the Spreedly API containing the payment method token.
"""
payload = {
'payment_method': {
'credit_card': card_info,
'retained': True
}
}
response = requests.post(
SPREEDLY_API_URL,
headers={
'Authorization': RYE_CARD_TOKENIZATION_SECRET,
'Content-Type': 'application/json'
},
json=payload
)
return response.json()
def submit_cart(cart_id, payment_method_token):
"""
Submit a cart with the specified cart ID and payment method token.
Args:
cart_id (str): The ID of the cart to submit.
payment_method_token (str): The token of the payment method to use.
Returns:
dict: The response from the Rye API containing the submission status and any errors.
"""
query = """
mutation ($input: CartSubmitInput!) {
submitCart(input: $input) {
cart {
id
stores {
isSubmitted
errors {
code
message
}
}
}
errors {
code
message
}
}
}
"""
variables = {
'input': {
'id': cart_id,
'token': payment_method_token
}
}
response = requests.post(
RYE_API_URL,
headers={
'Content-Type': 'application/json',
'Authorization': RYE_API_KEY,
'Rye-Shopper-IP': RYE_SHOPPER_IP
},
json={
'query': query,
'variables': variables
}
)
return response.json()
def main():
# Buyer information
buyer_info = {
"firstName": "John",
"lastName": "Smith",
"email": "[email protected]",
"phone": "+14081234567",
"address1": "100 Main St.",
"address2": "",
"city": "Berkeley",
"provinceCode": "CA",
"countryCode": "US",
"postalCode": "94708"
}
# Create a cart
product_id = "B07Z4L57XQ"
quantity = 1
create_cart_response = create_cart(product_id, quantity, buyer_info)
cart_id = create_cart_response['data']['createCart']['cart']['id']
errors = create_cart_response['data']['createCart']['errors']
if cart_id and not errors:
print('✅ Cart successfully created!')
else:
print('❌ Error: Unable to create cart.')
print('Response from server:')
print(create_cart_response)
return
# Tokenize credit card
card_info = {
"first_name": "John",
"last_name": "Smith",
"number": "4242424242424242",
"verification_value": "181",
"month": "04",
"year": "2029"
}
tokenization_response = tokenize_card(card_info)
payment_method_token = tokenization_response.get('transaction', {}).get('payment_method', {}).get('token')
if payment_method_token:
print('✅ Card successfully tokenized!')
else:
print('❌ Error: Unable to tokenize card.')
print('Response from server:')
print(tokenization_response)
return
# Submit cart
submit_cart_response = submit_cart(cart_id, payment_method_token)
is_submitted = submit_cart_response['data']['submitCart']['cart']['stores'][0]['isSubmitted']
errors = submit_cart_response['data']['submitCart']['errors']
if is_submitted and not errors:
print('🎉 Hooray! You ordered an Amazon product via Python!')
print('You can view your order on https://console.rye.com/orders')
else:
print('❌ Error! Unable to submit cart.')
print('Response from server:')
print(submit_cart_response)
if __name__ == '__main__':
main()