doukao2180 2017-01-25 17:13
浏览 104
已采纳

使用配置文件和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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)