How to set up Google Pay for my django online ecommerce website?

2.1k views Asked by At

I have an online e-commerce website where the user can trade images with each other What I'm trying to achieve is: seller requests the buyer to give him money, the buyer clicks on the "accept payment" and then set up payment with his Google Pay account, finally the seller will get charged in his Google Pay account

How can I do so? I'm using Django 3.1

Here is my Trade model

class Trade(models.Model):
    seller = models.ForeignKey(
        User, related_name='seller', on_delete=models.CASCADE)
    buyer = models.ForeignKey(
        User, related_name='buyer', on_delete=models.CASCADE)
    price = models.IntegerField()
    date = models.DateTimeField(auto_now_add=True)

Here is the "Accept_trade" view

def accept_trade(request, pk):
    trade = get_object_or_404(Trade, pk=pk)
    if request.method == 'GET':
        return render(request, 'trade/accept_trade.html', {'trade': trade})
    else:
        # PAY
        payment = Payment.objects.create(
            variant='default',
            description= 'A trade',
            total=Decimal(trade.price),
            tax=Decimal(0),
            currency='USD',
            delivery=Decimal(0),
            billing_first_name=trade.buyer.first_name,
            billing_last_name=trade.buyer.last_name,
            customer_ip_address='127.0.0.1'
        )
        payment.save()
        payment.capture()
        payment.release()
        return redirect('home')

my accept_trade.html

{% extends 'users/base.html' %}

{% block title %}Accept trade | {% endblock title %}


{% block content %}
<div class="shadow-lg p-3 mb-5 col-md-5 bg-white rounded float-right m-4 p-4">
    <p>Seller: {{ trade.seller.username }}</p>
    <p style="color: #2ecc71;">Price: ${{ trade.price }}</p>
</div>
<form action="{% url 'trade:accept_trade' trade.id %}" method="post" class="m-4">
    {% csrf_token%}
    <button type="submit" class="btn btn-success btn-lg" style="padding:10px 30px; border-radius: 200px; margin: 30px; letter-spacing: 1px; word-spacing: 4px;">Pay and get product</button>
</form>
{% endblock content %}
1

There are 1 answers

4
wind On

You can use https://django-payments.readthedocs.io/en/latest/ to do this. So, for example, you could do:

from decimal import Decimal

from payments import get_payment_model

Payment = get_payment_model()
payment = Payment.objects.create(
    variant='default',  # this is the variant from PAYMENT_VARIANTS
    description='Book purchase',
    total=Decimal(120),
    tax=Decimal(20),
    currency='USD',
    delivery=Decimal(10),
    billing_first_name='Sherlock',
    billing_last_name='Holmes',
    billing_address_1='221B Baker Street',
    billing_address_2='',
    billing_city='London',
    billing_postcode='NW1 6XE',
    billing_country_code='UK',
    billing_country_area='Greater London',
    customer_ip_address='127.0.0.1')

# https://django-payments.readthedocs.io/en/latest/usage.html#making-a-payment