diff --git a/src/AbstractDatatransGateway.php b/src/AbstractDatatransGateway.php index cc3bd36..a026034 100644 --- a/src/AbstractDatatransGateway.php +++ b/src/AbstractDatatransGateway.php @@ -41,7 +41,7 @@ public function getDefaultParameters() self::RETURN_METHOD_POST, self::RETURN_METHOD_GET, ], - 'errorUrl' => '', + 'errorUrl' => null, 'language' => [ null, // account default 'de', // German diff --git a/src/Message/AbstractRedirectRequest.php b/src/Message/AbstractRedirectRequest.php index 7eb6ae4..db1ece5 100644 --- a/src/Message/AbstractRedirectRequest.php +++ b/src/Message/AbstractRedirectRequest.php @@ -218,7 +218,11 @@ public function getData() // These URLs are optional here, if set in the account. if ($this->getReturnUrl() !== null) { + // Default the errorUrl to the same returnURL. + // It can be overridden later if required. + $data['successUrl'] = $this->getReturnUrl(); + $data['errorUrl'] = $this->getReturnUrl(); } if ($this->getCancelUrl() !== null) { diff --git a/src/Message/TokenizeRequest.php b/src/Message/TokenizeRequest.php index 3d03325..85bce3b 100644 --- a/src/Message/TokenizeRequest.php +++ b/src/Message/TokenizeRequest.php @@ -31,7 +31,7 @@ class TokenizeRequest extends AbstractRedirectRequest */ public function getData() { - $this->validate('merchantId', 'transactionId', 'sign', 'returnUrl', 'errorUrl', 'cancelUrl'); + $this->validate('merchantId', 'transactionId', 'sign', 'returnUrl', 'cancelUrl'); $data = array( 'merchantId' => $this->getMerchantId(), @@ -44,7 +44,7 @@ public function getData() $data['successUrl'] = $this->getReturnUrl(); $data['cancelUrl'] = $this->getCancelUrl(); - $data['errorUrl'] = $this->getErrorUrl(); + $data['errorUrl'] = $this->getErrorUrl() ?: $this->getReturnUrl(); $data['cancelUrl'] = $this->getCancelUrl(); return $data; diff --git a/tests/Message/PurchaseRequestTest.php b/tests/Message/PurchaseRequestTest.php index 483eb8a..b4762f1 100644 --- a/tests/Message/PurchaseRequestTest.php +++ b/tests/Message/PurchaseRequestTest.php @@ -47,4 +47,35 @@ public function testGetDataWithoutCard() $this->assertEquals($expected, $this->request->getData()); } + + /** + * No errorUrl set explicitly. + */ + public function testErrorUrlDefaults() + { + $this->request->initialize(array( + 'merchantId' => 'asdf', + 'sign' => '123', + 'testMode' => true, + 'amount' => '12.00', + 'currency' => 'CHF', + 'transactionId' => '123', + 'returnUrl' => 'https://www.example.com/return', + 'cancelUrl' => 'https://www.example.com/cancel' + )); + + $expected = array( + 'merchantId' => 'asdf', + 'refno' => '123', + 'amount' => 1200, + 'currency' => 'CHF', + 'sign' => '123', + 'reqtype' => 'CAA', + 'successUrl' => 'https://www.example.com/return', + 'errorUrl' => 'https://www.example.com/return', + 'cancelUrl' => 'https://www.example.com/cancel' + ); + + $this->assertEquals($expected, $this->request->getData()); + } } diff --git a/tests/Message/TokenizeRequestTest.php b/tests/Message/TokenizeRequestTest.php new file mode 100644 index 0000000..7dfc00e --- /dev/null +++ b/tests/Message/TokenizeRequestTest.php @@ -0,0 +1,79 @@ +request = new TokenizeRequest($this->getHttpClient(), $this->getHttpRequest()); + } + + public function testGetDataWithoutCard() + { + $this->request->initialize(array( + 'merchantId' => 'asdf', + 'sign' => '123', + 'testMode' => true, + 'currency' => 'CHF', + 'transactionId' => '123', + 'returnUrl' => 'https://www.example.com/success', + 'errorUrl' => 'https://www.example.com/error', + 'cancelUrl' => 'https://www.example.com/cancel' + )); + + $expected = array( + 'merchantId' => 'asdf', + 'refno' => '123', + 'amount' => 0, + 'currency' => 'CHF', + 'sign' => '123', + 'successUrl' => 'https://www.example.com/success', + 'errorUrl' => 'https://www.example.com/error', + 'cancelUrl' => 'https://www.example.com/cancel', + 'useAlias' => 'yes', + ); + + $this->assertEquals($expected, $this->request->getData()); + } + + /** + * No errorUrl set explicitly. + */ + public function testErrorUrlDefaults() + { + $this->request->initialize(array( + 'merchantId' => 'asdfxxx', + 'sign' => '123', + 'testMode' => true, + 'currency' => 'CHF', + 'transactionId' => '123', + 'returnUrl' => 'https://www.example.com/return', + 'cancelUrl' => 'https://www.example.com/cancel' + )); + + $expected = array( + 'merchantId' => 'asdfxxx', + 'refno' => '123', + 'amount' => 0, + 'currency' => 'CHF', + 'sign' => '123', + 'successUrl' => 'https://www.example.com/return', + 'errorUrl' => 'https://www.example.com/return', + 'cancelUrl' => 'https://www.example.com/cancel', + 'useAlias' => 'yes', + ); + + $this->assertEquals($expected, $this->request->getData()); + } +}