public function handle($httpConfig, $request, $options) { $credential = $this->apiContext->getCredential(); $config = $this->apiContext->getConfig(); if ($credential == NULL) { // Try picking credentials from the config file $credMgr = PPCredentialManager::getInstance($config); $credValues = $credMgr->getCredentialObject(); if (!is_array($credValues)) { throw new PPMissingCredentialException("Empty or invalid credentials passed"); } $credential = new OAuthTokenCredential($credValues['clientId'], $credValues['clientSecret']); } if ($credential == NULL || !$credential instanceof OAuthTokenCredential) { throw new PPInvalidCredentialException("Invalid credentials passed"); } $httpConfig->setUrl(rtrim(trim($this->_getEndpoint($config)), '/') . (isset($options['path']) ? $options['path'] : '')); if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) { $httpConfig->addHeader("User-Agent", PPUserAgent::getValue(self::$sdkName, self::$sdkVersion)); } if (!is_null($credential) && $credential instanceof OAuthTokenCredential) { $httpConfig->addHeader('Authorization', "Bearer " . $credential->getAccessToken($config)); } if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') { $httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId()); } }
public function makeRequest($apiMethod, $params, $apiUsername = null) { $this->apiMethod = $apiMethod; if (is_string($apiUsername) || is_null($apiUsername)) { // $apiUsername is optional, if null the default account in config file is taken $credMgr = PPCredentialManager::getInstance($this->config); $apiCredential = clone $credMgr->getCredentialObject($apiUsername); } else { $apiCredential = $apiUsername; //TODO: Aargh } if (isset($this->config['accessToken']) && isset($this->config['tokenSecret'])) { $apiCredential->setThirdPartyAuthorization(new PPTokenAuthorization($this->config['accessToken'], $this->config['tokenSecret'])); } $request = new PPRequest($params, $this->serviceBinding); $request->setCredential($apiCredential); $httpConfig = new PPHttpConfig(null, PPHttpConfig::HTTP_POST); $this->runHandlers($httpConfig, $request); $formatter = FormatterFactory::factory($this->serviceBinding); $payload = $formatter->toString($request); $connection = PPConnectionManager::getInstance()->getConnection($httpConfig, $this->config); $this->logger->info("Request: {$payload}"); $response = $connection->execute($payload); $this->logger->info("Response: {$response}"); return array('request' => $payload, 'response' => $response); }
public function handle($httpConfig, $request, $options) { parent::handle($httpConfig, $request, $options); $config = $options['config']; if (is_string($this->apiUsername) || is_null($this->apiUsername)) { // $apiUsername is optional, if null the default account in config file is taken $credMgr = PPCredentialManager::getInstance($options['config']); $request->setCredential(clone $credMgr->getCredentialObject($this->apiUsername)); } else { $request->setCredential($this->apiUsername); } $endpoint = ''; $credential = $request->getCredential(); if (isset($options['port']) && isset($config['service.EndPoint.' . $options['port']])) { $endpoint = $config['service.EndPoint.' . $options['port']]; } else { if (isset($config['service.EndPoint'])) { $endpoint = $config['service.EndPoint']; } else { if (isset($config['mode'])) { if (strtoupper($config['mode']) == 'SANDBOX') { if ($credential instanceof PPSignatureCredential) { $endpoint = PPConstants::MERCHANT_SANDBOX_SIGNATURE_ENDPOINT; } else { if ($credential instanceof PPCertificateCredential) { $endpoint = PPConstants::MERCHANT_SANDBOX_CERT_ENDPOINT; } } } else { if (strtoupper($config['mode']) == 'LIVE') { if ($credential instanceof PPSignatureCredential) { $endpoint = PPConstants::MERCHANT_LIVE_SIGNATURE_ENDPOINT; } else { if ($credential instanceof PPCertificateCredential) { $endpoint = PPConstants::MERCHANT_LIVE_CERT_ENDPOINT; } } } } } else { throw new PPConfigurationException('endpoint Not Set'); } } } if ($request->getBindingType() == 'SOAP') { $httpConfig->setUrl($endpoint); } else { throw new PPConfigurationException('expecting service binding to be SOAP'); } $request->addBindingInfo("namespace", "xmlns:ns=\"urn:ebay:api:PayPalAPI\" xmlns:ebl=\"urn:ebay:apis:eBLBaseComponents\" xmlns:cc=\"urn:ebay:apis:CoreComponentTypes\" xmlns:ed=\"urn:ebay:apis:EnhancedDataTypes\""); // Call the authentication handler to tack authentication related info $handler = new PPAuthenticationHandler(); $handler->handle($httpConfig, $request, $options); }
public function handle($httpConfig, $request, $options) { parent::handle($httpConfig, $request, $options); if (is_string($this->apiUsername) || is_null($this->apiUsername)) { // $apiUsername is optional, if null the default account in config file is taken $credMgr = PPCredentialManager::getInstance($options['config']); $request->setCredential(clone $credMgr->getCredentialObject($this->apiUsername)); } else { $request->setCredential($this->apiUsername); } $config = $options['config']; $credential = $request->getCredential(); //TODO: Assuming existence of getApplicationId if ($credential && $credential->getApplicationId() != null) { $httpConfig->addHeader('X-PAYPAL-APPLICATION-ID', $credential->getApplicationId()); } if (isset($config['port']) && isset($config['service.EndPoint.' . $options['port']])) { $endpoint = $config['service.EndPoint.' . $options['port']]; } else { if (isset($config['service.EndPoint'])) { $endpoint = $config['service.EndPoint']; } else { if (isset($config['mode'])) { if (strtoupper($config['mode']) == 'SANDBOX') { $endpoint = PPConstants::PLATFORM_SANDBOX_ENDPOINT; } else { if (strtoupper($config['mode']) == 'LIVE') { $endpoint = PPConstants::PLATFORM_LIVE_ENDPOINT; } else { if (strtoupper($config['mode']) == 'TLS') { $endpoint = PPConstants::PLATFORM_TLS_ENDPOINT; } } } } else { throw new PPConfigurationException('endpoint Not Set'); } } } $httpConfig->setUrl($endpoint . $options['serviceName'] . '/' . $options['apiMethod']); // Call the authentication handler to tack authentication related info $handler = new PPAuthenticationHandler(); $handler->handle($httpConfig, $request, $options); }
/** * @test */ public function testInvalidConfiguration() { $this->setExpectedException('PayPal\\Exception\\PPMissingCredentialException'); $o = PPCredentialManager::getInstance(array('mode' => 'sandbox')); }
/** * @test */ public function testGetPlatformCredentialObject() { $cred = $this->object->getCredentialObject(); $this->assertEquals('APP-80W284485P519543T', $cred->getApplicationId()); }