Setup payment method and checkout later
This solution makes sense if you require your customers to provide a payment method upfront (to be charged later). This solution also makes sense if you are just looking to collect updated payment information for an existing active customer.
Install and import our client
For this recipe, we will use the Python SDK, which you can install using pip install getopenpay or poetry add getopenpay":
from getopenpay.client import ApiKeys, OpenPayClient
OP_PUBLISHABLE_TOKEN = 'TODO_YOUR_PUBLISHABLE_TOKEN'
OP_SECRET_TOKEN = 'TODO_YOUR_SECRET_TOKEN'
api_keys = ApiKeys(publishable_key=OP_PUBLISHABLE_TOKEN, secret_key=OP_SECRET_TOKEN)
# sandbox/staging environment
sandbox_host = 'https://connto.openpaystaging.com'
# production environment
production_host = 'https://connto.getopenpay.com'
client = OpenPayClient(api_keys=api_keys, host=sandbox_host)
Create customer
create_customer_request = CreateCustomerRequest(
email='test_customer+1@getopenpay.com',
first_name='John',
last_name='Smith',
line1='123 Main St',
city='San Francisco',
state='CA',
country='US',
zip_code='94105'
)
customer_external = op_client.customers.create_customer(
create_customer_request=create_customer_request.to_dict()
)
Create a checkout session
Create a CheckoutSession withSETUP
mode
checkout = op_client.checkout.create_checkout_session(
CreateCheckoutSessionRequest(
customer_id=customer_id, # <- pass in cutomser_id
mode=CheckoutMode.SETUP,
success_url='https://youapp.com/success-callback',
)
)
Reroute to our hosted page
print(f'Reroute customer to this page for payment: {checkout_session.url})
Handle success callback
Verify customer and payment method
# handle success callback
@router.get('/success-callback')
def success_callback(request: Request):
query_params = request.query_params
secure_token = query_params.get('secure_token')
payment_method_id = query_params.get('payment_method_id')
checkout_session = op_client.checkout.verify_checkout_session_payment_method(
checkout_secure_token=secure_token,
payment_method_id=payment_method_id,
)
if checkout_session:
# (Optional) continue for authorization/charging