Submit form from Paypal script after approved payment in Django

I have a user registration form and I would like the user to pay in order to register for my app. I have the frontend paypal set up for the payment as well as the user registration with a submit button but I do not know how to integrate the two.

<div id="paypal-button-container"></div>

   <script src=""></script>

        // Render the PayPal button into #paypal-button-container

            // Set up the transaction
            createOrder: function(data, actions) {
                return actions.order.create({
                    purchase_units: [{
                        amount: {
                            value: '00.01'

            // 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 ' + + '!');


    <button class="w3-button w3-blue w3-padding-large" type="submit">
        Buy Now

I would like the submit button to be used in the ‘onapprove’ part of the script but I am not sure how. Alternatively I was thinking of calling my ‘buy’ function from the views

def buy(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            data = form.cleaned_data
            email = f"{data['email']}"
            username = f"{data['username']}"
            form.instance.username = username
            return redirect('login')
        form = UserRegisterForm()
    return render(request, 'users/buy.html', {'form': form})

The other thing is that the system would have to check that the form inputs are unique and valid before payment. Would it just be best to have the redirect the user to the registration page following a successful payment?

class UserRegisterForm(UserCreationForm):
    email = forms.EmailField()
    username = forms.CharField(max_length=20)

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


Please share your approach :slight_smile: