使用配置文件和Paypal SDK会抛出PayPalInvalidCredentialException

时间:2017-01-25 17:13:29

标签: php paypal paypal-rest-sdk

这最初是支持票,最终成为我的rubber duck。我将它发布到堆栈溢出,以帮助任何落后于我的人。希望它能为您节省一些时间。

必填信息

  • PHP版本:5.3
  • PayPal-PHP-SDK版本:1.11
  • 调试ID:?

问题描述

使用https://github.com/paypal/PayPal-PHP-SDK/wiki/File-based-Configurations上的说明似乎不起作用。我收到以下错误:

PHP Fatal error:  Uncaught exception 'PayPal\Exception\PayPalInvalidCredentialException' with message 'Credential not found for  default user. Please make sure your configuration/APIContext has credential information

上述文章链接到与文章中的代码不匹配的sample code。此外,它似乎引用了不存在的$apiContext

官方samples包含sdk_config.ini,但它似乎无法在任何地方使用。

其他(较旧)sources更喜欢使用" Classic TEST API凭证。"我更喜欢使用id和secret,因为这似乎是一个更好的解决方案,只要它能起作用。

文件

(请忽略不良做法。这是一项实验。)

test.php的     

require_once  '../../vendor/autoload.php';
require_once './class/PaymentManager.php';
date_default_timezone_set('America/Chicago');

print_r("\n\n");

$payment = new PaymentManager();
$payment->test();

print_r("\n\n");

PaymentManager.php     

if(!defined("PP_CONFIG_PATH")) {
    define("PP_CONFIG_PATH", 'config/paypal.ini');
}

class PaymentManager {


    public function test(){
        echo $this->saveCC("visa", "4417119669820331", "012", "11", "2019", "Joe", "Shopper");
    }


    private function saveCC($type, $number, $ccv, $month, $year, $firstName, $lastName){
        error_log(file_get_contents('config/paypal.ini'));
        $creditCard = new \PayPal\Api\CreditCard();
        $creditCard->setType($type)
            ->setNumber($number)
            ->setExpireMonth($month)
            ->setExpireYear($year)
            ->setCvv2($ccv)
            ->setFirstName($firstName)
            ->setLastName($lastName);

        try {
            $creditCard->create();
        }
        catch (\PayPal\Exception\PayPalConnectionException $ex) {
            error_log("Exception while saving CC: ".$ex->getData());
        }

        return $creditCard;
    }


}

paypal.ini

; Modified from: https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/sdk_config.ini

;## This is an example configuration file for the SDK.
;## This is an example configuration file for the SDK.
;## The sample scripts configure the SDK dynamically
;## but you can choose to go for file based configuration
;## in simpler apps (See bootstrap.php for more).
[Account]
acct1.ClientId = [REDACTED]
acct1.ClientSecret = [REDACTED]

;Connection Information
[Http]
; Add Curl Constants to be configured
; The settings provided in configurations would override defaults
; if provided in configurations
http.CURLOPT_CONNECTTIMEOUT = 30

; Adding HTTP Headers to each request sent to PayPal APIs
;http.headers.PayPal-Partner-Attribution-Id = 123123123

;http.Proxy=http://[username:password]@hostname[:port]

;Service Configuration
[Service]
; can be set to sandbox / live
mode = sandbox

;Logging Information
[Log]
; For custom logging implementation, you can set the
; logging factory provider class here.
; The class should be implementing \PayPal\Log\PayPalLogFactory.
; If this is not set, it will default to \PayPal\Log\PayPalDefaultLogFactory.
;log.AdapterFactory=\PayPal\Log\PayPalDefaultLogFactory

; Settings for PayPalDefaultLogFactory
log.LogEnabled=true

; When using a relative path, the log file is created
; relative to the .php file that is the entry point
; for this request. You can also provide an absolute
; path here
; Settings for PayPalDefaultLogFactory
log.FileName=config/PayPal.log

; Logging level can be one of any provided at \Psr\Log\LogLevel
; Logging is most verbose in the 'DEBUG' level and
; decreases as you proceed towards ERROR
; DEBUG level is disabled for live, to not log sensitive information.
; If the level is set to DEBUG, it will be reduced to INFO automatically
log.LogLevel=INFO

;Caching Configuration
[cache]
; If Cache is enabled, it stores the access token retrieved from ClientId and Secret from the
; server into a file provided by the cache.FileName option or by using
; the constant $CACHE_PATH value in PayPal/Cache/AuthorizationCache if the option is omitted/empty.
; If the value is set to 'true', it would try to create a file and store the information.
; For any other value, it would disable it
; Please note, this is a very good performance improvement, and we would encourage you to
; set this up properly to reduce the number of calls, to almost 50% on normal use cases
; PLEASE NOTE: You may need to provide proper write permissions to /var directory under PayPal-PHP-SDK on
; your hosting server or whichever custom directory you choose
cache.enabled=true
; When using a relative path, the cache file is created
; relative to the .php file that is the entry point
; for this request. You can also provide an absolute
; path here
cache.FileName=config/auth.cache

1 个答案:

答案 0 :(得分:1)

解决方案:

  • PP_CONFIG_PATH应该是.ini文件的路径,没有文件名。

    if(!defined("PP_CONFIG_PATH")) {
        define("PP_CONFIG_PATH", 'config/');
    }
    
  • .ini文件本身应命名为sdk_config.ini。可能有一种方法可以更改文件名,但这是sdk默认查找的内容。如果有人知道如何更改它,我当然会感兴趣,但这不是一个表演限制器。