title: Updating django database with paypal transaction details
body: <p>I have a django project that takes payments and I need to update my database with a new owed balance of 0 after the paypal transaction is complete. I'm a newbie to javascript and the paypal docs aren't being helpful. here's my code:</p>
  
  <p>I need to create a new payment from this and update the invoice with the correct amount owed.</p>
  
  <p><a href="https://i.stack.imgur.com/Ke6iA.png" rel="nofollow noreferrer">This is the payment page where the payment happens, make_payment.html. Payments go through successfully.</a></p>
  
  <pre><code>{% extends "base.html" %}
  {% load crispy_forms_tags %}
  {% block content %}
  
  
      &lt;div class="card card-primary"&gt;
          &lt;div class="card-header"&gt;
              &lt;h2&gt;Make a Payment&lt;/h2&gt;
          &lt;/div&gt;
          &lt;div class="float-right list-group-item"&gt;
              &lt;p class="font-weight-bold float-left p-0 m-0"&gt;Invoice Number&lt;/p&gt;
              &lt;p class="font-weight-bold float-right p-0 m-0"&gt;{{ object.pk }}&lt;/p&gt;
          &lt;/div&gt;
  
          &lt;div class="float-right list-group-item"&gt;
              &lt;p class="font-weight-bold float-left p-0 m-0"&gt;Amount Billed&lt;/p&gt;
              &lt;p class="font-weight-bold float-right p-0 m-0"&gt;${{ object.amount_billed }}&lt;/p&gt;
          &lt;/div&gt;
  
          &lt;div class="float-right list-group-item"&gt;
              &lt;p class="font-weight-bold float-left p-0 m-0"&gt;Amount Owed&lt;/p&gt;
              &lt;p class="font-weight-bold float-right p-0 m-0"&gt;${{ object.amount_owed }}&lt;/p&gt;
          &lt;/div&gt;
  
  
  
          &lt;div class="float-right list-group-item"&gt;
              &lt;!-- Set up a container element for the button --&gt;
              &lt;div id="paypal-button-container"&gt;&lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="float-right list-group-item"&gt;
              &lt;a href="{% url 'invoice-list'%}"&gt;&lt;button type="button" class="btn btn-primary float-right"&gt;Go back&lt;/button&gt;&lt;/a&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
      &lt;!-- Include the PayPal JavaScript SDK --&gt;
      &lt;script src="https://www.paypal.com/sdk/js?client-id=AeFSJDq8sonOdMT62SM-B040Eo4YWi6IS6xsPqDe-eamtEbGs9Jtbf5AbtwjnPC45LjFPOCa4sNoHEIt&amp;currency=USD&amp;disable-funding=credit&amp;commit=false"&gt;&lt;/script&gt;
  
      &lt;script&gt;
          // 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');
      &lt;/script&gt;
  
  
  {% endblock content %}
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/tZZVt.png" rel="nofollow noreferrer">This is the invoice screen where I'll need the updated amount owed after a payment, this is invoice_list.html</a></p>
  
  <pre><code>    {% extends "base.html" %}
  
  {% block content %}
  
  
  
      &lt;div class="card p-4 float"&gt;
          &lt;div class="card-header mb-3"&gt;&lt;h3&gt;Invoices - {{ user.get_full_name }}&lt;/h3&gt;&lt;/div&gt;
  
  
  
                  &lt;div class="card"&gt;
  
  
                      &lt;div class="list-group"&gt;
                          &lt;div  class="list-group-item list-group-item-action active disabled"&gt;
                              &lt;p class="float-left"&gt;Invoice Number&lt;/p&gt;
                              &lt;p class="float-right"&gt;Amount Owed&lt;/p&gt;
  
                          &lt;/div&gt;
                      {% for invoice in user.invoice_set.all %}
                          &lt;a href="{% url 'make-payment' pk=invoice.pk %}" class="list-group-item list-group-item-action
                                      {% if invoice.amount_owed &lt;= 0 %} disabled {% endif %}"&gt;
                          &lt;p class="float-left"&gt;{{ invoice.pk}}&lt;/p&gt;
                          &lt;p class="float-right"&gt;${{ invoice.amount_owed }}&lt;/p&gt;
                          &lt;/a&gt;
                      {% endfor %}
  
                      &lt;/div&gt;
  
  
                  &lt;/div&gt;
  
  
  
      &lt;/div&gt;
  
  {% endblock content %}
  </code></pre>
  
  <p>This is models.py for the payments application</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>views.py</p>
  
  <pre><code>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'
  </code></pre>