|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Omnipay\WorldPay\Message; |
| 4 | + |
| 5 | +use Omnipay\Common\Message\AbstractRequest; |
| 6 | + |
| 7 | +abstract class JsonAbstractRequest extends AbstractRequest |
| 8 | +{ |
| 9 | + protected $endpoint = 'https://api.worldpay.com/v1'; |
| 10 | + |
| 11 | + public function getHttpMethod() |
| 12 | + { |
| 13 | + return 'POST'; |
| 14 | + } |
| 15 | + |
| 16 | + public function getMerchantId() |
| 17 | + { |
| 18 | + return $this->getParameter('merchantId'); |
| 19 | + } |
| 20 | + |
| 21 | + public function setMerchantId($value) |
| 22 | + { |
| 23 | + return $this->setParameter('merchantId', $value); |
| 24 | + } |
| 25 | + |
| 26 | + public function getServiceKey() |
| 27 | + { |
| 28 | + return $this->getParameter('serviceKey'); |
| 29 | + } |
| 30 | + |
| 31 | + public function setServiceKey($value) |
| 32 | + { |
| 33 | + return $this->setParameter('serviceKey', $value); |
| 34 | + } |
| 35 | + |
| 36 | + public function getClientKey() |
| 37 | + { |
| 38 | + return $this->getParameter('clientKey'); |
| 39 | + } |
| 40 | + |
| 41 | + public function setClientKey($value) |
| 42 | + { |
| 43 | + return $this->setParameter('clientKey', $value); |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public function sendRequest($data) |
| 48 | + { |
| 49 | + $config = $this->httpClient->getConfig(); |
| 50 | + $curlOptions = $config->get('curl.options'); |
| 51 | + $curlOptions[CURLOPT_SSLVERSION] = 6; |
| 52 | + $config->set('curl.options', $curlOptions); |
| 53 | + $this->httpClient->setConfig($config); |
| 54 | + |
| 55 | + // don't throw exceptions for 4xx errors |
| 56 | + $this->httpClient->getEventDispatcher()->addListener( |
| 57 | + 'request.error', |
| 58 | + function ($event) { |
| 59 | + if ($event['response']->isClientError()) { |
| 60 | + $event->stopPropagation(); |
| 61 | + } |
| 62 | + } |
| 63 | + ); |
| 64 | + |
| 65 | + $httpRequest = $this->httpClient->createRequest( |
| 66 | + $this->getHttpMethod(), |
| 67 | + $this->getEndpoint(), |
| 68 | + null, |
| 69 | + json_encode($data) |
| 70 | + ); |
| 71 | + |
| 72 | + $httpResponse = $httpRequest |
| 73 | + ->setHeader('Authorization', $this->getServiceKey()) |
| 74 | + ->setHeader('Content-type', 'application/json') |
| 75 | + ->send(); |
| 76 | + |
| 77 | + return $httpResponse; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @return string |
| 82 | + */ |
| 83 | + public function getResponseClassName() |
| 84 | + { |
| 85 | + return '\Omnipay\WorldPay\Message\JsonResponse'; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * @param mixed $data |
| 90 | + * |
| 91 | + * @return JsonResponse |
| 92 | + */ |
| 93 | + public function sendData($data) |
| 94 | + { |
| 95 | + $httpResponse = $this->sendRequest($data); |
| 96 | + |
| 97 | + $responseClass = $this->getResponseClassName(); |
| 98 | + return $this->response = new $responseClass($this, $httpResponse); |
| 99 | + } |
| 100 | +} |
0 commit comments