使用paypal交易详细信息更新Django数据库

2024-04-20 13:23:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个django项目需要付款,我需要在paypal交易完成后用新的欠款余额0更新我的数据库。我是javascript的新手,paypal文档没有任何帮助。这是我的密码:

我需要在此基础上创建新的付款,并用正确的欠款金额更新发票

This is the payment page where the payment happens, make_payment.html. Payments go through successfully.

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}


    <div class="card card-primary">
        <div class="card-header">
            <h2>Make a Payment</h2>
        </div>
        <div class="float-right list-group-item">
            <p class="font-weight-bold float-left p-0 m-0">Invoice Number</p>
            <p class="font-weight-bold float-right p-0 m-0">{{ object.pk }}</p>
        </div>

        <div class="float-right list-group-item">
            <p class="font-weight-bold float-left p-0 m-0">Amount Billed</p>
            <p class="font-weight-bold float-right p-0 m-0">${{ object.amount_billed }}</p>
        </div>

        <div class="float-right list-group-item">
            <p class="font-weight-bold float-left p-0 m-0">Amount Owed</p>
            <p class="font-weight-bold float-right p-0 m-0">${{ object.amount_owed }}</p>
        </div>



        <div class="float-right list-group-item">
            <!-- Set up a container element for the button -->
            <div id="paypal-button-container"></div>
        </div>

        <div class="float-right list-group-item">
            <a href="{% url 'invoice-list'%}"><button type="button" class="btn btn-primary float-right">Go back</button></a>
        </div>

    </div>
    <!-- Include the PayPal JavaScript SDK -->
    <script src="https://www.paypal.com/sdk/js?client-id=AeFSJDq8sonOdMT62SM-B040Eo4YWi6IS6xsPqDe-eamtEbGs9Jtbf5AbtwjnPC45LjFPOCa4sNoHEIt&currency=USD&disable-funding=credit&commit=false"></script>

    <script>
        // Render the PayPal button into #paypal-button-container

        paypal.Buttons({

            // Set up the transaction
            createOrder: function (data, actions) {
                return actions.order.create({
                    purchase_units: [{
                        amount: {
                            value: {{ object.amount_owed_as_string }}
                        }
                    }]
                });
            },

            // Finalize the transaction
            onApprove: function (data, actions) {
                return actions.order.capture().then(function (details) {
                    // Show a success message to the buyer

                    alert('Transaction completed by ' + details.payer.name.given_name + '!');

                });

            }


        }).render('#paypal-button-container');
    </script>


{% endblock content %}

This is the invoice screen where I'll need the updated amount owed after a payment, this is invoice_list.html

    {% extends "base.html" %}

{% block content %}



    <div class="card p-4 float">
        <div class="card-header mb-3"><h3>Invoices - {{ user.get_full_name }}</h3></div>



                <div class="card">


                    <div class="list-group">
                        <div  class="list-group-item list-group-item-action active disabled">
                            <p class="float-left">Invoice Number</p>
                            <p class="float-right">Amount Owed</p>

                        </div>
                    {% for invoice in user.invoice_set.all %}
                        <a href="{% url 'make-payment' pk=invoice.pk %}" class="list-group-item list-group-item-action
                                    {% if invoice.amount_owed <= 0 %} disabled {% endif %}">
                        <p class="float-left">{{ invoice.pk}}</p>
                        <p class="float-right">${{ invoice.amount_owed }}</p>
                        </a>
                    {% endfor %}

                    </div>


                </div>



    </div>

{% endblock content %}

这是付款应用程序的models.py

from django.db import models
from django.contrib.auth.models import User
from django.contrib.admin.utils import timezone





class Invoice(models.Model):
    # invoice number is it's primary key
    patient = models.ForeignKey(User, on_delete=models.CASCADE)
    amount_owed = models.DecimalField(decimal_places=2, max_digits=8)
    amount_billed = models.DecimalField(decimal_places=2, max_digits=8)
    date_billed = models.DateField(default=timezone.now)


    def amount_owed_as_string(self):
        return str(self.amount_owed)

    def __str__(self):
        return str(self.pk) + " - " + str(self.amount_billed)


class Payment(models.Model):
    payment_method = models.CharField(max_length=15)
    invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE)
    payment_amount = models.DecimalField(decimal_places=2, max_digits=8)

    def __str__(self):
        return str(self.invoice.pk) + " - " + str(self.payment_amount)

views.py

from django.views.generic import ListView, DetailView

from .models import Payment, Invoice


class PaymentList(ListView):
    model = Payment
    paginate_by = 15
    ordering = ['payment_amount']
    template_name = 'payment_list.html'



class MakePayment(DetailView):
    model = Invoice
    template_name = 'make_payment.html'



class InvoiceList(ListView):
    model = Invoice
    paginate_by = 15
    ordering = ['date_billed']
    template_name = 'invoice_list.html'

Tags: theselfrightdivmodelsgroupbuttoninvoice
1条回答
网友
1楼 · 发布于 2024-04-20 13:23:16
        onApprove: function (data, actions) {
            return actions.order.capture().then(function (details) {
                // Show a success message to the buyer

                alert('Transaction completed by ' + details.payer.name.given_name + '!');

            });

        }

这就是您需要添加自己的JS代码的地方

  1. 向买家显示一条消息(使用document.getElementbyId或类似工具)以更新他们刚刚做过的事情的状态
  2. 与后端通信(使用fetch()或类似工具,并记录新交易、“更新任何欠款”或其他内容)

请注意,这是一个不安全的、仅客户端的付款创建和捕获设计

对于安全的解决方案,您需要从服务器调用v2/orders API。下面是一个使用该模式的前端,通过fetch()与服务器上的路由进行通信(您需要实现该路由,然后调用PayPal v2/orders API)https://developer.paypal.com/demo/checkout/#/pattern/server

相关问题 更多 >