/** * @param HttpMethod $method * @param string $url * @param mixed[]|null $data * @param string[] $headers * @return Response * @throws CurlDriverException */ public function request(HttpMethod $method, string $url, array $data = null, array $headers = []) : Response { $ch = curl_init($url); if ($method->equalsValue(HttpMethod::POST) || $method->equalsValue(HttpMethod::PUT)) { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method->getValue()); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers + ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); $output = curl_exec($ch); if ($output === false) { throw new CurlDriverException($ch); } $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); $headers = substr($output, 0, $headerSize); $body = substr($output, $headerSize); $responseCode = new ResponseCode(curl_getinfo($ch, CURLINFO_HTTP_CODE)); curl_close($ch); return new Response($responseCode, json_decode($body, true), $this->parseHeaders($headers)); }
public function send(ApiClient $apiClient) : PaymentResponse { $price = $this->cart->getCurrentPrice(); $requestData = ['merchantId' => $this->merchantId, 'orderNo' => $this->orderId, 'payOperation' => $this->payOperation->getValue(), 'payMethod' => $this->payMethod->getValue(), 'totalAmount' => $price->getAmount(), 'currency' => $price->getCurrency()->getValue(), 'closePayment' => $this->closePayment, 'returnUrl' => $this->returnUrl, 'returnMethod' => $this->returnMethod->getValue(), 'cart' => array_map(function (CartItem $cartItem) { $cartItemValues = ['name' => $cartItem->getName(), 'quantity' => $cartItem->getQuantity(), 'amount' => $cartItem->getAmount()]; if ($cartItem->getDescription() !== null) { $cartItemValues['description'] = $cartItem->getDescription(); } return $cartItemValues; }, $this->cart->getItems()), 'description' => $this->description, 'language' => $this->language->getValue()]; if ($this->merchantData !== null) { $requestData['merchantData'] = base64_encode($this->merchantData); } if ($this->customerId !== null) { $requestData['customerId'] = $this->customerId; } if ($this->ttlSec !== null) { $requestData['ttlSec'] = $this->ttlSec; } if ($this->logoVersion !== null) { $requestData['logoVersion'] = $this->logoVersion; } if ($this->colorSchemeVersion !== null) { $requestData['colorSchemeVersion'] = $this->colorSchemeVersion; } $response = $apiClient->post('payment/init', $requestData, new SignatureDataFormatter(['merchantId' => null, 'orderNo' => null, 'dttm' => null, 'payOperation' => null, 'payMethod' => null, 'totalAmount' => null, 'currency' => null, 'closePayment' => null, 'returnUrl' => null, 'returnMethod' => null, 'cart' => ['name' => null, 'quantity' => null, 'amount' => null, 'description' => null], 'description' => null, 'merchantData' => null, 'customerId' => null, 'language' => null, 'ttlSec' => null, 'logoVersion' => null, 'colorSchemeVersion' => null]), new SignatureDataFormatter(['payId' => null, 'dttm' => null, 'resultCode' => null, 'resultMessage' => null, 'paymentStatus' => null, 'authCode' => null])); $data = $response->getData(); return new PaymentResponse($data['payId'], DateTimeImmutable::createFromFormat('YmdHis', $data['dttm']), new ResultCode($data['resultCode']), $data['resultMessage'], isset($data['paymentStatus']) ? new PaymentStatus($data['paymentStatus']) : null, $data['authCode'] ?? null); }
/** * @param HttpMethod $method * @param string $url * @param mixed[]|null $data * @param string[] $headers * @return Response * @throws GuzzleDriverException */ public function request(HttpMethod $method, string $url, array $data = null, array $headers = []) : Response { $postData = null; if ($method->equalsValue(HttpMethod::POST) || $method->equalsValue(HttpMethod::PUT)) { $postData = json_encode($data); } $headers += ['Content-Type' => 'application/json']; $request = new Request($method->getValue(), $url, $headers, $postData); try { $httpResponse = $this->client->send($request, [RequestOptions::HTTP_ERRORS => false, RequestOptions::ALLOW_REDIRECTS => false]); $responseCode = new ResponseCode($httpResponse->getStatusCode()); $responseHeaders = array_map(function ($item) { return !is_array($item) || count($item) > 1 ? $item : array_shift($item); }, $httpResponse->getHeaders()); return new Response($responseCode, json_decode((string) $httpResponse->getBody(), true), $responseHeaders); } catch (\Throwable $e) { throw new GuzzleDriverException($e); } }
private function logRequest(HttpMethod $method, string $url, array $queries, array $requestData = null, Response $response) { if ($this->logger === null) { return; } $responseData = $response->getData(); unset($requestData['signature']); unset($queries['signature']); unset($responseData['signature']); if (isset($responseData['extensions'])) { foreach ($responseData['extensions'] as $key => $extensionData) { unset($responseData['extensions'][$key]['signature']); } } $context = ['request' => ['method' => $method->getValue(), 'queries' => $queries, 'data' => $requestData], 'response' => ['code' => $response->getResponseCode()->getValue(), 'data' => $responseData, 'headers' => $response->getHeaders()]]; $this->logger->info($url, $context); }