Python Client for Openpay API Services
This is a client implementing the payment services for Openpay at www.openpay.mx
You don't need this source code unless you want to modify the package. If you just want use the Openpay Python bindings, you should run:
pip install openpay
or
pip install openpay --udgrade
See www.pip-installer.org/en/latest/index.html for instructions on installing pip.
Before use the library will be necessary to set up your Merchant ID and Private key.
import openpay
openpay.api_key = "sk_10d37cc4da8e4ffd902cdf62e37abd1b"
openpay.verify_ssl_certs = False
openpay.merchant_id = "mynvbjhtzxdyfewlzmdo"
openpay.production = True # By default this works in sandbox mode
openpay.country = 'mx' # 'mx' is default value, to use for Colombia set country='co'
Once configured the library, you can use it to interact with Openpay API services.
Creating a token:
openpay.Token.create(
card_number="4111111111111111",
holder_name="Juan Perez Ramirez",
expiration_year="20",
expiration_month="12",
cvv2="110",
address={
"city": "Querétaro",
"country_code": "MX",
"postal_code": "76900",
"line1": "Av 5 de Febrero",
"line2": "Roble 207",
"line3": "col carrillo",
"state": "Queretaro"
})
Creating a customer:
customer = openpay.Customer.create(
name="Juan",
email="[email protected]",
address={
"city": "Queretaro",
"state": "Queretaro",
"line1": "Calle de las penas no 10",
"postal_code": "76000",
"line2": "col. san pablo",
"line3": "entre la calle de la alegria y la calle del llanto",
"country_code": "MX"
},
last_name="Perez",
phone_number="44209087654"
)
Once you have a customer, you have access to few resources for current customer. According to the current version of the Openpay API, these resources are:
- cards
- charges
- transfers
- payouts
- bank accounts
- subscriptions
You can access all of these resources as public variables of the root instance (customer in this example), so, if you want to add a new card you will be able to do it as follows:
card = customer.cards.create(
card_number="4111111111111111",
holder_name="Juan Perez Ramirez",
expiration_year="20",
expiration_month="12",
cvv2="110",
address={
"city": "Querétaro",
"country_code": "MX",
"postal_code": "76900",
"line1": "Av 5 de Febrero",
"line2": "Roble 207",
"line3": "col carrillo",
"state": "Queretaro"
})
Get a customer
customer = openpay.Customer.retrieve('amce5ycvwycfzyarjf8l')
Update a customer
customer = openpay.Customer.retrieve('amce5ycvwycfzyarjf8l')
customer.last_name = "Lopez"
customer.save()
Get all customers
customers = openpay.Customer.all()
Get all customer cards
cards = customer.cards.all()
Get specific customer card
card = customer.cards.retrieve('kvxvccpsesm4pwmtgnjb')
Delete a customer card
card = customer.cards.retrieve('kvxvccpsesm4pwmtgnjb')
card.delete()
Get all customer transfers (inbound and outbound)
transfers = customer.transfers.all()
Create a customer transfer
transfer1 = customer.transfers.create(
customer_id="acuqxruyv0hi1wfdwmym",
amount=100,
description="Test transfer",
order_id="oid-00059"
)
Get specific transfer
transfer2 = customer.transfers.retrieve(transfer1.id)
Add bank account to customer
bank_account = customer.bank_accounts.create(
clabe="032180000118359719",
alias="Cuenta principal",
holder_name="Juan Perez"
)
Get all customer bank accounts
accounts = customer.bank_accounts.all()
Get specific bank account
account = customer.back_accounts.retrieve("bsbg7igxh3yukpu8t2q4")
Add subscription to customer
customer.subscriptions.create(plan_id="pbkliysxavp8bvvp8f0k", trial_days="5", card_id="kvxvccpsesm4pwmtgnjb")
Cancel subscription
subscription = customer.subscriptions.all()[0]
subscription.delete()
List all customers subscriptions
customer.subscriptions.all()
Update subscription
subscription = customer.subscriptions.all()[0]
subscription.cancel_at_end_period = True
subscription.save()
Add payout for customer
bank_account = customer.bank_accounts.all()[0] # We get the first account
customer.payouts.create(
method='bank_account', # possible values ['bank_accunt', 'card']
destination_id=bank_account.id,
amount="100",
description="First payout",
order_id="oid-00058"
)
Get all payouts
customer.payouts.all()
Get specific payout
customer.payouts.retrieve("tbs6a7g4pypww4eq640d")
Create new plan
plan = openpay.Plan.create(
amount=150.00,
status_after_retry="cancelled",
retry_times=2,
name="Curso de Ingles",
repeat_unit="month",
trial_days=30,
repeat_every=1
)
Get specific plan
plan2 = openpay.Plan.retrieve(plan.id)
Update a plan
plan = openpay.Plan.retrieve('pbkliysxavp8bvvp8f0k')
plan.name = "Curso de Ingles II"
plan.save()
Delete plan
plan = openpay.Plan.retrieve('pbkliysxavp8bvvp8f0k')
plan.delete()
You may charge a fee as follows:
fee = openpay.Fee.create(
customer_id="amce5ycvwycfzyarjf8l",
amount=12.50,
description="Fee Charge",
order_id="oid=1245"
)
List all charged fees
fees = openpay.Fee.all()
import openpay
openpay.api_key = "sk_10d37cc4da8e4ffd902cdf62e37abd1b"
openpay.verify_ssl_certs = False
openpay.merchant_id = "mynvbjhtzxdyfewlzmdo"
openpay.production = True # By default this works in sandbox mode
openpay.country = 'co' # 'mx' is default value, to use for Colombia set country='co'
Once configured the library, you can use it to interact with Openpay API services for Colombia.
Creating a token:
openpay.Token.create(
holder_name="Juan Perez Ramirez",
card_number="4111111111111111",
cvv2="110",
expiration_month="12",
expiration_year="25",
address={
"city": "Bogotá",
"country_code": "CO",
"postal_code": "76900",
"line1": "bogota",
"line2": "colombia",
"line3": "col carrillo",
"state": "Bogota"
})
Creating a customer:
customer = openpay.Customer.create(
name="Juan",
last_name="Perez",
email="[email protected]",
address={
"city": "Bogotá",
"country_code": "CO",
"postal_code": "76900",
"line1": "bogota",
"line2": "colombia",
"line3": "col carrillo",
"state": "Bogota"
},
phone_number="7711234567"
)
Once you have a customer, you have access to few resources for current customer. According to the current version of the Openpay API, these resources are:
- cards
- charges
- subscriptions
- pse
You can access all of these resources as public variables of the root instance (customer in this example), so, if you want to add a new card you will be able to do it as follows:
card = customer.cards.create(
card_number="4111111111111111",
holder_name="Juan Perez Ramirez",
expiration_year="25",
expiration_month="12",
cvv2="110",
address={
"city": "Bogotá",
"country_code": "CO",
"postal_code": "76900",
"line1": "bogota",
"line2": "colombia",
"line3": "col carrillo",
"state": "Bogota"
})
Get a customer
customer = openpay.Customer.retrieve('amce5ycvwycfzyarjf8l')
Update a customer
customer = openpay.Customer.retrieve('amce5ycvwycfzyarjf8l')
customer.last_name = "Lopez"
customer.save()
Get all customers
customers = openpay.Customer.all()
Get all customer cards
cards = customer.cards.all()
Get specific customer card
card = customer.cards.retrieve('kvxvccpsesm4pwmtgnjb')
Delete a customer card
card = customer.cards.retrieve('kvxvccpsesm4pwmtgnjb')
card.delete()
Add pse to customer
pse = customer.pse.create(
method='bank_account',
currency="COP",
iva="1900",
description="description",
redirect_url="/"
)
pse = pse.create(
method='bank_account',
currency="COP",
iva="1900",
description="description",
redirect_url="/",
customer={
"name": "Cliente Colombia",
"last_name": "Vazquez Juarez",
"email": "[email protected]",
"phone_number": "4448936475",
"requires_account": false,
"customer_address": {
"department": "Medellín",
"city": "Antioquía",
"additional": "Avenida 7m bis #174-25 Apartamento 637"
}
}
)
Add subscription to customer
customer.subscriptions.create(plan_id="pbkliysxavp8bvvp8f0k", trial_days="5", card_id="kvxvccpsesm4pwmtgnjb")
Cancel subscription
subscription = customer.subscriptions.all()[0]
subscription.delete()
List all customers subscriptions
customer.subscriptions.all()
Update subscription
subscription = customer.subscriptions.all()[0]
subscription.cancel_at_end_period = True
subscription.save()
Create new plan
plan = openpay.Plan.create(
amount=150.00,
status_after_retry="cancelled",
retry_times=2,
name="Curso de Ingles",
repeat_unit="month",
trial_days=30,
repeat_every=1
)
Get specific plan
plan2 = openpay.Plan.retrieve(plan.id)
Update a plan
plan = openpay.Plan.retrieve('pbkliysxavp8bvvp8f0k')
plan.name = "Curso de Ingles II"
plan.save()
Delete plan
plan = openpay.Plan.retrieve('pbkliysxavp8bvvp8f0k')
plan.delete()
The Openpay API generates several types of errors depending on the situation, to handle this, the Python client has implemented four type of exceptions:
- InvalidRequestError: This category includes requests when format is not JSON and Requests with non existents urls
- AuthenticationError: missing Private key
- CardError: Transfer not accepted, Declined card, Expired card, Inssuficient funds, Stolen Card, Fraudulent card.
- APIError: All other types API errors