I'm trying to integrate the PayPal SDK into my site but have run into a problem which I cannot find a solution for in the documentation or via Stackoverflow.
I use a standard set-up:
start.php:
<?php
//require 'vendor/autoload.php';
require __DIR__ . '/../vendor/autoload.php';
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
define('BASE_URL', 'http://localhost:80/paypaltut/');
if(!defined("PP_CONFIG_PATH")){
define('PP_CONFIG_PATH', '../vendor/paypal/rest-api-sdk-php/tests/');
}
$clientid = 'ARe54bHOzRcn13nRglDpIst46bWOp6pyBRYlP4nulwwTL2ivIuKlIJrUp5LdgZfuC0qPbqIuGdVFsmeK';
$clientsecret = 'EJarieZ8B_6WEZ__gZl0uS-Dmc-ypa1RH1joF1u4_XlJje2IINBRCsARhNyZk-dJG7kBJS8ceQF5GNVr';
$apiContext = new ApiContext(new OAuthTokenCredential($clientid, $clientsecret));
;Account credentials from developer portal
[Account]
acct1.ClientId = AYSq3RDGsmBLJE-otTkBtM-jBRd1TCQwFf9RGfwddNXWz0uFU9ztymylOhRS
acct1.ClientSecret = EGnHDxD_qRPdaLdZz8iCr8N7_MzF-YHPTkjs6NKYQvQSBngp4PTTVWkPZRbL
acct2.ClientId = TestClientId
acct2.ClientSecret = TestClientSecret
;Connection Information
[Http]
http.ConnectionTimeOut = 60
http.Retry = 1
;http.Proxy=http://[username:password]@hostname[:port][/path]
mode=sandbox
;Service Configuration
[Service]
service.EndPoint="https://api.sandbox.paypal.com"
; Uncomment this line for integrating with the live endpoint
; service.EndPoint="https://api.paypal.com"
;Logging Information
[Log]
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
log.FileName=PayPal.log
; Logging level can be one of FINE, INFO, WARN or ERROR
; Logging is most verbose in the 'FINE' level and
; decreases as you proceed towards ERROR
log.LogLevel=DEBUG
;Validation Configuration
[validation]
; If validation is set to strict, the PayPalModel would make sure that
; there are proper accessors (Getters and Setters) for each model
; objects. Accepted value is
; 'log' : logs the error message to logger only (default)
; 'strict' : throws a php notice message
; 'disable' : disable the validation
validation.level=strict
When I attempt to process the payment using the paypal sandbox version of my app, I get the error:
exception 'PayPal\Exception\PayPalInvalidCredentialException' with message 'Credential not found for default user. Please make sure your configuration/APIContext has credential information' in D:\WebDev\htdocs\paypaltut\vendor\paypal\rest-api-sdk-php\lib\PayPal\Core\PayPalCredentialManager.php:154 Stack trace:
Stack trace:
#0 D:\WebDev\htdocs\paypaltut\vendor\paypal\rest-api-sdk-php\lib\PayPal\Rest\ApiContext.php(56): PayPal\Core\PayPalCredentialManager->getCredentialObject()
#1 D:\WebDev\htdocs\paypaltut\vendor\paypal\rest-api-sdk-php\lib\PayPal\Handler\RestHandler.php(51): PayPal\Rest\ApiContext->getCredential()
#2 D:\WebDev\htdocs\paypaltut\vendor\paypal\rest-api-sdk-php\lib\PayPal\Transport\PayPalRestCall.php(71): PayPal\Handler\RestHandler->handle(Object(PayPal\Core\PayPalHttpConfig), '{"payer_id":"6H...', Array)
#3 D:\WebDev\htdocs\paypaltut\vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalResourceModel.php(103): PayPal\Transport\PayPalRestCall->execute(Array, '/v1/payments/pa...', 'POST', '{"payer_id":"6H...', NULL)
#4 D:\WebDev\htdocs\paypaltut\vendor\paypal\rest-api-sdk-php\lib\PayPal\Api\Payment.php(498): PayPal\Common\PayPalResourceModel::executeCall('/v1/payments/pa...', 'POST', '{"payer_id":"6H...', NULL, NULL, NULL)
#5 D:\WebDev\htdocs\paypaltut\pay.php(36): PayPal\Api\Payment->execute(Object(PayPal\Api\PaymentExecution))
#6 {main}
I have gone through the process of creating the app through the paypal developer's dashboard, I've tried adding a new ApiContext to teh pay (return url ) file and still get the same error. anybody know how to fix this one?
oh an I'm using the v1.5.0 sdk and php 5.6.1
解决方案
Found the root cause.
You forgot to pass $apiContext object to execute call in your pay.php code.