Last active
October 10, 2017 06:26
-
-
Save jatinkrmalik/aa9124ce0e44c3456459276d2d585d8e to your computer and use it in GitHub Desktop.
Implementing RazorPay client in Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Razorpay docs: | |
# https://github.com/razorpay/razorpay-python | |
# https://docs.razorpay.com | |
import razorpay # pip3 install razorpay | |
api_key = '<enter your api key here>' | |
api_secret = '<enter your api secret here>' | |
class RazorPay: | |
def __init__(self): | |
self.__client = razorpay.Client(auth=(api_key, api_secret)) | |
self.__client.set_app_details({"title": "<enter your app title here>", "version": "<enter your app version here>"}) | |
# Payments - remember to give amount in Paisa i.e. 100p = 1 Re | |
def fetch_all_payments(self): | |
return self.__client.payment.all() | |
def capture_payment(self, payment_id, amount): | |
return self.__client.payment.capture(payment_id, amount) | |
def fetch_payment(self, payment_id): | |
return self.__client.payment.fetch(payment_id) | |
# Refunds - remember to give amount in Paisa i.e. 100p = 1 Re | |
def fetch_all_refunds(self): | |
return self.__client.refund.all() | |
def fetch_refund(self, refund_id): | |
return self.__client.refund.fetch(refund_id) | |
def process_refund(self, payment_id, refund_amount): | |
return self.__client.payment.refund(payment_id, refund_amount) | |
# Orders - Sample request and response data included | |
def create_order(self, data): | |
''' | |
:param data: | |
{ | |
"amount": "amount of order", | |
"currency": "currency of order", | |
"receipt": "receipt id of order", | |
"payment_capture": "1 if capture should be done automatically or else 0", | |
"notes": "optional notes for order" | |
} | |
:return: | |
{ | |
"id": "order_6JUYuvmgCLfgjY", | |
"entity": "order", | |
"amount": 50000, | |
"currency": "INR", | |
"receipt": "rcptid42", | |
"status": "created", | |
"attempts": 0, | |
"notes": [], | |
"created_at": 1487348538 | |
} | |
''' | |
return self.__client.order.create(data=data) | |
def fetch_all_orders(self): | |
return self.__client.order.all() | |
def fetch_order(self, order_id): | |
return self.__client.order.fetch(order_id) | |
def fetch_payment_from_order(self, order_id): | |
return self.__client.order.payment(order_id) | |
# Invoices - Sample request and response data included | |
def create_invoice(self, data): | |
''' | |
:param data: | |
{ | |
"type":"invoice", | |
"customer":{ | |
"name": "Test", | |
"email":"test@test.com", | |
"contact":"9999999999", | |
"billing_address":{ | |
"line1":"#11, Navi Camp", | |
"city":"Pandora", | |
"state":"Karnataka", | |
"zipcode":"560076", | |
"country":"India" | |
} | |
}, | |
"line_items":[ | |
{ | |
"name":"Book / English August", | |
"description":"Funny story of an IAS officer wanting to be aything other than an IAS.", | |
"amount":2000000, | |
"currency":"INR", | |
"quantity":2 | |
}, | |
{ | |
"item_id": "item_8fhLmXnzF1AYvS" | |
} | |
], | |
"currency":"INR", | |
"sms_notify": "1", | |
"email_notify": "1" | |
} | |
:return: | |
{ | |
"id": "inv_7Nb4lWu28WPV5L", | |
"entity": "invoice", | |
"receipt": "max-14-char-no", | |
"customer_id": "cust_7NZ3uTXhicUQgQ", | |
"customer": { | |
"name": null, | |
"email": "test@test.com", | |
"contact": "9999999999", | |
"billing_address": { | |
"id": "addr_7NZ3uUd4tI7RCF", | |
"type": "billing_address", | |
"primary": true, | |
"line1": "#11, Navi Camp", | |
"line2": null, | |
"zipcode": "560076", | |
"city": "Pandora", | |
"state": "Karnataka", | |
"country": "in" | |
} | |
}, | |
"order_id": null, | |
"line_items": [ | |
{ | |
"id": "li_7Nb4lYxT02VP2l", | |
"name": "Book / English August", | |
"description": null, | |
"amount": 20000, | |
"currency": "INR", | |
"quantity": 2 | |
}, | |
{ | |
"id": "li_7Nb4lZcjZSndqz", | |
"name": "Book / Ignited Minds", | |
"description": null, | |
"amount": 15000, | |
"currency": "INR", | |
"quantity": 1 | |
} | |
], | |
"payment_id": null, | |
"status": "draft", | |
"expire_by": 1498439025, | |
"issued_at": 1488446398, | |
"paid_at": null, | |
"expired_at": null, | |
"sms_status": "pending", | |
"email_status": "pending", | |
"date": 1488439025, | |
"terms": "Terms and condition of the service/invoice", | |
"amount": 55000, | |
"description": "Just an optional description for the invoice", | |
"notes": { | |
"random_key": "Random note" | |
}, | |
"comment": "Optional comment to the customer for the invoice", | |
"currency": "INR", | |
"short_url": null, | |
"view_less": true, | |
"type": "invoice", | |
"user_id": null, | |
"created_at": 1488446398 | |
} | |
''' | |
return self.__client.invoice.create(data=data) | |
def fetch_all_invoice(self): | |
return self.__client.invoice.all() | |
def fetch_invoice(self, invoice_id): | |
return self.__client.invoice.fetch(invoice_id) | |
# Customers | |
def create_customer(self, data): | |
return self.__client.customer.create(data=data) | |
def fetch_customer(self, customer_id): | |
return self.__client.customer.fetch(customer_id=customer_id) | |
def edit_customer(self, customer_id, data): | |
return self.__client.customer.edit(customer_id=customer_id, data=data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment