Exemple #1
0
 /**
  * @param Response $response
  * @param Credentials $credentials = null
  * @return ReceiptResponse
  * @throws \LogicException
  */
 public function getReceipt(Response $response, Credentials $credentials = null)
 {
     if (!$credentials && !$this->defaultCredentials) {
         throw new \LogicException(sprintf('%s requires credentials, no default credentials set, and no credentials were passed as argument', __METHOD__));
     }
     $credentials = $credentials ?: $this->defaultCredentials;
     if ($this->output !== self::OUTPUT_JSON) {
         throw new \LogicException(sprintf('%s output is not supported (yet)', $this->output));
     }
     if (!$response->getReceipt()) {
         throw new \LogicException(sprintf('The response (which was %s) does not have a receipt value', $response->getStatus() !== Response::STATUS_OK ? 'Unsuccessful' : 'Successful'));
     }
     /** @var string apiUrl */
     //override apiUrl, filling in receipt
     //always force new apiUrl!
     $this->apiUrl = sprintf($this->getApiUrl(true), $response->getReceipt());
     $options = [\CURLOPT_POSTFIELDS => ['token' => $credentials->getToken()]];
     $curlResp = $this->getRawCurl($response, $options);
     $receipt = json_decode($curlResp);
     $err = json_last_error();
     if ($err !== \JSON_ERROR_NONE) {
         throw new \RuntimeException(sprintf('JSON error %d - %s (response string: %s)', $err, json_last_error_msg(), $curlResp));
     }
     return new ReceiptResponse($receipt);
 }
 public function testErrorResponses()
 {
     foreach ($this->data->error as $vals) {
         $resp = new Response($vals);
         $objErr = $resp->getErrors();
         if (isset($vals->user)) {
             $this->assertEquals('user identifier is invalid', reset($objErr));
         }
         $this->assertNotEquals(Response::STATUS_OK, $resp->getStatus());
         $this->assertNotEmpty($resp->getErrors());
         $objErrors = $resp->getErrors();
         $this->assertEquals($vals->errors, $resp->getErrors());
         $this->assertEquals(count($vals->errors), count($objErr));
         foreach ($vals->errors as $k => $v) {
             $this->assertArrayHasKey($k, $objErr);
             $this->assertEquals($v, $objErr[$k]);
         }
         $this->assertEquals($vals->request, $resp->getRequest());
         if (isset($vals->user)) {
             $this->assertEquals($vals->user, $resp->getUser());
         }
     }
 }