From cc5e0449e7ddc8803e5321ec589ca102fde6297c Mon Sep 17 00:00:00 2001 From: Lee Siong Chan Date: Fri, 15 Apr 2016 14:55:08 +0800 Subject: [PATCH] Fix currency issue - MOLPay returns currency as 'RM' instead of 'MYR' causing the skey generation failed. - Remove callback notification as it should not be part of Omnipay, user should handle it themselves. --- README.md | 9 +---- src/Gateway.php | 3 +- src/Message/AbstractRequest.php | 2 +- src/Message/CompletePurchaseRequest.php | 39 ++++++------------- src/Message/CompletePurchaseResponse.php | 10 ----- tests/GatewayTest.php | 6 +-- tests/Message/CompletePurchaseRequestTest.php | 6 +-- .../Message/CompletePurchaseResponseTest.php | 7 ---- tests/Message/PurchaseRequestTest.php | 6 +-- 9 files changed, 24 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index cfb0047..79ac007 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ $options = [ 'amount' => '10.00', 'card' => new CreditCard(array( 'country' => 'MY', - 'email' => 'ahlee2326@me.com', + 'email' => 'abc@example.com', 'name' => 'Lee Siong Chan', 'phone' => '0123456789', )), @@ -86,16 +86,9 @@ if ($response->isSuccessful()) { echo $response->getTransactionReference(); } elseif ($response->isPending()) { // Do something -} elseif ($response->isCancelled()) { - // Do something } else { // Error } - -// Check if is a callback notification -if ($response->isCallbackNotification()) { - // Do something -} ``` ## Out Of Scope diff --git a/src/Gateway.php b/src/Gateway.php index d2a1bfe..9dc731a 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -150,8 +150,7 @@ public function completePurchase(array $parameters = array()) array( 'appCode' => $this->httpRequest->request->get('appcode'), 'domain' => $this->httpRequest->request->get('domain'), - 'errorMessage' => $this->httpRequest->request->get('error_desc'), - 'nbcb' => $this->httpRequest->request->get('nbcb'), + 'errorMessage' => strlen($this->httpRequest->request->get('error_desc')) > 0 ? $this->httpRequest->request->get('error_desc') : null, 'payDate' => $this->httpRequest->request->get('paydate'), 'sKey' => $this->httpRequest->request->get('skey'), 'status' => $this->httpRequest->request->get('status'), diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index c301798..0d503d8 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -175,7 +175,7 @@ protected function validatePaymentMethod() { $this->validate('paymentMethod'); - $paymentMethod = $this->getPaymentMethod(); + $paymentMethod = strtolower($this->getPaymentMethod()); if ( PaymentMethod::AFFIN_BANK !== $paymentMethod && diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index 3f93439..e98fb84 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -14,9 +14,6 @@ * * domain [required] - Merchant ID in MOLPay system * * errorMessage - Error message * * payDate [required] - Date/Time of the transaction - * * nbcb - Notification from MOLPay - * - 1 for callback notification - * - 2 for notification * * sKey [required] - Data integrity protection hash string. * * status [required] - Payment status * - 00 for successful payment @@ -116,28 +113,6 @@ public function setPayDate($value) return $this->setParameter('payDate', $value); } - /** - * Get nbcb. - * - * @return string - */ - public function getNBCB() - { - return $this->getParameter('nbcb'); - } - - /** - * Set nbcb. - * - * @param string $value - * - * @return $this - */ - public function setNBCB($value) - { - return $this->setParameter('nbcb', $value); - } - /** * Get sKey. * @@ -224,7 +199,6 @@ public function getData() } return array( - 'nbcb' => $this->getNBCB(), 'status' => $this->getStatus(), 'transactionId' => $this->getTransactionId(), 'transactionReference' => $this->getTransactionReference(), @@ -253,7 +227,7 @@ protected function generatePreSKey() { $this->validate('amount', 'currency', 'domain', 'status', 'transactionId', 'transactionReference'); - return md5($this->getTransactionReference().$this->getTransactionId().$this->getStatus().$this->getDomain().$this->getAmount().$this->getCurrency()); + return md5($this->getTransactionReference().$this->getTransactionId().$this->getStatus().$this->getDomain().$this->getAmount().$this->convertCurrency()); } /** @@ -267,4 +241,15 @@ protected function generateSKey() return md5($this->getPayDate().$this->getDomain().$this->generatePreSKey().$this->getAppCode().$this->getVerifyKey()); } + + /** + * Convert currency to 'MOLPay''s style currency. + * @NOTE Funny enough, MOLPay returns currency as 'RM' not 'MYR'. And I am not sure about other currencies though. + * + * @return string + */ + private function convertCurrency() + { + return $this->getCurrency() !== 'MYR' ? $this->getCurrency() : 'RM'; + } } diff --git a/src/Message/CompletePurchaseResponse.php b/src/Message/CompletePurchaseResponse.php index a7cb45c..2214cf1 100644 --- a/src/Message/CompletePurchaseResponse.php +++ b/src/Message/CompletePurchaseResponse.php @@ -32,16 +32,6 @@ public function getTransactionReference() return $this->getRequest()->getTransactionReference(); } - /** - * Check if the request is a callback notification. - * - * @return bool - */ - public function isCallbackNotification() - { - return '1' === $this->getRequest()->getNBCB(); - } - /** * {@inheritdoc} */ diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index 93c911f..e84e379 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -27,7 +27,7 @@ public function setUp() 'amount' => '10.00', 'card' => new CreditCard(array( 'country' => 'MY', - 'email' => 'ahlee2326@me.com', + 'email' => 'abc@example.com', 'name' => 'Lee Siong Chan', 'phone' => '0123456789', )), @@ -45,7 +45,7 @@ public function testPurchase() $this->assertTrue($response->isRedirect()); $this->assertNull($response->getTransactionReference()); $this->assertEquals( - 'https://www.onlinepayment.com.my/MOLPay/pay/test1234/?amount=10.00&bill_desc=Test+Payment&bill_email=ahlee2326%40me.com&bill_mobile=0123456789&bill_name=Lee+Siong+Chan&channel=credit&country=MY¤cy=MYR&langcode=en&orderid=20160331082207680000&vcode=f3d5496b444ae3d11e09fa92a753ac60', + 'https://www.onlinepayment.com.my/MOLPay/pay/test1234/?amount=10.00&bill_desc=Test+Payment&bill_email=abc%40example.com&bill_mobile=0123456789&bill_name=Lee+Siong+Chan&channel=credit&country=MY¤cy=MYR&langcode=en&orderid=20160331082207680000&vcode=f3d5496b444ae3d11e09fa92a753ac60', $response->getRedirectUrl() ); } @@ -56,7 +56,7 @@ public function testCompletePurchaseSuccess() 'appcode' => 'abcdefg', 'domain' => 'test4321', 'paydate' => '2016-03-29 04:02:21', - 'skey' => '0c1e98d6d58f9a30b919bcaeb9790852', + 'skey' => '9b8be764cc5bad1b4a5d58a3ba4daf58', 'status' => '00', 'tranID' => '000001', )); diff --git a/tests/Message/CompletePurchaseRequestTest.php b/tests/Message/CompletePurchaseRequestTest.php index f754354..8856cb9 100644 --- a/tests/Message/CompletePurchaseRequestTest.php +++ b/tests/Message/CompletePurchaseRequestTest.php @@ -16,7 +16,7 @@ public function setUp() 'currency' => 'MYR', 'domain' => 'test4321', 'payDate' => '2016-03-29 04:02:21', - 'sKey' => '0c1e98d6d58f9a30b919bcaeb9790852', + 'sKey' => '9b8be764cc5bad1b4a5d58a3ba4daf58', 'status' => '00', 'transactionId' => '20160331082207680000', 'transactionReference' => '000001', @@ -35,7 +35,7 @@ public function testGetData() public function testSendSuccess() { $this->request->setStatus('00'); - $this->request->setSKey('0c1e98d6d58f9a30b919bcaeb9790852'); + $this->request->setSKey('9b8be764cc5bad1b4a5d58a3ba4daf58'); $response = $this->request->send(); @@ -49,7 +49,7 @@ public function testSendSuccess() public function testSendPending() { $this->request->setStatus('22'); - $this->request->setSKey('8192eb5627012f236fc36e73f5d49a5e'); + $this->request->setSKey('9d65ed0b785fea1c8fc80b8316555ee3'); $response = $this->request->send(); diff --git a/tests/Message/CompletePurchaseResponseTest.php b/tests/Message/CompletePurchaseResponseTest.php index 2b51492..2926139 100644 --- a/tests/Message/CompletePurchaseResponseTest.php +++ b/tests/Message/CompletePurchaseResponseTest.php @@ -28,11 +28,4 @@ public function testCompletePurchasePending() $this->assertTrue($this->response->isPending()); } - - public function testCompletePurchaseCallbackNotification() - { - $this->getMockRequest()->shouldReceive('getNBCB')->andReturn('1'); - - $this->assertTrue($this->response->isCallbackNotification()); - } } diff --git a/tests/Message/PurchaseRequestTest.php b/tests/Message/PurchaseRequestTest.php index 02aba28..589246b 100644 --- a/tests/Message/PurchaseRequestTest.php +++ b/tests/Message/PurchaseRequestTest.php @@ -15,7 +15,7 @@ public function setUp() 'amount' => '10.00', 'card' => new CreditCard(array( 'country' => 'MY', - 'email' => 'ahlee2326@me.com', + 'email' => 'abc@example.com', 'name' => 'Lee Siong Chan', 'phone' => '0123456789', )), @@ -35,7 +35,7 @@ public function testGetData() $this->assertEquals('10.00', $data['amount']); $this->assertEquals('MY', $data['country']); - $this->assertEquals('ahlee2326@me.com', $data['bill_email']); + $this->assertEquals('abc@example.com', $data['bill_email']); $this->assertEquals('Lee Siong Chan', $data['bill_name']); $this->assertEquals('0123456789', $data['bill_mobile']); $this->assertEquals('MYR', $data['currency']); @@ -54,7 +54,7 @@ public function testSendSuccess() $this->assertTrue($response->isRedirect()); $this->assertNull($response->getTransactionReference()); $this->assertEquals( - 'https://www.onlinepayment.com.my/MOLPay/pay/test1234/?amount=10.00&bill_desc=Test+Payment&bill_email=ahlee2326%40me.com&bill_mobile=0123456789&bill_name=Lee+Siong+Chan&channel=credit&country=MY¤cy=MYR&langcode=en&orderid=20160331082207680000&vcode=f3d5496b444ae3d11e09fa92a753ac60', + 'https://www.onlinepayment.com.my/MOLPay/pay/test1234/?amount=10.00&bill_desc=Test+Payment&bill_email=abc%40example.com&bill_mobile=0123456789&bill_name=Lee+Siong+Chan&channel=credit&country=MY¤cy=MYR&langcode=en&orderid=20160331082207680000&vcode=f3d5496b444ae3d11e09fa92a753ac60', $response->getRedirectUrl() ); }