Braintree payment gateway integration with Django
Introduction
Here Im going to tell you How to integrate the Braintree payment gateway with Django based application. I really thankful to Braintree support team because when i start to integrate i feel really hard. i have successfully completed the project and here i'm put the Github link for demo project.
Workflow Diagram
Integration
Step 1 : Create Sandbox account with Barintree :- Create sandox account
Step 2 : Setup Client
Step 3 : Setup Server
Step 5 : Have fun .... with Brain tree
BrainTree Integration with Django Project
Create the Sandbox account with braintree , they will provide the merchant id, private and public key
Install braintree in your env using pip ( pip install braintree ).
Add the keys and merchant id with project settings
settings.py
BRAINTREE_MERCHANT = 'dznq5k44zc3qrycm'
BRAINTREE_PUBLIC_KEY = 'ttkr358rbpfnjvgn'
BRAINTREE_PRIVATE_KEY = 'dcfa177a5f71e00370323f17221e6cea'
Configure the Braintree with this above credentials
Views.py
from django.conf import settings
import braintree
braintree.Configuration.configure(braintree.Environment.Sandbox,
merchant_id=settings.BRAINTREE_MERCHANT,
public_key=settings.BRAINTREE_PUBLIC_KEY,
private_key=settings.BRAINTREE_PRIVATE_KEY)
Genrate a client token in server side and render to the client side for processing payment method nonce.
Views.py
@login_required
@csrf_exempt
def checkout(request):
rg = request.POST.get
amount = request.POST.get('prise') // when user click the subscription for payment
user = Staff.objects.get(id=request.user.id)
a_customer_id = ''
if not user.customer_id:
result = braintree.Customer.create({
"first_name": user.first_name,
"last_name": user.last_name,
"company": "Braintree",
"email": user.email,
"phone": "312.555.1234",
"fax": "614.555.5678",
"website": "www.example.com"
})
if result.is_success:
user.customer_id = result.customer.id
user.save()
a_customer_id = user.customer_id
else:
a_customer_id = user.customer_id
if not user.client_token:
client_token = client_token = braintree.ClientToken.generate({
"customer_id": a_customer_id
})
user.client_token = client_token
user.save()
else:
client_token = user.client_token
varibles ={'amount':amount,'client_token':client_token}
return render(request, 'checkout.html',varibles)
Setup Client
checkout.html
<div class="form-style-5">
{{amount}}
<input type='hidden' id='client_token' value='{{client_token}}'> // client token from server side
<form id="checkout" method="post" action="/payment">
<div id="payment-form">
<input type='hidden' name='amount' value='{{amount}}'> // amount
<input type='text' name='amount' value='{{amount}}' readonly>
</div>
<input type="submit" value="Subcribe"> // Submit Button
</form>
</div>
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
<script>
var clientToken = document.getElementById('client_token').value;
// creating payment method nonse
braintree.setup(clientToken, "dropin", {
container: "payment-form"
});
</script>
How the Client look ?
When the user click on the submit button it will post the payment method nonce to server side.
Create Transcation
@login_required
@csrf_exempt
def payment(request):
if request.POST:
if request.POST.get("payment_method_nonce"):
// payment method nonce from client side.
nonce_from_the_client = request.POST.get("payment_method_nonce")
staff = Staff.objects.get(id=request.user.id)
sub = Subscription()
sub.staff = staff
sub.payment_nonce = nonce_from_the_client
sub.amount = request.POST.get("amount")
sub.save()
result = braintree.Transaction.sale({
"amount": sub.amount,
"payment_method_nonce": sub.payment_nonce
})
transaction_id = result.transaction.id
sub.txnid = transaction_id
sub.save()
message = ''
if result.is_success: // transaction success or not
sub.result = True
sub.save()
message = 'Transaction successfully completed'+' : '+ transaction_id
varibles ={'message':message}
return render(request, 'success.html',varibles)
else:
message = 'Error Transaction Faild'
varibles ={'message':message,}
return render(request, 'checkout.html',varibles)
else:
message = 'No transaction'
varibles ={'message':message,}
return render(request, 'checkout.html',varibles)
Transaction Results in my braintree account
Project code : braintree payment gateway demo project
Thank you.
wow!!!! this is amazing and very wonderful oppurtunity for me and really everyone..Vmware Training in Chennai
ReplyDeleteIt’s the best time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I want to suggest you few interesting things or suggestions.You can write next articles referring to this article. I desire to read even more things about it..
ReplyDeleteSAP Training in Chennai
SAP ABAP Training in Chennai
SAP FICO Training in Chennai
Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with
ReplyDeleteexcellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us.
Informatica Training in Chennai
wonderful post and very helpful, thanks for all this information. You are including better information regarding this topic in an effective way.Thank you so much
ReplyDeleteinformatica training in chennai
site is move to www.djangocrew.com
ReplyDeleteNICE ONE...CONTINUE AS IT IS..
ReplyDeleteIsoft Innovations Facebook | Isoft innovations faq | Isoft Innovations About Us | Isoft Innovations Address | Isoft Innovations Contact Us |isoft innovations Software Company | Isoft Innovations Chennai | Isoft Innovations Employees | Isoft Innovations Careers | Isoft Innovations Consulting Services
Great explanation! Keep up!
ReplyDeleteI was wondering if there is a way to implement Bank transactions using braintree. Iam in India
ReplyDeleteIts quite impressive post please continue posting like this and find the links to be updated relevant to best interior designss if interested from:
ReplyDeleteBest Architects in India
Turnkey Interior Contractors in Chennai
Architecture Firms in Chennai
Warehouse Architect
Factory Architect Chennai
Office Interiors in Chennai
Rainwater Harvesting chennai
Wonderful bloggers like yourself who would positively reply encouraged me to be more open and engaging in commenting.So know it's helpful.
ReplyDeleteData Science course in Indira nagar
Data Science course in marathahalli
Data Science Interview questions and answers
Data science training in tambaram | Data Science Course in Chennai
Data Science course in btm layout | Data Science training in Bangalore
Data science course in kalyan nagar | Data Science Course in Bangalore
Excellent blog, I wish to share your post with my folks circle. It’s really helped me a lot, so keep sharing post like this
ReplyDeleteangularjs-Training in pune
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in chennai
automation anywhere online Training
angularjs interview questions and answers
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
ReplyDeleteDevops Training in Bangalore
Best Devops Training in pune
Microsoft azure training in Bangalore
Power bi training in Chennai
I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone.
ReplyDeleteData science course in bangalore
Very nice post here and thanks for it .I always like and such a super contents of these post.
ReplyDeleteExcellent and very cool idea and great content of different kinds of the valuable information's.
Java training in Bangalore
Thanks For sharing Your information The Information Shared Is Very Valuable Please Keep updating Us Time Just Went On Redaing The Article Python Online Course Devops Online Course Data Science Online Course Aws Science Online Course
ReplyDeleteI went through your blog its really interesting and holds an informative content. Thanks for uploading such a wonderful blog.Green Pen | Digital payment integration | Online Educational Services | Web Data Extraction Services
ReplyDelete
ReplyDeleteI enjoy what you guys are usually up too sort of clever work and coverage! Keep up the wonderful works guys.
python training in chennai |python course in chennai
This comment has been removed by the author.
ReplyDeleteAttractive, post. I just stumbled upon your weblog and wanted to say that I have liked browsing your blog posts. After all, I will surely subscribe to your feed, and I hope you will write again soon! 정보이용료 현금화
ReplyDeleteThanks for sharing and keep updating
ReplyDeleteDigital Marketing Company in Chennai
eCommerce Development Company
ERP Software Development Company in Chennai
Free Appointment Scheduling Software
Payments Gateway in Qatar thus becoming an important tool in the progress of online transactions or another form of electronic transactions. There are always security concerns when a transaction is done online.
ReplyDeleteI really enjoy the blog.Much thanks again. Really Great.
ReplyDeleteVery informative article post. Really looking forward to read more. Will read on
java training in chennai
java training in annanagar
aws training in chennai
aws training in annanagar
python training in chennai
python training in annanagar
selenium training in chennai
selenium training in annanagar
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletedata science training in chennai
data science training in tambaram
android training in chennai
android training in tambaram
devops training in chennai
devops training in tambaram
artificial intelligence training in chennai
artificial intelligence training in tambaram
It’s the best time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I want to suggest you few interesting things or suggestions.You can write next articles referring to this article. I desire to read even more things about it..
ReplyDeletejava training in chennai
java training in omr
aws training in chennai
aws training in omr
python training in chennai
python training in omr
selenium training in chennai
selenium training in omr
I read over your blog, and I found it inquisitive. Also, you may also find Django Development Company Interested.
ReplyDeleteThanks for the post. It was very interesting and meaningful. I really appreciate it! Keep updating stuff like this.
ReplyDeletePython
Data Science
Selenium
ETL Testing
AWS
Reading this blog is really good, and it was very effective it delivers good informative content. I have shared this on my social media as well share these information with my friends. We have got good knowledge.
ReplyDeletePython course In Bangalore
Good informative content and it was worthy content to share with my friends and colleagues. Information was delivered perfectly with the simple sentence. This blog makes my day good. Keep up the good work.
ReplyDeleteiOS Course in Bangalore
Good informative content and it was worthy content to share with my friends and colleagues. Information was delivered perfectly with the simple sentence. This blog makes my day good. Keep up the good work.
ReplyDeleteAdvance Excel training In Bangalore
Hello! This post couldn't be composed any better! Perusing this post helps me to remember my past flat mate! He constantly continued lecturing about this. I assuredly will send this data to him. Genuinely certain he'll have an incredible perused. Much obliged for sharing!
ReplyDeleteDot Net training In Bangalore
Howdy, I do figure your blog could be having internet browser similarity issues. At the point when I take a gander at your site in Safari, it looks fine however when opening in IE, it makes them cross-over issues. I only needed to give you a fast watch out! Besides that, great blog!
ReplyDeleteC and C++ training In Bangalore
This post is so interactive and informative.keep update more information...
ReplyDeleteJava Training in Tambaram
java course in tambaram
It is imperative that we read blog post very carefully. I am already done it and find that this post is really amazing. White Label Payment Service Provider
ReplyDelete