【问题标题】:Updating django database with paypal transaction details使用贝宝交易详细信息更新 django 数据库
【发布时间】:2020-08-08 13:47:13
【问题描述】:

我有一个接受付款的 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'

【问题讨论】:

    标签: javascript python django database paypal


    【解决方案1】:
            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

    【讨论】:

      猜你喜欢
      • 2020-06-08
      • 2013-08-11
      • 2011-02-18
      • 1970-01-01
      • 2018-04-03
      • 2015-03-04
      • 2013-02-23
      • 2021-11-01
      • 2012-08-31
      相关资源
      最近更新 更多