Mr. TG
Mr. TG

Reputation: 99

PayPal Payment on codeigniter

Payment using PayPal goes through. however, I am finding it difficult to return the transaction id as well as the amount that was paid to my controller.

Here is my paypal code.

<script>
        // Render the PayPal button into #paypal-button-container
        paypal.Buttons({
    // Configure environment
            env: 'sandbox',
                client: {
                    sandbox: 'AZE8rjV6EqzKFAcEx6f7L7ZZwDsSLR5bBQOQN5pj3gAwghAivl0VUt-e0SkETWrcoesYXGbxO292vYZ3',
                    production: 'AUO3T8Mto5rojZl9Ff6nuYw1cLFjgF-4TPh6v1FhXyNdQsjxvdLTACn3xlv3EqwvPLJmmozl4_1pufFl'
                },

                // Set up the transaction
                createOrder: function(data, actions) {
                    var amount = $("#amount").val();
                    var name = '<?php echo($email); ?>';

                        return actions.order.create({
              redirect_urls:{
                   return_url:'http://localhost/escort/success'
              },
                                purchase_units: [{
                                        amount: {
                                                value: amount,
                                                currency: 'USD'
                                        }
                                }]
                        });
                },

                // Finalize the transaction
                onApprove: function(data, actions) {

                    return actions.request.post('Home/AddPayment/', {
                    paymentID: data.paymentID,
                    payerID:   data.payerID,
                    email: email,
                    amount: amount
                        })
                        .then(function(response) {
          // 3. Show the buyer a confirmation message.
                    if (response.error == 'false') {
                        console.log('Payment Completed');
                    }
                    else{
                        console.log('Error');
                    }
        });
                }                       

        }).render('#paypal-button-container');
</script>

My question is simple. I need to figure out a way to send transaction Id and amount to the controller after payment has been confirmed.

Upvotes: 2

Views: 757

Answers (1)

Preston PHX
Preston PHX

Reputation: 30402

Do so in the onApprove function. It seems you're already posting a paymentID and amount, but if you want to capture first, you need to actions.order.capture(). See the demo pattern at https://developer.paypal.com/demo/checkout/#/pattern/client


A better, server-side design, would be to have the onApprove function call a server endpoint that executes a server-side API call to capture the transaction. See the demo pattern at https://developer.paypal.com/demo/checkout/#/pattern/server

Upvotes: 2

Related Questions