<filename>examples/example_goods_milestone_order.py
from __future__ import (absolute_import, print_function)
from json import dumps
from uuid import uuid4
# Import the Client class
from payoneer_escrow_sdk.client import Client


# This example file will create two new accounts - a buyer and a seller. It will
# then create an order between them, and move the order through the process to
# completion.

# ////////////////////////////////////////////////////////////////////////
#  WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING //
# //////////////////////////////////////////////////////////////////////
#
# This file contains example code which will create accounts, users, and orders,
# and move those orders through the order process. Although they are not
# destructive, some of these actions are non-reversible.
#
# You should start with the example.py file to ensure your API key and secret
# are correct before proceeding to this example file.
#
# ////////////////////////////////////////////////////////////////////////
#  WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING //
# //////////////////////////////////////////////////////////////////////


# To run this example, you will need a working Sandbox API key, stored in a
# file. Make sure you create this file. It should contain your credentials in
# the following format:
#
# PAYONEER_ESCROW_API_KEY = "ENTER_YOUR_API_KEY_HERE"
# PAYONEER_ESCROW_SECRET = "ENTER_YOUR_API_SECRET_HERE"
#
try:
    from api_credentials import (
        PAYONEER_ESCROW_API_KEY as pe_api_key,
        PAYONEER_ESCROW_SECRET as pe_secret)
except ImportError:
    import sys
    import os.path
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    from api_credentials import (
        PAYONEER_ESCROW_API_KEY as pe_api_key,
        PAYONEER_ESCROW_SECRET as pe_secret)


# Instantiate a new API client, and set the Sandbox environment flag to TRUE
should_use_sandbox = True
client = Client(pe_api_key, pe_secret, should_use_sandbox)


def pretty_print(json_string):
    """Allow command line output to look nice"""
    print(dumps(json_string, sort_keys=True, indent=4, separators=(',', ': ')), "\n")


# User emails must be unique, so create a unique identifier to include in the
# email addresses. This will allow you to run this example file multiple times
# without email address collisions. The unique identifier will also be used in
# the account company names and the order summary in order to make it easier to
# tell similar-looking test values apart.
unique = uuid4().hex[0:8]

# Create a buyer user and account
buyer_account = client.accounts().create({
    'company': "Example buyer company {}".format(unique),
    'address': '123 Pine Rd.',
    'city': 'Anytown',
    'state': 'MO',
    'country': 'us',
    'zip': '12345',
    'user_name': "<NAME> {}".format(unique),
    'user_email': "<EMAIL>".format(unique),
    'user_phone': '+1 8005551234',
    'email_confirmed': True,
    })
pretty_print(buyer_account)
# Retrieve the newly created user
buyer_user = client.accounts().users(buyer_account['account_id']).all()
buyer_user = buyer_user[0]
pretty_print(buyer_user)

# Create a seller user and account
seller_account = client.accounts().create({
    'company': "Example seller company {}".format(unique),
    'address': '123 Pine Rd.',
    'city': 'Anytown',
    'state': 'MO',
    'country': 'us',
    'zip': '12345',
    'user_name': "Example Seller {}".format(unique),
    'user_email': "<EMAIL>".format(unique),
    'user_phone': '+1 8005551234',
    'email_confirmed': True,
    })
pretty_print(seller_account)
# Retrieve the newly created user
seller_user = client.accounts().users(seller_account['account_id']).all()
seller_user = seller_user[0]
pretty_print(seller_user)

# Ask the seller to provide bank account details for the account to which they'd
# like to receive payment for the order.
response = client.accounts().users(buyer_account['account_id']).authentications(
    buyer_user['user_id']).create({
        'uri': "/accounts/{}/bankaccounts".format(seller_account['account_id']),
        'action': 'create',
        })
pretty_print(response)

# Create an order between the buyer and seller
#
# We will create a goods order with milestone payments for this example
order = client.accounts().orders(seller_account['account_id']).create({
    'order_type': 1,  # An escrow order for goods
    'seller_id': seller_user['user_id'],
    'buyer_id': buyer_user['user_id'],
    'amount': 10000,
    'pays_fees': 'seller',
    'summary': "Test goods milestones order {}".format(unique),
    'description': "A test goods milestones order generated by the " \
    "payoneer-escrow-sdk example script ({})".format(unique),
    'inspection': True,  # This order includes a goods inspection step prior to shipping
    'goodsmilestones': [
        {
            'name': 'Order created',
            'amount': 3000,
            'escrow': 4000,
        }, {
            'name': 'Goods inspected',
            'amount': 1000,
            'escrow': 6000,
        }, {
            'name': 'Goods shipped',
            'amount': 0,
            'escrow': 0,
        }, {
            'name': 'Order released',
            'amount': 6000,
            'escrow': 0,
        }],
    'invoice_num': '12345',
    'purchase_order_num': '67890',
    })
pretty_print(order)

# Display payment instructions to the buyer
response = client.accounts().users(buyer_account['account_id']).authentications(
    buyer_user['user_id']).create({
        'uri': "/accounts/{}/orders/{}/paymentinstructions".format(
            order['account_id'], order['order_id']),
        'action': 'view',
        })
pretty_print(response)
# Use the returned URL to display an iFrame to the user with the payment
# instructions. For the purposes of this example, we will imagine that the user
# has viewed those instructions as we move on to the next step...

# Create a payment on the order.
#
# This function is for testing only, and is only exposed in the Sandbox
# environment. No actual funds will be transferred.
response = client.accounts().orders(seller_account['account_id']).update(
    order['order_id'],
    {
        'action': 'add_payment',
        'confirm': True,
        'source_account_id': buyer_account['account_id'],
        'amount': 4000,
        })
pretty_print(response)

# Once the first payment is made, the first milestone is automatically released
# to the seller.
#
# Prior to the goods being inspected, payment for the second milestone must be
# placed in escrow. So, we'll add another payment. Again, this is Sandbox only.
response = client.accounts().orders(seller_account['account_id']).update(
    order['order_id'],
    {
        'action': 'add_payment',
        'confirm': True,
        'source_account_id': buyer_account['account_id'],
        'amount': 6000,
        })
pretty_print(response)

# The next step is to indicate once the goods have been inspected.
response = client.accounts().orders(order['account_id']).update(
    order['order_id'],
    {
        'action': 'completeinspection',
        'confirm': True,
        })
pretty_print(response)

# Get a list of shipment carriers
response = client.shipment_carriers().all()
pretty_print(response)

# Once the goods have been inspected, they need to be shipped to the buyer.
response = client.accounts().orders(order['account_id']).shipments(
    order['order_id']).create({
        'user_id': seller_user['user_id'],
        'carrier_id': 8,  # UPS
        'tracking_id': 'z1234567890123456',
        'description': 'Shipped via UPS ground in a protective box.',
        })
pretty_print(response)

# Check the status of the various milestones on the order.
response = client.accounts().orders(order['account_id']).milestones(
    order['order_id']).all()
pretty_print(response)

# Once the buyer has received the goods and reviewed them, the last step is to
# release the remaining payment from escrow.
#
# Note that this action usually requires the buyer to release the funds from
# escrow themselves, either by visiting the Payoneer Escrow site or via a
# Payoneer Escrow interface displayed in an iFrame on a partner's site. Partners
# are authorized to take this action on a user's behalf in the Payoneer Escrow
# Sandbox environment for testing purposes, but will not be able to do so in
# Production.
response = client.accounts().orders(order['account_id']).update(
    order['order_id'],
    {
        'action': 'release',
        'confirm': True,
        })
pretty_print(response)

<filename>examples/example_goods_milestone_order.py
from __future__ import (absolute_import, print_function)
from json import dumps
from uuid import uuid4
# Import the Client class
from payoneer_escrow_sdk.client import Client


# This example file will create two new accounts - a buyer and a seller. It will
# then create an order between them, and move the order through the process to
# completion.

# ////////////////////////////////////////////////////////////////////////
#  WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING //
# //////////////////////////////////////////////////////////////////////
#
# This file contains example code which will create accounts, users, and orders,
# and move those orders through the order process. Although they are not
# destructive, some of these actions are non-reversible.
#
# You should start with the example.py file to ensure your API key and secret
# are correct before proceeding to this example file.
#
# ////////////////////////////////////////////////////////////////////////
#  WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING //
# //////////////////////////////////////////////////////////////////////


# To run this example, you will need a working Sandbox API key, stored in a
# file. Make sure you create this file. It should contain your credentials in
# the following format:
#
# PAYONEER_ESCROW_API_KEY = "ENTER_YOUR_API_KEY_HERE"
# PAYONEER_ESCROW_SECRET = "ENTER_YOUR_API_SECRET_HERE"
#
try:
    from api_credentials import (
        PAYONEER_ESCROW_API_KEY as pe_api_key,
        PAYONEER_ESCROW_SECRET as pe_secret)
except ImportError:
    import sys
    import os.path
    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
    from api_credentials import (
        PAYONEER_ESCROW_API_KEY as pe_api_key,
        PAYONEER_ESCROW_SECRET as pe_secret)


# Instantiate a new API client, and set the Sandbox environment flag to TRUE
should_use_sandbox = True
client = Client(pe_api_key, pe_secret, should_use_sandbox)


def pretty_print(json_string):
    """Allow command line output to look nice"""
    print(dumps(json_string, sort_keys=True, indent=4, separators=(',', ': ')), "\n")


# User emails must be unique, so create a unique identifier to include in the
# email addresses. This will allow you to run this example file multiple times
# without email address collisions. The unique identifier will also be used in
# the account company names and the order summary in order to make it easier to
# tell similar-looking test values apart.
unique = uuid4().hex[0:8]

# Create a buyer user and account
buyer_account = client.accounts().create({
    'company': "Example buyer company {}".format(unique),
    'address': '123 Pine Rd.',
    'city': 'Anytown',
    'state': 'MO',
    'country': 'us',
    'zip': '12345',
    'user_name': "<NAME> {}".format(unique),
    'user_email': "<EMAIL>".format(unique),
    'user_phone': '+1 8005551234',
    'email_confirmed': True,
    })
pretty_print(buyer_account)
# Retrieve the newly created user
buyer_user = client.accounts().users(buyer_account['account_id']).all()
buyer_user = buyer_user[0]
pretty_print(buyer_user)

# Create a seller user and account
seller_account = client.accounts().create({
    'company': "Example seller company {}".format(unique),
    'address': '123 Pine Rd.',
    'city': 'Anytown',
    'state': 'MO',
    'country': 'us',
    'zip': '12345',
    'user_name': "Example Seller {}".format(unique),
    'user_email': "<EMAIL>".format(unique),
    'user_phone': '+1 8005551234',
    'email_confirmed': True,
    })
pretty_print(seller_account)
# Retrieve the newly created user
seller_user = client.accounts().users(seller_account['account_id']).all()
seller_user = seller_user[0]
pretty_print(seller_user)

# Ask the seller to provide bank account details for the account to which they'd
# like to receive payment for the order.
response = client.accounts().users(buyer_account['account_id']).authentications(
    buyer_user['user_id']).create({
        'uri': "/accounts/{}/bankaccounts".format(seller_account['account_id']),
        'action': 'create',
        })
pretty_print(response)

# Create an order between the buyer and seller
#
# We will create a goods order with milestone payments for this example
order = client.accounts().orders(seller_account['account_id']).create({
    'order_type': 1,  # An escrow order for goods
    'seller_id': seller_user['user_id'],
    'buyer_id': buyer_user['user_id'],
    'amount': 10000,
    'pays_fees': 'seller',
    'summary': "Test goods milestones order {}".format(unique),
    'description': "A test goods milestones order generated by the " \
    "payoneer-escrow-sdk example script ({})".format(unique),
    'inspection': True,  # This order includes a goods inspection step prior to shipping
    'goodsmilestones': [
        {
            'name': 'Order created',
            'amount': 3000,
            'escrow': 4000,
        }, {
            'name': 'Goods inspected',
            'amount': 1000,
            'escrow': 6000,
        }, {
            'name': 'Goods shipped',
            'amount': 0,
            'escrow': 0,
        }, {
            'name': 'Order released',
            'amount': 6000,
            'escrow': 0,
        }],
    'invoice_num': '12345',
    'purchase_order_num': '67890',
    })
pretty_print(order)

# Display payment instructions to the buyer
response = client.accounts().users(buyer_account['account_id']).authentications(
    buyer_user['user_id']).create({
        'uri': "/accounts/{}/orders/{}/paymentinstructions".format(
            order['account_id'], order['order_id']),
        'action': 'view',
        })
pretty_print(response)
# Use the returned URL to display an iFrame to the user with the payment
# instructions. For the purposes of this example, we will imagine that the user
# has viewed those instructions as we move on to the next step...

# Create a payment on the order.
#
# This function is for testing only, and is only exposed in the Sandbox
# environment. No actual funds will be transferred.
response = client.accounts().orders(seller_account['account_id']).update(
    order['order_id'],
    {
        'action': 'add_payment',
        'confirm': True,
        'source_account_id': buyer_account['account_id'],
        'amount': 4000,
        })
pretty_print(response)

# Once the first payment is made, the first milestone is automatically released
# to the seller.
#
# Prior to the goods being inspected, payment for the second milestone must be
# placed in escrow. So, we'll add another payment. Again, this is Sandbox only.
response = client.accounts().orders(seller_account['account_id']).update(
    order['order_id'],
    {
        'action': 'add_payment',
        'confirm': True,
        'source_account_id': buyer_account['account_id'],
        'amount': 6000,
        })
pretty_print(response)

# The next step is to indicate once the goods have been inspected.
response = client.accounts().orders(order['account_id']).update(
    order['order_id'],
    {
        'action': 'completeinspection',
        'confirm': True,
        })
pretty_print(response)

# Get a list of shipment carriers
response = client.shipment_carriers().all()
pretty_print(response)

# Once the goods have been inspected, they need to be shipped to the buyer.
response = client.accounts().orders(order['account_id']).shipments(
    order['order_id']).create({
        'user_id': seller_user['user_id'],
        'carrier_id': 8,  # UPS
        'tracking_id': 'z1234567890123456',
        'description': 'Shipped via UPS ground in a protective box.',
        })
pretty_print(response)

# Check the status of the various milestones on the order.
response = client.accounts().orders(order['account_id']).milestones(
    order['order_id']).all()
pretty_print(response)

# Once the buyer has received the goods and reviewed them, the last step is to
# release the remaining payment from escrow.
#
# Note that this action usually requires the buyer to release the funds from
# escrow themselves, either by visiting the Payoneer Escrow site or via a
# Payoneer Escrow interface displayed in an iFrame on a partner's site. Partners
# are authorized to take this action on a user's behalf in the Payoneer Escrow
# Sandbox environment for testing purposes, but will not be able to do so in
# Production.
response = client.accounts().orders(order['account_id']).update(
    order['order_id'],
    {
        'action': 'release',
        'confirm': True,
        })
pretty_print(response)

# Import the Client class # This example file will create two new accounts - a buyer and a seller. It will # then create an order between them, and move the order through the process to # completion. # //////////////////////////////////////////////////////////////////////// #  WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING // # ////////////////////////////////////////////////////////////////////// # # This file contains example code which will create accounts, users, and orders, # and move those orders through the order process. Although they are not # destructive, some of these actions are non-reversible. # # You should start with the example.py file to ensure your API key and secret # are correct before proceeding to this example file. # # //////////////////////////////////////////////////////////////////////// #  WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING // # ////////////////////////////////////////////////////////////////////// # To run this example, you will need a working Sandbox API key, stored in a # file. Make sure you create this file. It should contain your credentials in # the following format: # # PAYONEER_ESCROW_API_KEY = "ENTER_YOUR_API_KEY_HERE" # PAYONEER_ESCROW_SECRET = "ENTER_YOUR_API_SECRET_HERE" # # Instantiate a new API client, and set the Sandbox environment flag to TRUE Allow command line output to look nice # User emails must be unique, so create a unique identifier to include in the # email addresses. This will allow you to run this example file multiple times # without email address collisions. The unique identifier will also be used in # the account company names and the order summary in order to make it easier to # tell similar-looking test values apart. # Create a buyer user and account # Retrieve the newly created user # Create a seller user and account # Retrieve the newly created user # Ask the seller to provide bank account details for the account to which they'd # like to receive payment for the order. # Create an order between the buyer and seller # # We will create a goods order with milestone payments for this example # An escrow order for goods # This order includes a goods inspection step prior to shipping # Display payment instructions to the buyer # Use the returned URL to display an iFrame to the user with the payment # instructions. For the purposes of this example, we will imagine that the user # has viewed those instructions as we move on to the next step... # Create a payment on the order. # # This function is for testing only, and is only exposed in the Sandbox # environment. No actual funds will be transferred. # Once the first payment is made, the first milestone is automatically released # to the seller. # # Prior to the goods being inspected, payment for the second milestone must be # placed in escrow. So, we'll add another payment. Again, this is Sandbox only. # The next step is to indicate once the goods have been inspected. # Get a list of shipment carriers # Once the goods have been inspected, they need to be shipped to the buyer. # UPS # Check the status of the various milestones on the order. # Once the buyer has received the goods and reviewed them, the last step is to # release the remaining payment from escrow. # # Note that this action usually requires the buyer to release the funds from # escrow themselves, either by visiting the Payoneer Escrow site or via a # Payoneer Escrow interface displayed in an iFrame on a partner's site. Partners # are authorized to take this action on a user's behalf in the Payoneer Escrow # Sandbox environment for testing purposes, but will not be able to do so in # Production.