The Open Event Server enables organizers to manage events from concerts to conferences and meetups. It offers features for events with several tracks and venues. This blog post explains how Paypal has been integrated in the Open Event Server in order to accept payments for tickets.
The integration of Paypal in the server involved the following steps:
- An endpoint to accept the Paypal token from the client applications.
- Using the token to get the approved payment details.
- Capturing the payment using the fetched payment details.
Endpoint for Paypal token
The server exposes an endpoint to get the Paypal token in order to accept payments.
api.route(ChargeList, ‘charge_list’, ‘/orders/<identifier>/charge’, ‘/orders/<order_identifier>/charge’) |
The above endpoint accepts the Paypal token and uses that to get the payment details from Paypal and then capture the payments.
Getting Approved Payment Details
We use the Paypal Name-Value pair API in the project. First we get the credentials of the event organizer who will be accepting the payments using a call to the get_credentials helper method. It returns the data as the following dictionary:
credentials = { 'USER': settings['paypal_live_username'], 'PWD': settings['paypal_live_password'], 'SIGNATURE': settings['paypal_live_signature'], 'SERVER': 'https://api-3t.paypal.com/nvp', 'CHECKOUT_URL': 'https://www.paypal.com/cgi-bin/webscr', 'EMAIL': '' if not event or not event.paypal_email or event.paypal_email == "" else event.paypal_email }
Next, we use the credentials to get the approved payment details from paypal using the following code snippet.
@staticmethod def get_approved_payment_details(order, credentials=None): if not credentials: credentials = PayPalPaymentsManager.get_credentials(order.event) if not credentials: raise Exception('PayPal credentials have not been set correctly') data = { 'USER': credentials['USER'], 'PWD': credentials['PWD'], 'SIGNATURE': credentials['SIGNATURE'], 'SUBJECT': credentials['EMAIL'], 'METHOD': 'GetExpressCheckoutDetails', 'VERSION': PayPalPaymentsManager.api_version, 'TOKEN': order.paypal_token } if current_app.config['TESTING']: return data response = requests.post(credentials['SERVER'], data=data) return json.loads(response.text)
Capturing the payments
After successfully fetching the payment details, the final step is to capture the payment. We set the amount to be charged to the amount of the order and the payer_id to be the payer id received from step 2. Then we simply make a POST request to the Paypal nvp server and capture the payments. The below method is responsible for executing this task:
@staticmethod def capture_payment(order, payer_id, currency=None, credentials=None): if not credentials: credentials = PayPalPaymentsManager.get_credentials(order.event) if not credentials: raise Exception('PayPal credentials have not be set correctly') if not currency: currency = order.event.payment_currency if not currency or currency == "": currency = "USD" data = { 'USER': credentials['USER'], 'PWD': credentials['PWD'], 'SIGNATURE': credentials['SIGNATURE'], 'SUBJECT': credentials['EMAIL'], 'METHOD': 'DoExpressCheckoutPayment', 'VERSION': PayPalPaymentsManager.api_version, 'TOKEN': order.paypal_token, 'PAYERID': payer_id, 'PAYMENTREQUEST_0_PAYMENTACTION': 'SALE', 'PAYMENTREQUEST_0_AMT': order.amount, 'PAYMENTREQUEST_0_CURRENCYCODE': currency, } response = requests.post(credentials['SERVER'], data=data) return json.loads(response.text)
References
- Paypal NVP : https://developer.paypal.com/docs/classic/api/NVPAPIOverview/
- Paypal Python SDK: https://github.com/paypal/PayPal-Python-SDK
- Flask REST JSON API: flask-rest-jsonapi