Proweb
Proweb

Reputation: 1

PAYPAL PAYMENT Client Side REST SCRIPT

I have integerated PayPal Client Side REST to my website. I have used the sample code provided from link below: https://developer.paypal.com/demo/checkout/#/pattern/client

This code below worked over a month, however today it shows below error Error: Request to post https://www.sandbox.paypal.com/v1/payments/payment

failed with 400 error

{
    "name": "MALFORMED_REQUEST",
    "message": "Incoming JSON request does not map to API request",
    "information_link": "https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST",
    "debug_id": "a26904ff6211a"
}

my code follows:

 <div id="paypal-button-container" class="info"></div><script src="https://www.paypalobjects.com/api/checkout.js"></script>
<script>

    // Render the PayPal button

    paypal.Button.render({

        // Set your environment

        env: 'production', // sandbox | production

        // PayPal Client IDs - replace with your own
        // Create a PayPal app: https://developer.paypal.com/developer/applications/create

        client: {
            sandbox:    '<?=SANDBOXPAYPAL?>',
            production: '<?=PAYPAL_TOKEN?>'
        },

        // Set to 'Pay Now'

        commit: true,

        // Wait for the PayPal button to be clicked

        payment: function() {
$('#card').attr('checked',true);
            // Make a client-side call to the REST api to create the payment


            return paypal.rest.payment.create(this.props.env, this.props.client, {
                transactions: [
                    {
                        amount: { total: '12.99', currency: 'GBP' }
                    }
                ]
            });
        },

        // Wait for the payment to be authorized by the customer

        onAuthorize: function(data, actions) {

           jQuery.ajax({
           type: "POST",
           url: "ipn.php",
           data: data,
           success: function(data){ 
           }
         });

            return actions.payment.execute().then(function() {
                document.querySelector('#paypal-button-container').innerText = 'Payment Complete!';
            });
        }

    }, '#paypal-button-container');

</script>

Upvotes: -1

Views: 1214

Answers (1)

z900collector
z900collector

Reputation: 1134

Your code does not quite match the example, but I am also using the client-sdie API from a Laravel Shoping Cart I am building, I use AJAX to push the payment data back to my system this way:

onAuthorize:function(data, actions)
    {
        console.log("onAuthorize()");
        console.log("Dumping DATA");
        console.log(data);
        console.log("Dumping ACTIONS");
        console.log(actions);
        return actions.payment.execute().then(function(payment)
        {
            console.log("payment.execute called");
            document.querySelector('#paypal-button-container').innerText = 'Payment Complete!';
            console.log("Dumping payment:");
            console.log("CART: "+payment['cart']);
            console.log(payment);

            var values = encodeURIComponent(JSON.stringify(payment));
            $.ajaxSetup({headers:{'X-CSRF-TOKEN':'{{ $token }}' } } );
            ajaxRequest= $.ajax({ url: "/ajax/payment", type: "post",data:values });
            ajaxRequest.done(function(response, textStatus, jqXHR)
            {
                var result = $.parseJSON(response);
                console.log(result);
            });

        });
    },

/ajax/payment is my capture script i'm developing at present but it does record all the data from the purchase.. hope it helps.

Upvotes: 1

Related Questions