cancel
Showing results for 
Search instead for 
Did you mean: 

Get Paypal transaction ID to post to form from OnApprove

Options
KieranL33
Contributor
Posted on

I have a paypal transaction set up with a smart button and I would like on approval following a redirect for the payment ID to be posted into my registration form. I can get it to redirect after the payment successfully but I cannot seem to get it to post the ID into the 'invoice' field.

Here is my working so far -

<script>    function initPayPalButton() {
      paypal.Buttons({
        style: {
          shape: 'rect',
          color: 'black',
          layout: 'vertical',
          label: 'buynow',
          
        },

        createOrder: function(data, actions) {
          return actions.order.create({
            purchase_units: [{"amount":{"currency_code":"USD","value":1}}]
          });
        },

        onApprove: function(data, actions) {
          return actions.order.capture().then(function(details) {
            window.location.replace("https://www.mywebsite.com/register");
            document.getElementById("invoice").value=details.id;
          });
        },

        onError: function(err) {
          console.log(err);
        }
      }).render('#paypal-button-container');
    }
    initPayPalButton();
  </script>

And then my forms.py is set up as follows -

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()
    invoice = forms.CharField(max_length=50, widget= forms.TextInput(attrs={'id':'invoice'}))

    class Meta:
        model = User
        fields = ['email', 'invoice', 'password1', 'password2']

I should mention that the form is on the register page that I am redirecting to.

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.