diff --git a/.env.example b/.env.example index c428f81..c1420e0 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ API_MERCHANT=112233 API_SECRET=foobarfoobaz -API_URL=https://payments.comgate.cz/v1.0/ +API_URL_REST=https://payments.comgate.cz/v2.0/ APPLICATION_ENV=dev diff --git a/README.md b/README.md index 36442a9..764f349 100644 --- a/README.md +++ b/README.md @@ -277,7 +277,7 @@ use Comgate\SDK\Entity\TerminalPayment; use Comgate\SDK\Entity\TerminalRefund; use Comgate\SDK\Exception\ApiException; -$clientTerminal = Comgate::defaultsRest() +$clientTerminal = Comgate::defaults() ->setMerchant('123456') // get on portal.comgate.cz ->setSecret('foobarbaz') // get on portal.comgate.cz ->createTerminalClient(); diff --git a/src/Client.php b/src/Client.php index 2e0c89f..d6628e1 100644 --- a/src/Client.php +++ b/src/Client.php @@ -62,7 +62,7 @@ public function __construct(ITransport $transport) public function createPayment(Payment $payment): PaymentCreateResponse { $paymentCreateRequest = new PaymentCreateRequest($payment); - $paymentCreateResponse = $this->transport->post($paymentCreateRequest->getUrn(), + $paymentCreateResponse = $this->transport->postJson($paymentCreateRequest->getUrn(), $paymentCreateRequest->toArray()); return new PaymentCreateResponse($paymentCreateResponse); } @@ -70,7 +70,7 @@ public function createPayment(Payment $payment): PaymentCreateResponse public function getStatus(string $transId): PaymentStatusResponse { $paymentStatusRequest = new PaymentStatusRequest($transId); - $statusResponse = $this->transport->post($paymentStatusRequest->getUrn(), $paymentStatusRequest->toArray()); + $statusResponse = $this->transport->get($paymentStatusRequest->getUrn()); return new PaymentStatusResponse($statusResponse); } @@ -80,7 +80,7 @@ public function getMethods(?MethodsRequest $methodsRequest = null): MethodsRespo $methodsRequest = new MethodsRequest(); } - $methodsResponse = $this->transport->post($methodsRequest->getUrn(), $methodsRequest->toArray()); + $methodsResponse = $this->transport->get($methodsRequest->getUrn()); return new MethodsResponse($methodsResponse); } @@ -88,37 +88,35 @@ public function getMethods(?MethodsRequest $methodsRequest = null): MethodsRespo public function cancelPayment(string $transId): PaymentCancelResponse { $cancelPaymentRequest = new PaymentCancelRequest($transId); - $cancelResponse = $this->transport->post($cancelPaymentRequest->getUrn(), $cancelPaymentRequest->toArray()); + $cancelResponse = $this->transport->delete($cancelPaymentRequest->getUrn()); return new PaymentCancelResponse($cancelResponse); } public function capturePreauth(string $transId, Money $amount): PreauthCaptureResponse { $capturePreauthRequest = new PreauthCaptureRequest($transId, $amount); - $captureResponse = $this->transport->post($capturePreauthRequest->getUrn(), $capturePreauthRequest->toArray()); + $captureResponse = $this->transport->putJson($capturePreauthRequest->getUrn(), $capturePreauthRequest->toArray()); return new PreauthCaptureResponse($captureResponse); } public function cancelPreauth(string $transId): PreauthCancelResponse { $cancelPreauthRequest = new PreauthCancelRequest($transId); - $cancelResponse = $this->transport->post($cancelPreauthRequest->getUrn(), $cancelPreauthRequest->toArray()); + $cancelResponse = $this->transport->delete($cancelPreauthRequest->getUrn()); return new PreauthCancelResponse($cancelResponse); } public function refundPayment(Refund $refund): RefundResponse { $refundRequest = new PaymentRefundRequest($refund); - $refundResponse = $this->transport->post($refundRequest->getUrn(), $refundRequest->toArray()); + $refundResponse = $this->transport->postJson($refundRequest->getUrn(), $refundRequest->toArray()); return new RefundResponse($refundResponse); } public function initRecurringPayment(Payment $payment): RecurringPaymentResponse { $recurringRequest = new RecurringPaymentRequest($payment); - echo $recurringRequest->getUrn(); - $recurringResponse = $this->transport->post($recurringRequest->getUrn(), $recurringRequest->toArray()); - echo $recurringResponse->getContent(); + $recurringResponse = $this->transport->postJson($recurringRequest->getUrn(), $recurringRequest->toArray()); return new RecurringPaymentResponse($recurringResponse); } @@ -130,7 +128,7 @@ public function initRecurringPayment(Payment $payment): RecurringPaymentResponse public function simulation(array $params): SimulationResponse { $simulationRequest = new SimulationRequest($params); - $simulationResponse = $this->transport->post($simulationRequest->getUrn(), $simulationRequest->toArray()); + $simulationResponse = $this->transport->postJson($simulationRequest->getUrn(), $simulationRequest->toArray()); return new SimulationResponse($simulationResponse); } @@ -143,52 +141,46 @@ public function simulation(array $params): SimulationResponse public function transferList(DateTimeInterface $date, bool $test): TransferListResponse { $transferListRequest = new TransferListRequest($date, $test); - $transferListResponse = $this->transport->post($transferListRequest->getUrn(), $transferListRequest->toArray()); + $transferListResponse = $this->transport->get($transferListRequest->getUrn()); return new TransferListResponse($transferListResponse); } public function singleTransfer(int $transferId, bool $test): SingleTransferResponse { $singleTransferRequest = new SingleTransferRequest($transferId, $test); - $singleTransferResponse = $this->transport->post($singleTransferRequest->getUrn(), - $singleTransferRequest->toArray()); + $singleTransferResponse = $this->transport->get($singleTransferRequest->getUrn()); return new SingleTransferResponse($singleTransferResponse); } public function csvSingleTransfer(string $transferId, bool $test): CsvSingleTransferResponse { $csvSingleTransferRequest = new CsvSingleTransferRequest($transferId, $test); - $csvSingleTransferResponse = $this->transport->post($csvSingleTransferRequest->getUrn(), - $csvSingleTransferRequest->toArray()); + $csvSingleTransferResponse = $this->transport->get($csvSingleTransferRequest->getUrn()); return new CsvSingleTransferResponse($csvSingleTransferResponse); } public function aboSingleTransfer(string $transferId, bool $test, string $type, string $encoding): AboSingleTransferResponse { $aboSingleTransferRequest = new AboSingleTransferRequest($transferId, $test, $type, $encoding); - $aboSingleTransferResponse = $this->transport->post($aboSingleTransferRequest->getUrn(), - $aboSingleTransferRequest->toArray()); + $aboSingleTransferResponse = $this->transport->get($aboSingleTransferRequest->getUrn()); return new AboSingleTransferResponse($aboSingleTransferResponse); } public function getAppleDomainAssociation(string $method = '', string $currency = ''): AppleDomainAssociationResponse{ $appleDomainAssociationRequest = new AppleDomainAssociationRequest($method, $currency); - $appleDomainAssociationResponse = $this->transport->post($appleDomainAssociationRequest->getUrn(), - $appleDomainAssociationRequest->toArray()); + $appleDomainAssociationResponse = $this->transport->get($appleDomainAssociationRequest->getUrn()); return new AppleDomainAssociationResponse($appleDomainAssociationResponse); } public function getCsvDownload(string $date, bool $test = false): void{ $csvDownloadRequest = new CsvDownloadRequest($date, $test); - $csvDownloadResponse = $this->transport->post($csvDownloadRequest->getUrn(), - $csvDownloadRequest->toArray()); + $csvDownloadResponse = $this->transport->get($csvDownloadRequest->getUrn()); new CsvDownloadResponse($csvDownloadResponse); } public function getAboDownload(string $date, string $type = '', bool $test = false, string $encoding = 'utf8'): void{ $aboDownloadRequest = new AboDownloadRequest($date, $type, $test, $encoding); - $aboDownloadResponse = $this->transport->post($aboDownloadRequest->getUrn(), - $aboDownloadRequest->toArray()); + $aboDownloadResponse = $this->transport->get($aboDownloadRequest->getUrn()); new AboDownloadResponse($aboDownloadResponse); } @@ -203,7 +195,7 @@ public function getAboDownload(string $date, string $type = '', bool $test = fa public function createMotoPayment(Payment $payment, PaymentCard $paymentCard): ?MotoPaymentCreateResponse { $publicCryptoKeyRequest = new PublicCryptoKeyRequest(); - $publicCryptoKeyResponse = new PublicCryptoKeyResponse($this->transport->post($publicCryptoKeyRequest->getUrn(), $publicCryptoKeyRequest->toArray())); + $publicCryptoKeyResponse = new PublicCryptoKeyResponse($this->transport->get($publicCryptoKeyRequest->getUrn())); $publicJkwKey = null; $jwkData = json_decode(base64_decode($publicCryptoKeyResponse->getKey(), true), true); @@ -237,7 +229,7 @@ public function createMotoPayment(Payment $payment, PaymentCard $paymentCard): ? } $motoPaymentCreateRequest = new MotoPaymentCreateRequest($motoPayment); - $motoPaymentCreateResponse = $this->transport->post($motoPaymentCreateRequest->getUrn(), $motoPaymentCreateRequest->toArray()); + $motoPaymentCreateResponse = $this->transport->postJson($motoPaymentCreateRequest->getUrn(), $motoPaymentCreateRequest->toArray()); return new MotoPaymentCreateResponse($motoPaymentCreateResponse); } diff --git a/src/Comgate.php b/src/Comgate.php index 8ef554b..d946dd4 100644 --- a/src/Comgate.php +++ b/src/Comgate.php @@ -36,17 +36,6 @@ public static function defaults(): self return $self; } - /** - * @return static - */ - public static function defaultsRest(): self - { - $self = new static(); - $self->url = Config::URL_REST; - - return $self; - } - /** * @return static */ diff --git a/src/Config.php b/src/Config.php index 57ad3ac..3bab14b 100644 --- a/src/Config.php +++ b/src/Config.php @@ -4,8 +4,8 @@ class Config { - public const URL = 'https://payments.comgate.cz/v1.0/'; - public const URL_REST = 'https://payments.comgate.cz/v2.0/'; + public const URL = 'https://payments.comgate.cz/v2.0/'; + /** @var string */ private $merchant; diff --git a/src/Entity/Request/AboDownloadRequest.php b/src/Entity/Request/AboDownloadRequest.php index f9d54ca..b73ba21 100644 --- a/src/Entity/Request/AboDownloadRequest.php +++ b/src/Entity/Request/AboDownloadRequest.php @@ -34,7 +34,15 @@ public function __construct(string $date, string $type, bool $test, string $enco */ public function getUrn(): string { - return 'aboDownload'; + $urn = 'aboDownload/date/' . urlencode($this->getDate()); + $params = [ + 'test' => $this->isTest() ? 'true' : 'false', + 'encoding' => $this->getEncoding(), + ]; + if ($this->getType() !== '') { + $params['type'] = $this->getType(); + } + return $urn . '?' . http_build_query($params); } /** diff --git a/src/Entity/Request/AboSingleTransferRequest.php b/src/Entity/Request/AboSingleTransferRequest.php index d0a9c3c..44abfab 100644 --- a/src/Entity/Request/AboSingleTransferRequest.php +++ b/src/Entity/Request/AboSingleTransferRequest.php @@ -29,7 +29,8 @@ class AboSingleTransferRequest implements IRequest * @var string */ protected $encoding; - private bool $download = false; // just for method sync Cest to pass, should be always false + /** @var bool */ + private $download = false; // just for method sync Cest to pass, should be always false public function __construct(string $transferId, bool $test, string $type, string $encoding) { @@ -44,7 +45,13 @@ public function __construct(string $transferId, bool $test, string $type, string */ public function getUrn(): string { - return 'aboSingleTransfer'; + $urn = 'aboSingleTransfer/transferId/' . urlencode($this->getTransferId()) . '.json'; + $params = [ + 'type' => $this->getType(), + 'encoding' => $this->getEncoding(), + 'test' => $this->isTest() ? 'true' : 'false', + ]; + return $urn . '?' . http_build_query($params); } /** diff --git a/src/Entity/Request/AppleDomainAssociationRequest.php b/src/Entity/Request/AppleDomainAssociationRequest.php index 4d4baca..7e93b88 100644 --- a/src/Entity/Request/AppleDomainAssociationRequest.php +++ b/src/Entity/Request/AppleDomainAssociationRequest.php @@ -8,7 +8,8 @@ class AppleDomainAssociationRequest implements IRequest * @var string */ protected $method; - protected string $currency; + /** @var string */ + protected $currency; public function __construct(string $method, string $currency) { @@ -18,7 +19,15 @@ public function __construct(string $method, string $currency) public function getUrn(): string { - return 'appleDomainAssociation'; + $params = []; + if ($this->getCurrency() !== '') { + $params['currency'] = $this->getCurrency(); + } + $urn = 'appleDomainAssociation.json'; + if (count($params) > 0) { + $urn .= '?' . http_build_query($params); + } + return $urn; } public function toArray(): array diff --git a/src/Entity/Request/CsvDownloadRequest.php b/src/Entity/Request/CsvDownloadRequest.php index d9b583e..e179316 100644 --- a/src/Entity/Request/CsvDownloadRequest.php +++ b/src/Entity/Request/CsvDownloadRequest.php @@ -25,7 +25,9 @@ public function __construct(string $date, bool $test) */ public function getUrn(): string { - return 'csvDownload'; + $urn = 'csvDownload/date/' . urlencode($this->getDate()); + $params = ['test' => $this->isTest() ? 'true' : 'false']; + return $urn . '?' . http_build_query($params); } /** diff --git a/src/Entity/Request/CsvSingleTransferRequest.php b/src/Entity/Request/CsvSingleTransferRequest.php index 70b9d25..9528466 100644 --- a/src/Entity/Request/CsvSingleTransferRequest.php +++ b/src/Entity/Request/CsvSingleTransferRequest.php @@ -13,7 +13,8 @@ class CsvSingleTransferRequest implements IRequest * @var bool */ protected $test; - private bool $download = false; // just for method sync Cest to pass, should be always false + /** @var bool */ + private $download = false; // just for method sync Cest to pass, should be always false public function __construct(string $transferId, bool $test) { @@ -26,7 +27,9 @@ public function __construct(string $transferId, bool $test) */ public function getUrn(): string { - return 'csvSingleTransfer'; + $urn = 'csvSingleTransfer/transferId/' . urlencode($this->getTransferId()) . '.json'; + $params = ['test' => $this->isTest() ? 'true' : 'false']; + return $urn . '?' . http_build_query($params); } /** diff --git a/src/Entity/Request/MethodsRequest.php b/src/Entity/Request/MethodsRequest.php index 93c3003..71dad35 100644 --- a/src/Entity/Request/MethodsRequest.php +++ b/src/Entity/Request/MethodsRequest.php @@ -61,7 +61,14 @@ class MethodsRequest implements IRequest public function getUrn(): string { - return 'methods'; + $params = $this->toArray(); + unset($params['type']); + + $urn = 'method.json'; + if (count($params) > 0) { + $urn .= '?' . http_build_query($params); + } + return $urn; } /** @@ -198,12 +205,16 @@ public function getPrice(): ?string } /** - * @param string|null $price + * @param string|int|float|null $price * @return void */ - public function setPrice(?string $price): void + public function setPrice($price): void { - $this->price = $price; + if ($price === null) { + $this->price = null; + } else { + $this->price = (string) $price; + } } /** diff --git a/src/Entity/Request/MotoPaymentCreateRequest.php b/src/Entity/Request/MotoPaymentCreateRequest.php index 8bfaa29..107fb91 100644 --- a/src/Entity/Request/MotoPaymentCreateRequest.php +++ b/src/Entity/Request/MotoPaymentCreateRequest.php @@ -19,7 +19,7 @@ public function __construct(MotoPayment $payment) */ public function getUrn(): string { - return 'moto'; + return 'moto.json'; } /** diff --git a/src/Entity/Request/PaymentCancelRequest.php b/src/Entity/Request/PaymentCancelRequest.php index a012df1..959c0b0 100644 --- a/src/Entity/Request/PaymentCancelRequest.php +++ b/src/Entity/Request/PaymentCancelRequest.php @@ -20,7 +20,7 @@ public function __construct(string $transId) */ public function getUrn(): string { - return 'cancel'; + return 'payment/transId/' . urlencode($this->getTransId()) . '.json'; } /** diff --git a/src/Entity/Request/PaymentCreateRequest.php b/src/Entity/Request/PaymentCreateRequest.php index 89c2fdf..a5f7c3b 100644 --- a/src/Entity/Request/PaymentCreateRequest.php +++ b/src/Entity/Request/PaymentCreateRequest.php @@ -20,7 +20,7 @@ public function __construct(Payment $payment) */ public function getUrn(): string { - return 'create'; + return 'payment.json'; } /** diff --git a/src/Entity/Request/PaymentRefundRequest.php b/src/Entity/Request/PaymentRefundRequest.php index 2cec5a9..2da2874 100644 --- a/src/Entity/Request/PaymentRefundRequest.php +++ b/src/Entity/Request/PaymentRefundRequest.php @@ -23,7 +23,7 @@ public function __construct(Refund $refund) */ public function getUrn(): string { - return 'refund'; + return 'refund.json'; } /** diff --git a/src/Entity/Request/PaymentStatusRequest.php b/src/Entity/Request/PaymentStatusRequest.php index 68df928..5f160c0 100644 --- a/src/Entity/Request/PaymentStatusRequest.php +++ b/src/Entity/Request/PaymentStatusRequest.php @@ -20,7 +20,7 @@ public function __construct(string $transId) */ public function getUrn(): string { - return 'status'; + return 'payment/transId/' . urlencode($this->getTransId()) . '.json'; } /** diff --git a/src/Entity/Request/PreauthCancelRequest.php b/src/Entity/Request/PreauthCancelRequest.php index c78ef9d..d2e98ff 100644 --- a/src/Entity/Request/PreauthCancelRequest.php +++ b/src/Entity/Request/PreauthCancelRequest.php @@ -22,7 +22,7 @@ public function __construct(string $transId){ */ public function getUrn(): string { - return 'cancelPreauth'; + return 'preauth/transId/' . urlencode($this->getTransId()) . '.json'; } /** diff --git a/src/Entity/Request/PreauthCaptureRequest.php b/src/Entity/Request/PreauthCaptureRequest.php index fc38009..4f82a1e 100644 --- a/src/Entity/Request/PreauthCaptureRequest.php +++ b/src/Entity/Request/PreauthCaptureRequest.php @@ -27,7 +27,7 @@ public function __construct(string $transId, Money $amount){ */ public function getUrn(): string { - return 'capturePreauth'; + return 'preauth/transId/' . urlencode($this->getTransId()) . '.json'; } /** @@ -35,13 +35,9 @@ public function getUrn(): string */ public function toArray(): array { - // Required - $output = [ - 'transId' => $this->getTransId(), + return [ 'amount' => $this->getAmount()->get(), ]; - - return $output; } /** diff --git a/src/Entity/Request/PublicCryptoKeyRequest.php b/src/Entity/Request/PublicCryptoKeyRequest.php index ab52fea..5f6367e 100644 --- a/src/Entity/Request/PublicCryptoKeyRequest.php +++ b/src/Entity/Request/PublicCryptoKeyRequest.php @@ -11,7 +11,7 @@ class PublicCryptoKeyRequest implements IRequest */ public function getUrn(): string { - return 'pubCryptoKey'; + return 'pubCryptoKey.json'; } /** diff --git a/src/Entity/Request/RecurringPaymentRequest.php b/src/Entity/Request/RecurringPaymentRequest.php index f711571..ddb27ec 100644 --- a/src/Entity/Request/RecurringPaymentRequest.php +++ b/src/Entity/Request/RecurringPaymentRequest.php @@ -22,7 +22,7 @@ public function __construct(Payment $payment){ */ public function getUrn(): string { - return 'recurring'; + return 'recurring.json'; } /** diff --git a/src/Entity/Request/SimulationRequest.php b/src/Entity/Request/SimulationRequest.php index cad76c2..ca83691 100644 --- a/src/Entity/Request/SimulationRequest.php +++ b/src/Entity/Request/SimulationRequest.php @@ -22,7 +22,7 @@ public function __construct(array $params) public function getUrn(): string { - return 'simulation'; + return 'simulation.json'; } /** diff --git a/src/Entity/Request/SingleTransferRequest.php b/src/Entity/Request/SingleTransferRequest.php index cec8b1c..76ebbcf 100644 --- a/src/Entity/Request/SingleTransferRequest.php +++ b/src/Entity/Request/SingleTransferRequest.php @@ -24,7 +24,9 @@ public function __construct(int $transferId, bool $test = false) */ public function getUrn(): string { - return 'singleTransfer'; + $urn = 'singleTransfer/transferId/' . $this->getTransferId() . '.json'; + $params = ['test' => $this->isTest() ? 'true' : 'false']; + return $urn . '?' . http_build_query($params); } /** diff --git a/src/Entity/Request/TransferListRequest.php b/src/Entity/Request/TransferListRequest.php index 5a83649..9196eca 100644 --- a/src/Entity/Request/TransferListRequest.php +++ b/src/Entity/Request/TransferListRequest.php @@ -28,7 +28,9 @@ public function __construct(DateTimeInterface $date, bool $test = false) */ public function getUrn(): string { - return 'transferList'; + $urn = 'transferList/date/' . urlencode($this->getDate()->format(self::DATE_FORMAT)) . '.json'; + $params = ['test' => $this->isTest() ? 'true' : 'false']; + return $urn . '?' . http_build_query($params); } /** diff --git a/src/Entity/Response/AppleDomainAssociationResponse.php b/src/Entity/Response/AppleDomainAssociationResponse.php index f99fa7b..10c28cf 100644 --- a/src/Entity/Response/AppleDomainAssociationResponse.php +++ b/src/Entity/Response/AppleDomainAssociationResponse.php @@ -4,19 +4,19 @@ use Comgate\SDK\Exception\Api\MissingParamException; use Comgate\SDK\Exception\ApiException; -use Comgate\SDK\Http\Query; use Comgate\SDK\Http\Response; class AppleDomainAssociationResponse { - protected string $fileContent; + /** @var string */ + protected $fileContent; /** * @param Response $appleDomainAssociationResponse * @throws ApiException */ public function __construct(Response $appleDomainAssociationResponse) { - $parsedResponse = Query::parse($appleDomainAssociationResponse->getContent()); + $parsedResponse = json_decode($appleDomainAssociationResponse->getContent(), true); if (isset($parsedResponse['code']) && isset($parsedResponse['message'])) { $code = (int) $parsedResponse['code']; diff --git a/src/Entity/Response/MotoPaymentCreateResponse.php b/src/Entity/Response/MotoPaymentCreateResponse.php index c2126ea..e8c9957 100644 --- a/src/Entity/Response/MotoPaymentCreateResponse.php +++ b/src/Entity/Response/MotoPaymentCreateResponse.php @@ -5,7 +5,6 @@ use Comgate\SDK\Exception\Api\MissingParamException; use Comgate\SDK\Exception\ApiException; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; class MotoPaymentCreateResponse { @@ -34,7 +33,7 @@ class MotoPaymentCreateResponse */ public function __construct(Response $motoPaymentsCreateResponse) { - $parsedResponse = Query::parse($motoPaymentsCreateResponse->getContent()); + $parsedResponse = json_decode($motoPaymentsCreateResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message']; diff --git a/src/Entity/Response/PaymentCancelResponse.php b/src/Entity/Response/PaymentCancelResponse.php index d3f38a5..4c04bde 100644 --- a/src/Entity/Response/PaymentCancelResponse.php +++ b/src/Entity/Response/PaymentCancelResponse.php @@ -5,7 +5,6 @@ use Comgate\SDK\Exception\Api\MissingParamException; use Comgate\SDK\Exception\ApiException; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; class PaymentCancelResponse { @@ -25,7 +24,7 @@ class PaymentCancelResponse */ public function __construct(Response $cancelPreauthResponse) { - $parsedResponse = Query::parse($cancelPreauthResponse->getContent()); + $parsedResponse = json_decode($cancelPreauthResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message']; diff --git a/src/Entity/Response/PaymentCreateResponse.php b/src/Entity/Response/PaymentCreateResponse.php index 47ae148..d06b6ba 100644 --- a/src/Entity/Response/PaymentCreateResponse.php +++ b/src/Entity/Response/PaymentCreateResponse.php @@ -5,7 +5,6 @@ use Comgate\SDK\Exception\Api\MissingParamException; use Comgate\SDK\Exception\ApiException; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; class PaymentCreateResponse { @@ -33,7 +32,7 @@ class PaymentCreateResponse */ public function __construct(Response $paymentsCreateResponse) { - $parsedResponse = Query::parse($paymentsCreateResponse->getContent()); + $parsedResponse = json_decode($paymentsCreateResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message']; diff --git a/src/Entity/Response/PaymentStatusResponse.php b/src/Entity/Response/PaymentStatusResponse.php index 9269fce..b3024e3 100644 --- a/src/Entity/Response/PaymentStatusResponse.php +++ b/src/Entity/Response/PaymentStatusResponse.php @@ -6,7 +6,6 @@ use Comgate\SDK\Entity\Method; use Comgate\SDK\Entity\Money; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; use Comgate\SDK\Exception\Api\PaymentNotFoundException; use Comgate\SDK\Exception\ApiException; @@ -123,7 +122,7 @@ class PaymentStatusResponse */ public function __construct(Response $paymentStatusResponse) { - $parsedResponse = Query::parse($paymentStatusResponse->getContent()); + $parsedResponse = json_decode($paymentStatusResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message']; @@ -132,22 +131,22 @@ public function __construct(Response $paymentStatusResponse) case 0: $this->setCode($code) ->setMessage($message) - ->setMerchant($parsedResponse['merchant']) - ->setSecret($parsedResponse['secret']) + ->setMerchant($parsedResponse['merchant'] ?? '') + ->setSecret($parsedResponse['secret'] ?? '') ->setTransId($parsedResponse['transId']) - ->setTest($parsedResponse['test'] === 'true') - ->setPrice(Money::ofCents((int) $parsedResponse['price'])) - ->setCurrency($parsedResponse['curr']) - ->setLabel($parsedResponse['label']) - ->setRefId($parsedResponse['refId']) + ->setTest(($parsedResponse['test'] ?? 'false') === 'true') + ->setPrice(Money::ofCents((int) ($parsedResponse['price'] ?? 0))) + ->setCurrency($parsedResponse['curr'] ?? '') + ->setLabel($parsedResponse['label'] ?? '') + ->setRefId($parsedResponse['refId'] ?? '') ->setPayerId($parsedResponse['payerId'] ?? '') - ->setMethod($parsedResponse['method']) + ->setMethod($parsedResponse['method'] ?? '') ->setAccount($parsedResponse['account'] ?? '') ->setEmail($parsedResponse['email'] ?? '') - ->setName($parsedResponse['name']) + ->setName($parsedResponse['name'] ?? '') ->setPhone($parsedResponse['phone'] ?? '') - ->setStatus($parsedResponse['status']) - ->setPayerName($parsedResponse['payerName']) + ->setStatus($parsedResponse['status'] ?? '') + ->setPayerName($parsedResponse['payerName'] ?? '') ->setPayerAcc($parsedResponse['payerAcc'] ?? '') ->setFee($parsedResponse['fee'] ?? '') ->setVs($parsedResponse['vs'] ?? '') diff --git a/src/Entity/Response/PreauthCancelResponse.php b/src/Entity/Response/PreauthCancelResponse.php index 3935a99..739f56c 100644 --- a/src/Entity/Response/PreauthCancelResponse.php +++ b/src/Entity/Response/PreauthCancelResponse.php @@ -6,7 +6,6 @@ use Comgate\SDK\Exception\Api\PreauthException; use Comgate\SDK\Exception\ApiException; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; class PreauthCancelResponse { @@ -27,7 +26,7 @@ class PreauthCancelResponse */ public function __construct(Response $cancelPreauthResponse) { - $parsedResponse = Query::parse($cancelPreauthResponse->getContent()); + $parsedResponse = json_decode($cancelPreauthResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message']; diff --git a/src/Entity/Response/PreauthCaptureResponse.php b/src/Entity/Response/PreauthCaptureResponse.php index 0466cfa..00719e9 100644 --- a/src/Entity/Response/PreauthCaptureResponse.php +++ b/src/Entity/Response/PreauthCaptureResponse.php @@ -6,7 +6,6 @@ use Comgate\SDK\Exception\Api\PreauthException; use Comgate\SDK\Exception\ApiException; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; class PreauthCaptureResponse { @@ -27,7 +26,7 @@ class PreauthCaptureResponse */ public function __construct(Response $capturePreauthResponse) { - $parsedResponse = Query::parse($capturePreauthResponse->getContent()); + $parsedResponse = json_decode($capturePreauthResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message']; diff --git a/src/Entity/Response/PublicCryptoKeyResponse.php b/src/Entity/Response/PublicCryptoKeyResponse.php index 1d64269..001ae40 100644 --- a/src/Entity/Response/PublicCryptoKeyResponse.php +++ b/src/Entity/Response/PublicCryptoKeyResponse.php @@ -4,7 +4,6 @@ namespace Comgate\SDK\Entity\Response; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; use Comgate\SDK\Exception\ApiException; class PublicCryptoKeyResponse @@ -27,7 +26,7 @@ class PublicCryptoKeyResponse */ public function __construct(Response $publicCryptoKeyResponse) { - $parsedResponse = Query::parse($publicCryptoKeyResponse->getContent()); + $parsedResponse = json_decode($publicCryptoKeyResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message'] ?? ''; diff --git a/src/Entity/Response/RecurringPaymentResponse.php b/src/Entity/Response/RecurringPaymentResponse.php index dad4464..74f1c41 100644 --- a/src/Entity/Response/RecurringPaymentResponse.php +++ b/src/Entity/Response/RecurringPaymentResponse.php @@ -5,7 +5,6 @@ use Comgate\SDK\Exception\Api\MissingParamException; use Comgate\SDK\Exception\ApiException; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; class RecurringPaymentResponse { @@ -29,7 +28,7 @@ class RecurringPaymentResponse */ public function __construct(Response $recurringPaymentResponse) { - $parsedResponse = Query::parse($recurringPaymentResponse->getContent()); + $parsedResponse = json_decode($recurringPaymentResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message']; diff --git a/src/Entity/Response/RefundResponse.php b/src/Entity/Response/RefundResponse.php index d34f1fe..95e96a9 100644 --- a/src/Entity/Response/RefundResponse.php +++ b/src/Entity/Response/RefundResponse.php @@ -6,7 +6,6 @@ use Comgate\SDK\Exception\Api\PreauthException; use Comgate\SDK\Exception\ApiException; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; class RefundResponse { @@ -26,7 +25,7 @@ class RefundResponse */ public function __construct(Response $refundResponse) { - $parsedResponse = Query::parse($refundResponse->getContent()); + $parsedResponse = json_decode($refundResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message']; diff --git a/src/Entity/Response/SimulationResponse.php b/src/Entity/Response/SimulationResponse.php index 33b97fc..e5aa1e0 100644 --- a/src/Entity/Response/SimulationResponse.php +++ b/src/Entity/Response/SimulationResponse.php @@ -6,7 +6,6 @@ use Comgate\SDK\Exception\Api\PreauthException; use Comgate\SDK\Exception\ApiException; use Comgate\SDK\Http\Response; -use Comgate\SDK\Http\Query; class SimulationResponse { @@ -25,7 +24,7 @@ class SimulationResponse */ public function __construct(Response $simulationResponse) { - $parsedResponse = Query::parse($simulationResponse->getContent()); + $parsedResponse = json_decode($simulationResponse->getContent(), true); $code = (int) $parsedResponse['code']; $message = $parsedResponse['message']; diff --git a/src/Http/ITransport.php b/src/Http/ITransport.php index 047e0d4..cdde888 100644 --- a/src/Http/ITransport.php +++ b/src/Http/ITransport.php @@ -25,4 +25,10 @@ public function get(string $uri, array $options = []): Response; * @param mixed[] $options */ public function delete(string $uri, array $options = []): Response; + + /** + * @param mixed[] $data + * @param mixed[] $options + */ + public function putJson(string $uri, array $data, array $options = []): Response; } diff --git a/src/Http/Transport.php b/src/Http/Transport.php index c6dc507..08f15b4 100644 --- a/src/Http/Transport.php +++ b/src/Http/Transport.php @@ -115,6 +115,15 @@ public function delete(string $urn, array $options = []): Response return $this->makeRestRequest('DELETE', $urn, [], $options); } + /** + * @param mixed[] $data + * @param mixed[] $options + */ + public function putJson(string $urn, array $data, array $options = []): Response + { + return $this->makeRestRequest('PUT', $urn, $data, $options); + } + /** * @param mixed[] $data * @param mixed[] $options @@ -138,7 +147,7 @@ private function makeRestRequest(string $method, string $urn, array $data = [], curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, true); - if ($method === 'POST' && count($data) != 0) { + if (in_array($method, ['POST', 'PUT'], true) && count($data) != 0) { curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); } diff --git a/tests/Integration/ClientCest.php b/tests/Integration/ClientCest.php index f746949..64ea1c0 100644 --- a/tests/Integration/ClientCest.php +++ b/tests/Integration/ClientCest.php @@ -509,7 +509,7 @@ private function getClient(): Client $client = Comgate::defaults() ->setMerchant($_ENV['API_MERCHANT']) ->setSecret($_ENV['API_SECRET']) - ->setUrl($_ENV['API_URL']) + ->setUrl($_ENV['API_URL_REST']) ->createClient(); return $client; } diff --git a/tests/Unit/Http/TransportCest.php b/tests/Unit/Http/TransportCest.php index b1ddc95..5e56737 100644 --- a/tests/Unit/Http/TransportCest.php +++ b/tests/Unit/Http/TransportCest.php @@ -16,7 +16,7 @@ public function testPostException(UnitTester $I) $client = Comgate::defaults() ->setMerchant($_ENV['API_MERCHANT']) ->setSecret($_ENV['API_SECRET']) - ->setUrl($_ENV['API_URL']) + ->setUrl($_ENV['API_URL_REST']) ->createClient(); $config = new Config('merchant', 'secret');