paypal / PayPal-PHP-SDK

PHP SDK for PayPal RESTful APIs
https://developer.paypal.com/docs/api/
Other
27 stars 100 forks source link

getFirstName() throws Notice #110

Closed sebastianhelbig closed 10 years ago

sebastianhelbig commented 10 years ago

After a paypal transaction has been approved I write the name and email of the buyer to our own db. But sometimes this fails:

$payment = Payment::get($row['payment_id'], $apiContext);
$execution = new PaymentExecution();
$execution->setPayerId($_GET['PayerID']);
$result = $payment->execute($execution, $apiContext);
...
$result->getPayer()->getPayerInfo()->getFirstName()
$result->getPayer()->getPayerInfo()->getLastName()
$result->getPayer()->getPayerInfo()->getEmail()

Error
Typ: Notice
Meldung: Undefined index: first_name
Datei: /var/www/washabich.de/inc/vendor/paypal/sdk-core-php/lib/PayPal/Common/PPModel.php
Zeile: 14

Trace: #0  ErrorHandler->handleError(8, Undefined index: first_name, /var/www/***.de/inc/vendor/paypal/sdk-core-php/lib/PayPal/Common/PPModel.php, 14, Array ([key] => first_name)) called at [/var/www/***.de/inc/class/MainController.php:140]
#1  errorHandler(8, Undefined index: first_name, /var/www/***.de/inc/vendor/paypal/sdk-core-php/lib/PayPal/Common/PPModel.php, 14, Array ([key] => first_name)) called at [/var/www/***.de/inc/vendor/paypal/sdk-core-php/lib/PayPal/Common/PPModel.php:14]
#2  PayPal\Common\PPModel->__get(first_name) called at [/var/www/***.de/inc/vendor/paypal/rest-api-sdk-php/lib/PayPal/Api/PayerInfo.php:69]
#3  PayPal\Api\PayerInfo->getFirstName() called at [/var/www/***.de/inc/class/SpendenController.php:129]
#4  SpendenController->perform(75b7a93a0aaa3c26) called at [/var/www/***.de/inc/class/MainController.php:786]
#5  MainController->perform() called at [/var/www/***.de/index.php:7]

Logfile:
PayPal\Core\PPHttpConnection: Connecting to https://api.paypal.com/v1/oauth2/token
PayPal\Core\PPHttpConnection: Payload grant_type=client_credentials
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3-7+squeeze19;bit=64;os=Linux_2.6.32-5-amd64;machine=x86_64;openssl=0.9.8o;curl=7.21.0)
PayPal\Core\PPHttpConnection: Adding header Authorization: Basic ***
PayPal\Core\PPHttpConnection: Adding header Accept: */*
PayPal\Core\PPHttpConnection: Connecting to https://api.paypal.com/v1/payments/payment
PayPal\Core\PPHttpConnection: Payload {"intent":"sale","payer":{"payment_method":"paypal"},"redirect_urls":{"return_url":"https:***&success=true","cancel_url":"https:***&success=false"},"transactions":[{"amount":{"currency":"EUR","total":"5.00","details":{"tax":"0.00","subtotal":"5.00"}},"item_list":{"items":[{"name":"Spende (***)","currency":"EUR","quantity":"1","price":"5.00"}]},"description":"Spende"}]}
PayPal\Core\PPHttpConnection: Adding header Content-Type: application/json
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3-7+squeeze19;bit=64;os=Linux_2.6.32-5-amd64;machine=x86_64;openssl=0.9.8o;curl=7.21.0)
PayPal\Core\PPHttpConnection: Adding header Authorization: Bearer***
PayPal\Core\PPHttpConnection: Adding header PayPal-Request-Id: ***
PayPal\Transport\PPRestCall: {"id":"PAY-***","create_time":"2014-09-06T17:39:15Z","update_time":"2014-09-06T17:39:15Z","state":"created","intent":"sale","payer":{"payment_method":"paypal","payer_info":{"shipping_address":{}}},"transactions":[{"amount":{"total":"5.00","currency":"EUR","details":{"subtotal":"5.00"}},"description":"Spende","item_list":{"items":[{"name":"Spende (***)","price":"5.00","currency":"EUR","quantity":"1"}]}}],"links":[{"href":"https://***","rel":"self","method":"GET"},{"href":"https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=***","rel":"approval_url","method":"REDIRECT"},{"href":"https://api.paypal.com/v1/payments/payment/PAY-***/execute","rel":"execute","method":"POST"}]}
PayPal\Core\PPHttpConnection: Connecting to https://api.paypal.com/v1/oauth2/token
PayPal\Core\PPHttpConnection: Payload grant_type=client_credentials
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3-7+squeeze19;bit=64;os=Linux_2.6.32-5-amd64;machine=x86_64;openssl=0.9.8o;curl=7.21.0)
PayPal\Core\PPHttpConnection: Adding header Authorization: Basic ***
PayPal\Core\PPHttpConnection: Adding header Accept: */*
PayPal\Core\PPHttpConnection: Connecting to https://api.paypal.com/v1/payments/payment/PAY-***
PayPal\Core\PPHttpConnection: Payload 
PayPal\Core\PPHttpConnection: Adding header Content-Type: application/json
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3-7+squeeze19;bit=64;os=Linux_2.6.32-5-amd64;machine=x86_64;openssl=0.9.8o;curl=7.21.0)
PayPal\Core\PPHttpConnection: Adding header Authorization: Bearer ***
PayPal\Transport\PPRestCall: {"id":"PAY-***","create_time":"2014-09-06T17:39:15Z","update_time":"2014-09-06T17:39:15Z","state":"created","intent":"sale","payer":{"payment_method":"paypal","status":"VERIFIED","payer_info":{"email":"***@***.**","first_name":"P***a","last_name":"S***r","payer_id":"***","shipping_address":{"line1":"V***1","city":"W***g","postal_code":"3***2","country_code":"DE","recipient_name":"P***r"}}},"transactions":[{"amount":{"total":"5.00","currency":"EUR","details":{"subtotal":"5.00"}},"description":"Spende","item_list":{"items":[{"name":"Spende (***)","price":"5.00","currency":"EUR","quantity":"1"}]}}],"links":[{"href":"https://api.paypal.com/v1/payments/payment/PAY-***","rel":"self","method":"GET"},{"href":"https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-***","rel":"approval_url","method":"REDIRECT"},{"href":"https://api.paypal.com/v1/payments/payment/PAY-***/execute","rel":"execute","method":"POST"}]}
PayPal\Core\PPHttpConnection: Connecting to https://api.paypal.com/v1/payments/payment/PAY-***/execute
PayPal\Core\PPHttpConnection: Payload {"payer_id":"***"}
PayPal\Core\PPHttpConnection: Adding header Content-Type: application/json
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3-7+squeeze19;bit=64;os=Linux_2.6.32-5-amd64;machine=x86_64;openssl=0.9.8o;curl=7.21.0)
PayPal\Core\PPHttpConnection: Adding header Authorization: Bearer ***
PayPal\Core\PPHttpConnection: Adding header PayPal-Request-Id: ***
PayPal\Transport\PPRestCall: {"id":"PAY-***","create_time":"2014-09-06T17:39:15Z","update_time":"2014-09-06T17:40:13Z","state":"approved","intent":"sale","payer":{"payment_method":"paypal","payer_info":{"shipping_address":{}}},"transactions":[{"amount":{"total":"5.00","currency":"EUR","details":{"subtotal":"5.00"}},"description":"Spende","item_list":{"items":[{"name":"Spende (***)","price":"5.00","currency":"EUR","quantity":"1"}],"shipping_address":{"recipient_name":""}},"related_resources":[{"sale":{"id":"99C31698TC163682X","create_time":"2014-09-06T17:39:15Z","update_time":"2014-09-06T17:40:13Z","amount":{"total":"5.00","currency":"EUR"},"payment_mode":"INSTANT_TRANSFER","state":"completed","protection_eligibility":"ELIGIBLE","protection_eligibility_type":"ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE","parent_payment":"PAY-***","links":[{"href":"https://api.paypal.com/v1/payments/sale/***","rel":"self","method":"GET"},{"href":"https://api.paypal.com/v1/payments/sale/***/refund","rel":"refund","method":"POST"},{"href":"https://api.paypal.com/v1/payments/payment/PAY-***","rel":"parent_payment","method":"GET"}]}}]}],"links":[{"href":"https://api.paypal.com/v1/payments/payment/PAY-***","rel":"self","method":"GET"}]}
jaypatel512 commented 10 years ago

Description

{
    "intent": "sale",
    "payer": {
        "payment_method": "credit_card",
        "funding_instruments": [
            {
                "credit_card": {
                    "type": "visa",
                    "number": "xxxxxxxxxxxx0331",
                    "expire_month": "11",
                    "expire_year": "2019",
                    "first_name": "Joe",
                    "last_name": "Shopper"
                }
            }
        ]
    },
    "transactions": [
        {
            "amount": {
                "total": "20.00",
                "currency": "USD",
                "details": {
                    "subtotal": "17.50",
                    "tax": "1.30",
                    "shipping": "1.20"
                }
            },
            "description": "Payment description",
            "item_list": {
                "items": [
                    {
                        "name": "Ground Coffee 40 oz",
                        "price": "7.50",
                        "currency": "USD",
                        "quantity": "1"
                    },
                    {
                        "name": "Granola bars",
                        "price": "2.00",
                        "currency": "USD",
                        "quantity": "5"
                    }
                ]
            },
            "related_resources": [
                {
                    "sale": {
                        "id": "8W754701YR607764V",
                        "create_time": "2014-09-17T16:53:22Z",
                        "update_time": "2014-09-17T16:53:43Z",
                        "amount": {
                            "total": "20.00",
                            "currency": "USD"
                        },
                        "state": "completed",
                        "parent_payment": "PAY-39861121J87096426KQM3ZAQ",
                        "links": [
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/sale/8W754701YR607764V",
                                "rel": "self",
                                "method": "GET"
                            },
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/sale/8W754701YR607764V/refund",
                                "rel": "refund",
                                "method": "POST"
                            },
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-39861121J87096426KQM3ZAQ",
                                "rel": "parent_payment",
                                "method": "GET"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "id": "PAY-39861121J87096426KQM3ZAQ",
    "create_time": "2014-09-17T16:53:22Z",
    "update_time": "2014-09-17T16:53:43Z",
    "state": "approved",
    "links": [
        {
            "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-39861121J87096426KQM3ZAQ",
            "rel": "self",
            "method": "GET"
        }
    ]
}
{
    "id": "PAY-4MA422039F6217725KQM32BY",
    "create_time": "2014-09-17T16:55:35Z",
    "update_time": "2014-09-17T16:55:48Z",
    "state": "approved",
    "intent": "sale",
    "payer": {
        "payment_method": "paypal",
        "payer_info": {
            "email": "sample@buy.com",
            "first_name": "SandboxTest",
            "last_name": "Account",
            "payer_id": "REABK2UGK7PLW",
            "shipping_address": {
                "line1": "1 Main St",
                "line2": "",
                "city": "San Jose",
                "state": "CA",
                "postal_code": "95131",
                "country_code": "US",
                "recipient_name": ""
            }
        }
    },
    "transactions": [
        {
            "amount": {
                "total": "20.00",
                "currency": "USD",
                "details": {
                    "subtotal": "17.50",
                    "tax": "1.30",
                    "shipping": "1.20"
                }
            },
            "description": "Payment description",
            "item_list": {
                "items": [
                    {
                        "name": "Ground Coffee 40 oz",
                        "price": "7.50",
                        "currency": "USD",
                        "quantity": "1"
                    },
                    {
                        "name": "Granola bars",
                        "price": "2.00",
                        "currency": "USD",
                        "quantity": "5"
                    }
                ],
                "shipping_address": {
                    "recipient_name": "",
                    "line1": "1 Main St",
                    "line2": "",
                    "city": "San Jose",
                    "state": "CA",
                    "postal_code": "95131",
                    "country_code": "US"
                }
            },
            "related_resources": [
                {
                    "sale": {
                        "id": "4UU45725DN228182R",
                        "create_time": "2014-09-17T16:55:35Z",
                        "update_time": "2014-09-17T16:55:48Z",
                        "amount": {
                            "total": "20.00",
                            "currency": "USD"
                        },
                        "payment_mode": "INSTANT_TRANSFER",
                        "state": "completed",
                        "protection_eligibility": "ELIGIBLE",
                        "protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE,UNAUTHORIZED_PAYMENT_ELIGIBLE",
                        "parent_payment": "PAY-4MA422039F6217725KQM32BY",
                        "links": [
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/sale/4UU45725DN228182R",
                                "rel": "self",
                                "method": "GET"
                            },
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/sale/4UU45725DN228182R/refund",
                                "rel": "refund",
                                "method": "POST"
                            },
                            {
                                "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-4MA422039F6217725KQM32BY",
                                "rel": "parent_payment",
                                "method": "GET"
                            }
                        ]
                    }
                }
            ]
        }
    ],
    "links": [
        {
            "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAY-4MA422039F6217725KQM32BY",
            "rel": "self",
            "method": "GET"
        }
    ]
}

Solution

P.S. I have used json_encode command instead of var_dump to echo the $result variables. This solves the issue with not showing complete information in var_dump

sebastianhelbig commented 10 years ago

So...no real solution for me as a sdk user...

jaypatel512 commented 10 years ago

The reason we may not have the first name available all the time for the Payer Info is because of the feature that allows anyone to checkout using a credit card without having a paypal account. This restricts us from having Payer information for all times.

Do you have a suggestion in mind, that could make it easier for you to integrate with your system ?

sebastianhelbig commented 10 years ago

The thing is, I just call $result->getPayer()->getPayerInfo()->getFirstName() I get a Payer Object and a PayerInfo Object and so I expect getFirstName() to -not- throw an error. An empty result would be entirely acceptable.

jaypatel512 commented 10 years ago

The thing is there is no PayerInfo object. So, that returns a null value. And you are trying to call getFirstName() method on a null value. Thats why it is causing such issue.

So basically, the issue is with the chained method invocation.

If we create an empty instance of PayerInfo even if it is not retrieved from API, it may cause a lot of confusion to all developers.

sebastianhelbig commented 10 years ago

Okay, to clarify this: I have no problems if -my- code throws errors, as i can precheck this as you said. But(!) if you check the trace in my first post, you can clearly see, that in this case there was a PayerInfo Object, which called PPModel which throwed the notice. So the problem is in the sdk.

Trace: #0  ErrorHandler->handleError(8, Undefined index: first_name, /var/www/***.de/inc/vendor/paypal/sdk-core-php/lib/PayPal/Common/PPModel.php, 14, Array ([key] => first_name)) called at [/var/www/***.de/inc/class/MainController.php:140]
#1  errorHandler(8, Undefined index: first_name, /var/www/***.de/inc/vendor/paypal/sdk-core-php/lib/PayPal/Common/PPModel.php, 14, Array ([key] => first_name)) called at [/var/www/***.de/inc/vendor/paypal/sdk-core-php/lib/PayPal/Common/PPModel.php:14]
#2  PayPal\Common\PPModel->__get(first_name) called at [/var/www/***.de/inc/vendor/paypal/rest-api-sdk-php/lib/PayPal/Api/PayerInfo.php:69]
#3  PayPal\Api\PayerInfo->getFirstName() called at [/var/www/***.de/inc/class/SpendenController.php:129]
#4  SpendenController->perform(75b7a93a0aaa3c26) called at [/var/www/***.de/inc/class/MainController.php:786]
#5  MainController->perform() called at [/var/www/***.de/index.php:7]
sebastianhelbig commented 9 years ago

Anything new here?

jziaja commented 9 years ago

Hi @ktrhn , have you tried using the latest version of this SDK? @jaypatel512 has since added better handling for getting property values that aren't set and should resolve the original issue you brought up regarding the exception being thrown from the SDK.