/** * @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 testResponseConstructor() { $receiptResponses = array(); foreach ($this->data->success as $k => $vals) { $resp = new Response($vals); $this->assertEquals(Response::STATUS_OK, $resp->getStatus()); $this->assertNull($resp->getErrors()); $this->assertEquals($vals->request, $resp->getRequest()); $this->assertEmpty($resp->getErrors()); if (in_array($k, $this->data->receiptKeys)) { $this->assertEquals($vals->receipt, $resp->getReceipt()); $receiptResponses[] = $resp; } } return $receiptResponses; }