doukao2180
2017-01-25 17:13 阅读 66
已采纳

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

This was originally a support ticket that ended up being my rubber duck. I'm posting it to stack overflow to help anyone that comes behind me. Hopefully, it'll save you some time.

Required Information

  • PHP Version: 5.3
  • PayPal-PHP-SDK Version: 1.11
  • Debug ID(s): ?

Issue Description

Using the instructions at: https://github.com/paypal/PayPal-PHP-SDK/wiki/File-based-Configurations does not seem to work. I'm getting the following error:

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

The above article links to sample code that doesn't match the code in the article. Furthermore, it seems to reference a nonexistent $apiContext.

The official samples contain an sdk_config.ini, but it doesn't seem to be used anywhere.

Other (older) sources prefer to use the "Classic TEST API Credentials." I'd prefer to use the id and secret as this seems to be a better solution if only it would work.

Files

(Please ignore the bad practices. This is an experiment.)

test.php

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

print_r("

");

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

print_r("

");

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条回答 默认 最新

  • 已采纳
    douchun9719 douchun9719 2017-01-25 17:13

    Solution:

    • PP_CONFIG_PATH should be the path to the .ini file, without the filename.

      if(!defined("PP_CONFIG_PATH")) {
          define("PP_CONFIG_PATH", 'config/');
      }
      
    • The .ini file itself should be named sdk_config.ini. There may be a way to change the filename, but this is what the sdk looks for by default. I'm certainly interested if someone knows how to change it, but this is not a show stopper.

    点赞 评论 复制链接分享

相关推荐