How to allow users to pay with paypal each other in Django

536 Views Asked by At

I was following This and ended up with users can pay me and get their product, however, I want users to pay each other.

Both users should enter their email address linked with their PayPal, now how can I make the transaction transfer money to other user email address instead of me? My html

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<scriptsrc="https://www.paypal.com/sdk/js?client-id=MY_SANDBOX_CLIENT_ID"></script>

<h1>you are about to pay {{ trade.price }}</h1>
<div id="paypal-button-container"></div>

<script>
            paypal.Buttons({
                createOrder: function (data, actions) {
                    return actions.order.create({
                        purchase_units: [{
                            amount: {
                                value: '{{trade.price}}'
                            }
                        }]
                    });
                },
                onApprove: function (data, actions) {
                    return actions.order.capture().then(function (details) {
                        $.ajax({
                            url: "{% url 'payment:pay_trade' trade.id %}",
                            data: {
                                csrfmiddlewaretoken: "{{ csrf_token }}"
                            },
                            method: 'POST',
                            success: function (response) {
                                alert('Successfully paid')
                            },
                            error: function (response) {
                                alert('something went wrong')
                            }
                        })
                    });
                }
            }).render('#paypal-button-container');
        </script>

The function:

def pay_trade(request, trade_id):
    trade = get_object_or_404(Trade, pk=trade_id)
    if request.method == 'GET':
        return render(request, 'payment/pay_trade.html', {'trade': trade})
    else:
        # Give something to the buyer
        # No idea how to check if the transaction was made but i hope so ;-;
          return JsonResponse({})

Also, it would be great if someone can help me to check if the payment is done before making the function, thanks!

1

There are 1 best solutions below

0
On

If you want to check if the payment is done, you should switch to a proper server-side integration of the v2/orders/checkout API, with or without the Checkout-Python-SDK.

You'll need two routes, one for 'Set Up Transaction' and another for 'Create Transaction', documented here: https://developer.paypal.com/docs/checkout/reference/server-integration/

Then change your button code to call those 2 routes: https://developer.paypal.com/demo/checkout/#/pattern/server


For users paying each other, add a payee object: https://developer.paypal.com/docs/checkout/integration-features/pay-another-account/