0

I'm implementing PayPal payments in my website.

The Paypal API sends me the order details through JSON to my success.php script.

I want to get each variable sent and store it in my database.

So I get the JSON data with :

$RAW=file_get_contents('php://input');

The data I get looks like this (print_r of $RAW) :

{"id":"XXXXXXXXXXXXX-XXXXXXXXX","event_version":"1.0","create_time":"2019-05-30T09:58:56.756Z","resource_type":"capture","resource_version":"2.0","event_type":"PAYMENT.CAPTURE.COMPLETED","summary":"Payment completed for EUR 10.0 EUR","resource":{"id":"XXXXXXXXXXXXXXXXXXX","amount":{"currency_code":"EUR","value":"10.00"},"final_capture":true,"seller_protection":{"status":"ELIGIBLE","dispute_categories":["ITEM_NOT_RECEIVED","UNAUTHORIZED_TRANSACTION"]},"seller_receivable_breakdown":{"gross_amount":{"currency_code":"EUR","value":"10.00"},"paypal_fee":{"currency_code":"EUR","value":"0.59"},"net_amount":{"currency_code":"EUR","value":"9.41"}},"status":"COMPLETED","create_time":"2019-05-30T09:58:52Z","update_time":"2019-05-30T09:58:52Z","links":[{"href":"https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXXXXX","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXXX/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/XXXXXXXXXXXXXX","rel":"up","method":"GET"}]},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXXXXXX-XXXXXXXXXXX","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX/resend","rel":"resend","method":"POST"}]}

I decode it in an Array with :

$response=json_decode($RAW, true);

I get this array (print_r of $response) :

Array
(
    [id] => XXXXXXXXXXX-XXXXXXXXXX
    [event_version] => 1.0
    [create_time] => 2019-05-30T09:58:56.756Z
    [resource_type] => capture
    [resource_version] => 2.0
    [event_type] => PAYMENT.CAPTURE.COMPLETED
    [summary] => Payment completed for EUR 10.0 EUR
    [resource] => Array
        (
            [id] => XXXXXXXXXXXXX
            [amount] => Array
                (
                    [currency_code] => EUR
                    [value] => 10.00
                )

            [final_capture] => 1
            [seller_protection] => Array
                (
                    [status] => ELIGIBLE
                    [dispute_categories] => Array
                        (
                            [0] => ITEM_NOT_RECEIVED
                            [1] => UNAUTHORIZED_TRANSACTION
                        )

                )

            [seller_receivable_breakdown] => Array
                (
                    [gross_amount] => Array
                        (
                            [currency_code] => EUR
                            [value] => 10.00
                        )

                    [paypal_fee] => Array
                        (
                            [currency_code] => EUR
                            [value] => 0.59
                        )

                    [net_amount] => Array
                        (
                            [currency_code] => EUR
                            [value] => 9.41
                        )

                )

            [status] => COMPLETED
            [create_time] => 2019-05-30T09:58:52Z
            [update_time] => 2019-05-30T09:58:52Z
            [links] => Array
                (
                    [0] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXX
                            [rel] => self
                            [method] => GET
                        )

                    [1] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXX/refund
                            [rel] => refund
                            [method] => POST
                        )

                    [2] => Array
                        (
                            [href] => https://api.sandbox.paypal.com/v2/checkout/orders/XXXXXXXXXXXXXXX
                            [rel] => up
                            [method] => GET
                        )

                )

        )
    [links] => Array
        (
            [0] => Array
                (
                    [href] => https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXX-XXXXXX
                    [rel] => self
                    [method] => GET
                )

            [1] => Array
                (
                    [href] => https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXX-XXXXXXXXX/resend
                    [rel] => resend
                    [method] => POST
                )

        )

)

But when I try to insert it in the database :

<?php
$RAW=file_get_contents('php://input');
$response=json_decode($RAW, true);

$sqluser="xxxxxxx";
$sqlpass="xxxx";
$database="xxxxxxxx";

$query="INSERT INTO testpaypal(

        webhookId,
        event_version,
        create_time,
        resource_type,
        resource_version,
        event_type,
        summary,
        resource__id,
        resource__amount__currency_code,
        resource__amount__value,
        resource__final_capture,
        resource__seller_protection__status,
        resource__seller_protection__dispute_categories__1,
        resource__seller_protection__dispute_categories__2,
        resource__seller_receivable__gross_amount__currency_code,
        resource__seller_receivable__gross_amount__value,
        resource__seller_receivable__paypal_fee__currency_code,
        resource__seller_receivable__paypal_fee__value,
        resource__seller_receivable__net_amount__currency_code,
        resource__seller_receivable__net_amount__value,
        resource__status,
        resource__create_time,
        resource__update_time,
        resource__links__href1,
        resource__links__rel1,
        resource__links__method1,
        resource__links__href2,
        resource__links__rel2,
        resource__links__method2,
        resource__links__href3,
        resource__links__rel3,
        resource__links__method3,
        links__href1,
        links__rel1,
        links__method1,
        links__href2,
        links__rel2,
        links__method2
                                        )
                                VALUES(
        $response[id],
        $response[event_version],
        $response[create_time],
        $response[resource_type],
        $response[resource_version],
        $response[event_type],
        $response[summary],
        $response[resource][id],
        $response[resource][amount][currency_code],
        $response[resource][amount][value],
        $response[resource][final_capture],
        $response[resource][seller_protection][status],
        $response[resource][seller_protection][dispute_categories][0],
        $response[resource][seller_protection][dispute_categories][1],
        $response[resource][seller_receivable_breakdown][gross_amount][currency_code],
        $response[resource][seller_receivable_breakdown][gross_amount][value],
        $response[resource][seller_receivable_breakdown][paypal_fee][currency_code],
        $response[resource][seller_receivable_breakdown][paypal_fee][value],
        $response[resource][seller_receivable_breakdown][net_amount][currency_code],
        $response[resource][seller_receivable_breakdown][net_amount][value],
        $response[resource][status],
        $response[resource][create_time],
        $response[resource][update_time],
        $response[resource][links][0][href],
        $response[resource][links][0][rel],
        $response[resource][links][0][method],
        $response[resource][links][1][href],
        $response[resource][links][1][rel],
        $response[resource][links][1][method],
        $response[resource][links][2][href],
        $response[resource][links][2][rel],
        $response[resource][links][2][method],
        $response[links][0][href],
        $response[links][0][rel],
        $response[links][0][method],
        $response[links][1]href],
        $response[links][1][rel],
        $response[links][1][method]
                                        );";

try { $db = new PDO("mysql:host=sqlserver;dbname=$database", $sqluser, $sqlpass); }
catch(Exception $e) { echo 'Error : '.$e->getMessage().''; echo 'N° : '.$e->getCode(); }

$result = $db->query($query);

file_put_contents("post.log", print_r($_POST, true));
file_put_contents("get.log", print_r($_GET, true));
file_put_contents("RAW.log", print_r($RAW, true));
file_put_contents("array.log", print_r($response, true));

?>

I get this warning :

log PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 57 PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 58 PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 59 PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 60 PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 61 [...] PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 87

And nothing is inserted.

What do i do wrong ?

Rahul
  • 18,271
  • 7
  • 41
  • 60
G7r7
  • 1
  • I would recommend looking at your database design, the idea of storing all of this data in one table isn't particularly good or flexible. – Nigel Ren May 30 '19 at 10:55
  • And literally for all values **quotes** are missing – Rahul May 30 '19 at 10:58
  • Every database (table) field has type and you must provide data in appropriate type to store it. You can't store string in number field or array in...pretty much any field. There's no "array" field type. – MilanG May 30 '19 at 10:58

2 Answers2

0

I think you should call your array elements this way:

$response['summary']

You are missing the quotes

Raúl Pérez López
  • 538
  • 1
  • 5
  • 14
  • https://stackoverflow.com/questions/4738850/interpolation-double-quoted-string-of-associative-arrays-in-php – Nigel Ren May 30 '19 at 10:58
  • PHP Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /xxxxxxxxxxxxxxxxx/success.php on line 51 – G7r7 May 30 '19 at 11:10
  • Then you have to ckeck your quotes of opening and closing the query string – Raúl Pérez López May 30 '19 at 11:56
  • Thank you that's it i was missing lot of quotes : $response['summary'] To this : '".$response['summary']."' Thank you for your time ! – G7r7 May 30 '19 at 12:05
0

I found my problem :

My query declaration was missing several dots, simple & doubles quotes :

$requete="INSERT INTO xxxxxxxxx.testpaypal(

        webhookId,
        event_version,
        create_time,
        resource_type,
        resource_version,
        event_type,
        summary,
        resource__id,
        resource__amount__currency_code,
        resource__amount__value,
        resource__final_capture,
        resource__seller_protection__status,
        resource__seller_protection__dispute_categories__1,
        resource__seller_protection__dispute_categories__2,
        resource__seller_receivable__gross_amount__currency_code,
        resource__seller_receivable__gross_amount__value,
        resource__seller_receivable__paypal_fee__currency_code,
        resource__seller_receivable__paypal_fee__value,
        resource__seller_receivable__net_amount__currency_code,
        resource__seller_receivable__net_amount__value,
        resource__status,
        resource__create_time,
        resource__update_time,
        resource__links__href1,
        resource__links__rel1,
        resource__links__method1,
        resource__links__href2,
        resource__links__rel2,
        resource__links__method2,
        resource__links__href3,
        resource__links__rel3,
        resource__links__method3,
        links__href1,
        links__rel1,
        links__method1,
        links__href2,
        links__rel2,
        links__method2
                                        )
                                VALUES(
        '".$response['id']."',
        '".$response['event_version']."',
        '".$response['create_time']."',
        '".$response['resource_type']."',
        '".$response['resource_version']."',
        '".$response['event_type']."',
        '".$response['summary']."',
        '".$response['resource']['id']."',
        '".$response['resource']['amount']['currency_code']."',
        '".$response['resource']['amount']['value']."',
        '".$response['resource']['final_capture']."',
        '".$response['resource']['seller_protection']['status']."',
        '".$response['resource']['seller_protection']['dispute_categories']['0']."',
        '".$response['resource']['seller_protection']['dispute_categories']['1']."',
        '".$response['resource']['seller_receivable_breakdown']['gross_amount']['currency_code']."',
        '".$response['resource']['seller_receivable_breakdown']['gross_amount']['value']."',
        '".$response['resource']['seller_receivable_breakdown']['paypal_fee']['currency_code']."',
        '".$response['resource']['seller_receivable_breakdown']['paypal_fee']['value']."',
        '".$response['resource']['seller_receivable_breakdown']['net_amount']['currency_code']."',
        '".$response['resource']['seller_receivable_breakdown']['net_amount']['value']."',
        '".$response['resource']['status']."',
        '".$response['resource']['create_time']."',
        '".$response['resource']['update_time']."',
        '".$response['resource']['links']['0']['href']."',
        '".$response['resource']['links']['0']['rel']."',
        '".$response['resource']['links']['0']['method']."',
        '".$response['resource']['links']['1']['href']."',
        '".$response['resource']['links']['1']['rel']."',
        '".$response['resource']['links']['1']['method']."',
        '".$response['resource']['links']['2']['href']."',
        '".$response['resource']['links']['2']['rel']."',
        '".$response['resource']['links']['2']['method']."',
        '".$response['links']['0']['href']."',
        '".$response['links']['0']['rel']."',
        '".$response['links']['0']['method']."',
        '".$response['links']['1']['href']."',
        '".$response['links']['1']['rel']."',
        '".$response['links']['1']['method']."'
  );";

Now it works, thanks for your help !

G7r7
  • 1