diff --git a/composer.json b/composer.json index 41ff5f8..2ea88e0 100644 --- a/composer.json +++ b/composer.json @@ -20,16 +20,16 @@ } ], "autoload": { - "psr-4": { "League\\Omnipay\\MOLPay\\" : "src/" } + "psr-4": { "Omnipay\\MOLPay\\" : "src/" } }, "autoload-dev": { - "psr-4": { "League\\Omnipay\\MOLPay\\Tests\\": "tests/" } + "psr-4": { "Omnipay\\MOLPay\\Tests\\": "tests/" } }, "require": { - "omnipay/omnipay": "~3.0@dev" + "omnipay/common": "^3" }, "require-dev": { - "omnipay/tests": "~3.0@dev" + "omnipay/tests": "^3" }, "extra": { "branch-alias": { diff --git a/src/Exception/InvalidCreditCardDetailsException.php b/src/Exception/InvalidCreditCardDetailsException.php index c1e8e7b..d6a1d24 100644 --- a/src/Exception/InvalidCreditCardDetailsException.php +++ b/src/Exception/InvalidCreditCardDetailsException.php @@ -1,8 +1,8 @@ false, 'locale' => 'en', 'testMode' => false, + 'httpMethod' => 'GET', ); } @@ -78,6 +79,18 @@ public function setLocale($value) return $this->setParameter('locale', $value); } + /** + * Set the HTTP Method. + * + * + * @param string $value + * + * @return $this + */ + public function setHttpMethod($value) + { + return $this->setParameter('httpMethod', $value); + } /** * Get merchantId. * @@ -122,16 +135,59 @@ public function setVerifyKey($value) return $this->setParameter('verifyKey', $value); } + /** + * Get secretKey. + * + * @return string + */ + public function getSecretKey() + { + return $this->getParameter('secretKey'); + } + + /** + * Set secretKey. + * + * @param string $value + * + * @return $this + */ + public function setSecretKey($value) + { + return $this->setParameter('secretKey', $value); + } + + /** + * Gets the test mode of the request from the gateway. + * + * @return boolean + */ + public function getTestMode() + { + return $this->getParameter('testMode'); + } + + /** + * Sets the test mode of the request. + * + * @param boolean $value True for test mode on. + * @return AbstractRequest + */ + public function setTestMode($value) + { + return $this->setParameter('testMode', $value); + } + /** * Create a purchase request. * * @param array $parameters * - * @return \League\Omnipay\MOLPay\Message\PurchaseRequest + * @return \Omnipay\MOLPay\Message\PurchaseRequest */ public function purchase(array $parameters = array()) { - return $this->createRequest('\League\Omnipay\MOLPay\Message\PurchaseRequest', $parameters); + return $this->createRequest('\Omnipay\MOLPay\Message\PurchaseRequest', $parameters); } /** @@ -139,26 +195,47 @@ public function purchase(array $parameters = array()) * * @param array $parameters * - * @return \League\Omnipay\MOLPay\Message\CompletePurchaseRequest + * @return \Omnipay\MOLPay\Message\CompletePurchaseRequest */ public function completePurchase(array $parameters = array()) { - $parsedBody = $this->httpRequest->getParsedBody(); - return $this->createRequest( - '\League\Omnipay\MOLPay\Message\CompletePurchaseRequest', + '\Omnipay\MOLPay\Message\CompletePurchaseRequest', array_merge( $parameters, array( - 'appCode' => isset($parsedBody['appcode']) ? $parsedBody['appcode'] : null, - 'domain' => isset($parsedBody['domain']) ? $parsedBody['domain'] : null, - 'errorMessage' => isset($parsedBody['error_desc']) && strlen($parsedBody['error_desc']) > 0 ? $parsedBody['error_desc'] : null, - 'payDate' => isset($parsedBody['paydate']) ? $parsedBody['paydate'] : null, - 'sKey' => isset($parsedBody['skey']) ? $parsedBody['skey'] : null, - 'status' => isset($parsedBody['status']) ? $parsedBody['status'] : null, - 'transactionReference' => isset($parsedBody['tranID']) ? $parsedBody['tranID'] : null, + 'appCode' => $this->httpRequest->request->get('appcode'), + 'domain' => $this->httpRequest->request->get('domain'), + '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'), + 'transactionReference' => $this->httpRequest->request->get('tranID'), + 'channel' => $this->httpRequest->request->get('channel') ) ) ); } + + /** + * Create a refund request + * + * @param array $parameters + * @return \Omnipay\Common\Message\AbstractRequest + */ + public function refund(array $parameters = array()) + { + return $this->createRequest('\Omnipay\MOLPay\Message\PartialRefundRequest', $parameters); + } + + /** + * Create a void request + * + * @param array $parameters + * @return \Omnipay\Common\Message\AbstractRequest + */ + public function void(array $parameters = array()) + { + return $this->createRequest('\Omnipay\MOLPay\Message\ReversalRequest', $parameters); + } } diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index ec15a72..2c0b17f 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -1,16 +1,16 @@ setParameter('locale', $value); } + + /** + * Get HTTP Method. + * + * @return string + */ + public function getHttpMethod() + { + return $this->getParameter('httpMethod'); + } + + /** + * Set HTTP Method. + * + * @param string $value + * + * @return $this + */ + public function setHttpMethod($value) + { + return $this->setParameter('httpMethod', $value); + } + /** * Get merchantId. * @@ -114,6 +144,49 @@ public function setVerifyKey($value) return $this->setParameter('verifyKey', $value); } + /** + * Get secretKey. + * + * @return string + */ + public function getSecretKey() + { + return $this->getParameter('secretKey'); + } + + /** + * Set secretKey. + * + * @param string $value + * + * @return $this + */ + public function setSecretKey($value) + { + return $this->setParameter('secretKey', $value); + } + + /** + * Gets the test mode of the request from the gateway. + * + * @return boolean + */ + public function getTestMode() + { + return $this->getParameter('testMode'); + } + + /** + * Sets the test mode of the request. + * + * @param boolean $value True for test mode on. + * @return AbstractRequest + */ + public function setTestMode($value) + { + return $this->setParameter('testMode', $value); + } + /** * Get endpoint. * @@ -123,7 +196,7 @@ public function getEndpoint() { $this->validate('merchantId'); - return $this->endpoint.$this->getMerchantId().'/'; + return ($this->getTestMode() ? $this->sandboxEndpoint : $this->endpoint) . $this->getMerchantId() . '/'; } /** @@ -131,7 +204,7 @@ public function getEndpoint() */ protected function sendIPN() { - $data = $this->httpRequest->getQueryParams(); + $data = $this->httpRequest->request->all(); $data['treq'] = 1; // Additional parameter required by IPN @@ -149,16 +222,12 @@ protected function validateCreditCardDetails() { $this->validate('card'); - $customer = $this->getCard()->getCustomer(); - - if (null === $customer) { - throw new InvalidCreditCardDetailsException('Customer is required'); - } + $card = $this->getCard(); foreach (array('country', 'email', 'name', 'phone') as $key) { - $method = 'get'.ucfirst(Helper::camelCase($key)); + $method = 'get' . ucfirst(Helper::camelCase($key)); - if (null === $customer->$method()) { + if (null === $card->$method()) { throw new InvalidCreditCardDetailsException("The $key parameter is required"); } } @@ -166,29 +235,19 @@ protected function validateCreditCardDetails() /** * Validate payment method: - * - Affin Bank - * - AmOnline - * - CIMB Clicks - * - Credit Card - * - FPX - * - Hong Leong Connect - * - Maybank2u - * - RHB Now. */ protected function validatePaymentMethod() { $this->validate('paymentMethod'); - $paymentMethod = strtolower($this->getPaymentMethod()); - - if (PaymentMethod::AFFIN_BANK !== $paymentMethod && - PaymentMethod::AM_ONLINE !== $paymentMethod && - PaymentMethod::CIMB_CLICKS !== $paymentMethod && - PaymentMethod::CREDIT_CARD !== $paymentMethod && - PaymentMethod::FPX !== $paymentMethod && - PaymentMethod::HONG_LEONG_CONNECT !== $paymentMethod && - PaymentMethod::MAYBANK2U !== $paymentMethod && - PaymentMethod::RHB_NOW !== $paymentMethod) { + $methods = PaymentMethod::supported(); + $supported = false; + foreach ($methods as $method) { + if ($paymentMethod == strtolower($method)) { + $supported = true; + } + } + if (!$supported) { throw new InvalidPaymentMethodException("The payment method ($paymentMethod) is not supported"); } } diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index d600173..9100508 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -1,8 +1,8 @@ setParameter('transactionReference', $value); } + public function getChannel() + { + return $this->getParameter('channel'); + } + + public function setChannel($value) + { + return $this->setParameter('channel', $value); + } + /** * {@inheritdoc} */ @@ -226,16 +236,18 @@ public function sendData($data) /** * Generate pre-sKey - first hash encryption. * + * @param bool $fallbackMode + * * @return string */ protected function generatePreSKey($fallbackMode = false) { $this->validate('amount', 'currency', 'domain', 'status', 'transactionId', 'transactionReference'); - $preSKey = md5($this->getTransactionReference().$this->getTransactionId().$this->getStatus().$this->getDomain().$this->getAmount()->getFormatted().$this->getCurrency()); + $preSKey = md5($this->getTransactionReference().$this->getTransactionId().$this->getStatus().$this->getDomain().$this->getAmount().$this->getCurrency()); if ($fallbackMode) { - $preSKey = md5($this->getTransactionReference().$this->getTransactionId().$this->getStatus().$this->getDomain().$this->getAmount()->getFormatted().$this->getConvertedCurrency()); + $preSKey = md5($this->getTransactionReference().$this->getTransactionId().$this->getStatus().$this->getDomain().$this->getAmount().$this->getConvertedCurrency()); } return $preSKey; @@ -244,13 +256,15 @@ protected function generatePreSKey($fallbackMode = false) /** * Generate sKey - final hash encryption. * + * @param bool $fallbackMode + * * @return string */ protected function generateSKey($fallbackMode = false) { - $this->validate('appCode', 'domain', 'payDate', 'verifyKey'); + $this->validate('appCode', 'domain', 'payDate', 'secretKey'); - return md5($this->getPayDate().$this->getDomain().$this->generatePreSKey($fallbackMode).$this->getAppCode().$this->getVerifyKey()); + return md5($this->getPayDate().$this->getDomain().$this->generatePreSKey($fallbackMode).$this->getAppCode().$this->getSecretKey()); } /** diff --git a/src/Message/CompletePurchaseResponse.php b/src/Message/CompletePurchaseResponse.php index 7de134b..2214cf1 100644 --- a/src/Message/CompletePurchaseResponse.php +++ b/src/Message/CompletePurchaseResponse.php @@ -1,8 +1,8 @@ setParameter('refId', $value); + } + + /** + * @return mixed + */ + public function getRefId() + { + return $this->getParameter('refId'); + } + + /** + * @param $value + * @return \Omnipay\Common\Message\AbstractRequest + */ + public function setChannel($value) + { + return $this->setParameter('channel', $value); + } + + /** + * @return mixed + */ + public function getChannel() + { + return $this->getParameter('channel'); + } + + /** + * @param $value + * @return \Omnipay\Common\Message\AbstractRequest + */ + public function setBankCode($value) + { + return $this->setParameter('bankCode', $value); + } + + /** + * @return mixed + */ + public function getBankCode() + { + return $this->getParameter('bankCode'); + } + + /** + * @param $value + * @return \Omnipay\Common\Message\AbstractRequest + */ + public function setBeneficiaryName($value) + { + return $this->setParameter('beneficiaryName', $value); + } + + /** + * @return mixed + */ + public function getBeneficiaryName() + { + return $this->getParameter('beneficiaryName'); + } + + /** + * @param $value + * @return \Omnipay\Common\Message\AbstractRequest + */ + public function setBeneficiaryAccountNo($value) + { + return $this->setParameter('beneficiaryAccountNo', $value); + } + + /** + * @return mixed + */ + public function getBeneficiaryAccountNo() + { + return $this->getParameter('beneficiaryAccountNo'); + } + + /** + * @param $value + * @return \Omnipay\Common\Message\AbstractRequest + */ + public function setMdrFlag($value) + { + return $this->setParameter('mdrFlag', $value); + } + + /** + * @return mixed + */ + public function getMdrFlag() + { + return $this->getParameter('mdrFlag'); + } + + /** + * {@inheritdoc} + */ + public function getData() + { + $this->validate('merchantId', 'refId', 'transactionReference', 'amount'); + + $data = array(); + $data['RefundType'] = $this->getRefundType(); + $data['MerchantID'] = $this->getMerchantId(); + $data['RefID'] = $this->getRefId(); + $data['TxnID'] = $this->getTransactionReference(); + $data['Channel'] = $this->getChannel(); + $data['Amount'] = $this->getAmount(); + $data['BankCode'] = $this->getBankCode(); + $data['BeneficiaryName'] = $this->getBeneficiaryName(); + $data['BeneficiaryAccNo'] = $this->getBeneficiaryAccountNo(); + $data['Signature'] = $this->generateSignature(); + $data['mdr_flag'] = $this->getMdrFlag(); + $data['notify_url'] = $this->getNotifyUrl(); + + return $data; + } + + /** + * {@inheritdoc} + */ + public function getEndpoint() + { + return $this->getTestMode() ? $this->sandboxEndpoint : $this->endpoint; + } + + /** + * {@inheritdoc} + */ + public function getHttpMethod() + { + return 'POST'; + } + + /** + * {@inheritdoc} + */ + public function sendData($data) + { + $httpRequest = $this->httpClient->createRequest( + $this->getHttpMethod(), + $this->getEndpoint(), + null, + $data + ); + + $httpResponse = $httpRequest->send(); + + return $this->response = new PartialRefundResponse($this, $httpResponse->json()); + } + + /** + * Generate Signature + * @return string + */ + protected function generateSignature() + { + $this->validate('merchantId', 'refId', 'transactionReference', 'amount', 'secretKey'); + + return md5($this->getRefundType() . $this->getMerchantId() .$this->getRefId() . $this->getTransactionReference() . $this->getAmount() . $this->getSecretKey()); + } +} diff --git a/src/Message/PartialRefundResponse.php b/src/Message/PartialRefundResponse.php new file mode 100644 index 0000000..90a1e85 --- /dev/null +++ b/src/Message/PartialRefundResponse.php @@ -0,0 +1,74 @@ +data)) { + return $this->data['error_desc']; + } + // Handle MOLPay return success with status 'Rejected' + else if (array_key_exists('reason', $this->data)) { + return $this->data['reason']; + } + // Handle MOLPay returned unknown exceptions that not specified in spec + else { + return 'Unknown error'; + } + } + + /** + * {@inheritdoc} + */ + public function isSuccessful() + { + if (array_key_exists('error_code', $this->data)) { + return false; + } + + // API returned 'success', not actual '00' at this development time + return ($this->data['Status'] === '00' || strtolower($this->data['Status']) === 'success'); + } + + /** + * {@inheritdoc} + */ + public function isPending() + { + if (array_key_exists('error_code', $this->data)) { + return false; + } + + // API returned 'pending', not actual '22' at this development time + return ($this->data['Status'] === '22' || strtolower($this->data['Status']) === 'pending'); + } +} diff --git a/src/Message/PurchaseRequest.php b/src/Message/PurchaseRequest.php index b405bb3..8dfe53a 100644 --- a/src/Message/PurchaseRequest.php +++ b/src/Message/PurchaseRequest.php @@ -1,6 +1,6 @@ validatePaymentMethod(); } - $customer = $this->getCard()->getCustomer(); + $card = $this->getCard(); return array( - 'amount' => $this->getAmount()->getFormatted(), + 'amount' => $this->getAmount(), 'bill_desc' => $this->getDescription(), - 'bill_email' => $customer->getEmail(), - 'bill_mobile' => $customer->getPhone(), - 'bill_name' => $customer->getName(), + 'bill_email' => $card->getEmail(), + 'bill_mobile' => $card->getPhone(), + 'bill_name' => $card->getName(), 'channel' => $this->getPaymentMethod(), - 'country' => $customer->getCountry(), + 'country' => $card->getCountry(), 'currency' => $this->getCurrency(), 'langcode' => $this->getLocale(), 'orderid' => $this->getTransactionId(), 'vcode' => $this->generateVCode(), + 'returnurl' => $this->getReturnUrl(), + 'cancelurl' => $this->getCancelUrl(), ); } @@ -62,6 +64,6 @@ protected function generateVCode() { $this->validate('amount', 'merchantId', 'transactionId', 'verifyKey'); - return md5($this->getAmount()->getFormatted().$this->getMerchantId().$this->getTransactionId().$this->getVerifyKey()); + return md5($this->getAmount().$this->getMerchantId().$this->getTransactionId().$this->getVerifyKey()); } } diff --git a/src/Message/PurchaseResponse.php b/src/Message/PurchaseResponse.php index e315c40..9c06b43 100644 --- a/src/Message/PurchaseResponse.php +++ b/src/Message/PurchaseResponse.php @@ -1,9 +1,9 @@ data; } /** @@ -20,7 +20,7 @@ public function getRedirectData() */ public function getRedirectMethod() { - return 'GET'; + return $this->getRequest()->getHttpMethod(); } /** @@ -28,7 +28,10 @@ public function getRedirectMethod() */ public function getRedirectUrl() { - return $this->getRequest()->getEndpoint().'?'.http_build_query($this->data); + if ($this->getRedirectMethod() == "GET") { + return $this->getRequest()->getEndpoint() . '?' . http_build_query($this->data); + } + return $this->getRequest()->getEndpoint(); } /** diff --git a/src/Message/ReversalRequest.php b/src/Message/ReversalRequest.php new file mode 100644 index 0000000..eb607e5 --- /dev/null +++ b/src/Message/ReversalRequest.php @@ -0,0 +1,93 @@ +validate('transactionReference', 'merchantId'); + + $data = array(); + $data['txnID'] = $this->getTransactionReference(); + $data['domain'] = $this->getMerchantId(); + $data['skey'] = $this->generateSKey(); + + return $data; + } + + /** + * {@inheritdoc} + */ + public function getEndpoint() + { + return $this->getTestMode() ? $this->sandboxEndpoint : $this->endpoint; + } + + /** + * {@inheritdoc} + */ + public function getHttpMethod() + { + return 'POST'; + } + + /** + * {@inheritdoc} + */ + public function sendData($data) + { + $httpRequest = $this->httpClient->createRequest( + $this->getHttpMethod(), + $this->getEndpoint(), + null, + $data + ); + + $httpResponse = $httpRequest->send(); + + return $this->response = new ReversalResponse($this, $httpResponse->getBody()); + } + + /** + * Generate SKey + * @return string + */ + protected function generateSKey() + { + $this->validate('transactionReference', 'merchantId', 'secretKey'); + + return md5($this->getTransactionReference() . $this->getMerchantId() . $this->getSecretKey()); + } +} diff --git a/src/Message/ReversalResponse.php b/src/Message/ReversalResponse.php new file mode 100644 index 0000000..ca7abdd --- /dev/null +++ b/src/Message/ReversalResponse.php @@ -0,0 +1,77 @@ + 'Success', + '11' => 'Failure', + '12' => 'Invalid or unmatched security hash string', + '13' => 'Not a refundable transaction', + '14' => 'Transaction date more than 45 days', + '15' => 'Requested day is on settlement day', + '16' => 'Forbidden transaction', + '17' => 'Transaction not found' + ); + + + /** + * ReversalResponse constructor. + * @param RequestInterface $request + * @param mixed $data + */ + public function __construct(RequestInterface $request, $data) + { + $this->request = $request; + + $search = array("\r\n", "\n", "\r"); + $data = str_replace($search, '&', $data); + + // Parse string to key value mapping + parse_str($data, $this->data); + } + + /** + * {@inheritdoc} + */ + public function getMessage() + { + return $this->statCodeMessages[$this->data['StatCode']]; + } + + /** + * {@inheritdoc} + */ + public function isSuccessful() + { + return '00' === $this->data['StatCode']; + } +} diff --git a/src/PaymentMethod.php b/src/PaymentMethod.php index bbb002b..666987f 100644 --- a/src/PaymentMethod.php +++ b/src/PaymentMethod.php @@ -1,18 +1,27 @@ mockGateway($this->getHttpRequest()); + $this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest()); + + $this->gateway->setTestMode(false); + $this->gateway->setCurrency('MYR'); + $this->gateway->setLocale('en'); + $this->gateway->setMerchantId('test1234'); + $this->gateway->setVerifyKey('abcdefg'); + $this->gateway->setSecretKey('hilkjmn'); $this->options = array( - 'amount' => 1000, // In cents form (eg. 1000 cents = $10.00) + 'amount' => '10.00', 'card' => new CreditCard(array( - 'customer' => new Customer(array( - 'country' => 'MY', - 'email' => 'abc@example.com', - 'name' => 'Lee Siong Chan', - 'phone' => '0123456789', - )), + 'country' => 'MY', + 'email' => 'abc@example.com', + 'name' => 'Lee Siong Chan', + 'phone' => '0123456789', )), 'description' => 'Test Payment', 'transactionId' => '20160331082207680000', @@ -50,17 +54,15 @@ public function testPurchase() public function testCompletePurchaseSuccess() { - $request = $this->getHttpRequest()->withParsedBody(array( + $this->getHttpRequest()->request->replace(array( 'appcode' => 'abcdefg', 'domain' => 'test4321', 'paydate' => '2016-03-29 04:02:21', - 'skey' => '9b8be764cc5bad1b4a5d58a3ba4daf58', + 'skey' => '0be898400610105af17b3a462c44241b', 'status' => '00', 'tranID' => '000001', )); - $this->mockGateway($request); - $response = $this->gateway->completePurchase($this->options)->send(); $this->assertTrue($response->isSuccessful()); @@ -68,11 +70,11 @@ public function testCompletePurchaseSuccess() } /** - * @expectedException \League\Omnipay\Common\Exception\InvalidResponseException + * @expectedException \Omnipay\Common\Exception\InvalidResponseException */ public function testCompletePurchaseInvalidSKey() { - $request = $this->getHttpRequest()->withParsedBody(array( + $this->getHttpRequest()->request->replace(array( 'appcode' => 'abcdefg', 'domain' => 'test4321', 'paydate' => '2016-03-29 04:02:21', @@ -81,17 +83,15 @@ public function testCompletePurchaseInvalidSKey() 'tranID' => '000001', )); - $this->mockGateway($request); - $response = $this->gateway->completePurchase($this->options)->send(); } /** - * @expectedException \League\Omnipay\Common\Exception\InvalidResponseException + * @expectedException \Omnipay\Common\Exception\InvalidResponseException */ public function testCompletePurchaseError() { - $request = $this->getHttpRequest()->withParsedBody(array( + $this->getHttpRequest()->request->replace(array( 'appcode' => 'abcdefg', 'domain' => 'test4321', 'paydate' => 'I am not a date', @@ -101,8 +101,6 @@ public function testCompletePurchaseError() 'tranID' => '000001', )); - $this->mockGateway($request); - $response = $this->gateway->completePurchase($this->options)->send(); $this->assertFalse($response->isSuccessful()); $this->assertFalse($response->isRedirect()); @@ -110,13 +108,42 @@ public function testCompletePurchaseError() $this->assertEquals('Invalid date', $response->getMessage()); } - private function mockGateway($request) + public function testVoid() { - $this->gateway = new Gateway($this->getHttpClient(), $request); + $request = $this->gateway->void(array( + 'transactionReference' => '25248208' + )); - $this->gateway->setCurrency('MYR'); - $this->gateway->setLocale('en'); - $this->gateway->setMerchantId('test1234'); - $this->gateway->setVerifyKey('abcdefg'); + $this->assertInstanceOf('\Omnipay\MOLPay\Message\ReversalRequest', $request); + $this->assertSame('25248208', $request->getTransactionReference()); + $endPoint = $request->getEndpoint(); + $this->assertSame('https://api.molpay.com/MOLPay/API/refundAPI/refundAPI/refund.php', $endPoint); + $data = $request->getData(); + $this->assertNotEmpty($data); } + + // public function testRefund() + // { + // $request = $this->gateway->refund(array( + // 'transactionReference' => '25248208', + // 'refId' => 'merchant_refund_ref_id', + // 'amount' => '10.00', + // 'bankCode' => 'MBBEMYKL', + // 'beneficiaryName' => 'beneficiary_name', + // 'beneficiaryAccountNo' => 'beneficiary_account_no', + // )); + + // $this->assertInstanceOf('\Omnipay\MOLPay\Message\PartialRefundRequest', $request); + // $this->assertSame('25248208', $request->getTransactionReference()); + // $this->assertSame('merchant_refund_ref_id', $request->getRefId()); + // $this->assertSame('10.00', $request->getAmount()); + // $this->assertSame('MBBEMYKL', $request->getBankCode()); + // $this->assertSame('beneficiary_name', $request->getBeneficiaryName()); + // $this->assertSame('beneficiary_account_no', $request->getBeneficiaryAccountNo()); + + // $endPoint = $request->getEndpoint(); + // $this->assertSame('https://api.molpay.com/MOLPay/API/refundAPI/index.php', $endPoint); + // $data = $request->getData(); + // $this->assertNotEmpty($data); + // } } diff --git a/tests/Message/CompletePurchaseRequestTest.php b/tests/Message/CompletePurchaseRequestTest.php index 92d45d4..c55b238 100644 --- a/tests/Message/CompletePurchaseRequestTest.php +++ b/tests/Message/CompletePurchaseRequestTest.php @@ -1,8 +1,8 @@ request = new CompletePurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); $this->request->initialize(array( - 'amount' => 1000, + 'amount' => '10.00', 'appCode' => 'abcdefg', 'currency' => 'MYR', 'domain' => 'test4321', 'payDate' => '2016-03-29 04:02:21', - 'sKey' => '9b8be764cc5bad1b4a5d58a3ba4daf58', + 'sKey' => '2e684713b97a79721e347492ef75765e', 'status' => '00', 'transactionId' => '20160331082207680000', 'transactionReference' => '000001', 'verifyKey' => 'abcdefg', + 'secretKey' => 'hilklmn', )); } @@ -35,7 +36,7 @@ public function testGetData() public function testSendSuccess() { $this->request->setStatus('00'); - $this->request->setSKey('9b8be764cc5bad1b4a5d58a3ba4daf58'); + $this->request->setSKey('2e684713b97a79721e347492ef75765e'); $response = $this->request->send(); @@ -49,7 +50,7 @@ public function testSendSuccess() public function testSendPending() { $this->request->setStatus('22'); - $this->request->setSKey('9d65ed0b785fea1c8fc80b8316555ee3'); + $this->request->setSKey('7f5b456722717f87ae37810d641742cb'); $response = $this->request->send(); diff --git a/tests/Message/CompletePurchaseResponseTest.php b/tests/Message/CompletePurchaseResponseTest.php index a57f877..2926139 100644 --- a/tests/Message/CompletePurchaseResponseTest.php +++ b/tests/Message/CompletePurchaseResponseTest.php @@ -1,8 +1,8 @@ getHttpClient(); + $request = $this->getHttpRequest(); + + $this->request = new PartialRefundRequest($client, $request); + } + + // public function testGetData() + // { + // $this->request->setMerchantId('your_merchant_id'); + // $this->request->setRefId('merchant_refund_ref_id'); + // $this->request->setTransactionReference('25248208'); + // $this->request->setChannel('FPX_MB2U'); + // $this->request->setAmount('10.00'); + // $this->request->setBankCode('MBBEMYKL'); + // $this->request->setBeneficiaryName('beneficiary_name'); + // $this->request->setBeneficiaryAccountNo('beneficiary_account_no'); + // $this->request->setSecretKey('your_secret_key'); + + // $expected = array(); + // $expected['RefundType'] = 'P'; + // $expected['MerchantID'] = 'your_merchant_id'; + // $expected['RefID'] = 'merchant_refund_ref_id'; + // $expected['TxnID'] = '25248208'; + // $expected['Channel'] = 'FPX_MB2U'; + // $expected['Amount'] = '10.00'; + // $expected['BankCode'] = 'MBBEMYKL'; + // $expected['BeneficiaryName'] = 'beneficiary_name'; + // $expected['BeneficiaryAccNo'] = 'beneficiary_account_no'; + // $expected['Signature'] = 'aafbef8720b13a33b37370a1a3b1c238'; + // $expected['mdr_flag'] = null; + // $expected['notify_url'] = null; + + // $this->assertEquals($expected, $this->request->getData()); + // } +} diff --git a/tests/Message/PartialRefundResponseTest.php b/tests/Message/PartialRefundResponseTest.php new file mode 100644 index 0000000..671fe4f --- /dev/null +++ b/tests/Message/PartialRefundResponseTest.php @@ -0,0 +1,26 @@ +getMockHttpResponse('PartialRefundPending.txt'); + // $response = new PartialRefundResponse($this->getMockRequest(), $httpResponse->json()); + + // $this->assertTrue($response->isPending()); + // } + + // public function testPartialRefundError() + // { + // $httpResponse = $this->getMockHttpResponse('PartialRefundError.txt'); + // $response = new PartialRefundResponse($this->getMockRequest(), $httpResponse->json()); + + // $this->assertFalse($response->isSuccessful()); + // $this->assertFalse($response->isPending()); + // $this->assertSame('Exceed refund amount for this transaction.', $response->getMessage()); + // } +} \ No newline at end of file diff --git a/tests/Message/PurchaseRequestTest.php b/tests/Message/PurchaseRequestTest.php index e7ec433..589246b 100644 --- a/tests/Message/PurchaseRequestTest.php +++ b/tests/Message/PurchaseRequestTest.php @@ -1,10 +1,9 @@ request = new PurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); $this->request->initialize(array( - 'amount' => 1000, + 'amount' => '10.00', 'card' => new CreditCard(array( - 'customer' => new Customer(array( - 'country' => 'MY', - 'email' => 'abc@example.com', - 'name' => 'Lee Siong Chan', - 'phone' => '0123456789', - )), + 'country' => 'MY', + 'email' => 'abc@example.com', + 'name' => 'Lee Siong Chan', + 'phone' => '0123456789', )), 'currency' => 'MYR', 'description' => 'Test Payment', diff --git a/tests/Message/PurchaseResponseTest.php b/tests/Message/PurchaseResponseTest.php index d1da9ab..51562c5 100644 --- a/tests/Message/PurchaseResponseTest.php +++ b/tests/Message/PurchaseResponseTest.php @@ -1,8 +1,8 @@ getHttpClient(); + $request = $this->getHttpRequest(); + + $this->request = new ReversalRequest($client, $request); + } + + public function testGetData() + { + $this->request->setTransactionReference('25248208'); + $this->request->setMerchantId('your_merchant_id'); + $this->request->setSecretKey('your_secret_key'); + + $expected = array(); + $expected['txnID'] = '25248208'; + $expected['domain'] = 'your_merchant_id'; + $expected['skey'] = 'd07b97e2b8c7234792d3fb1fe56db619'; + + $this->assertEquals($expected, $this->request->getData()); + } +} diff --git a/tests/Message/ReversalResponseTest.php b/tests/Message/ReversalResponseTest.php new file mode 100644 index 0000000..159485e --- /dev/null +++ b/tests/Message/ReversalResponseTest.php @@ -0,0 +1,41 @@ +getMockRequest(), + "TxnID=25248203\nDomain=your_merchant_id\nStatDate=2018-01-28 15:53:19\nStatCode=00\nVrfKey=f56d5ea9932861454b7fd69851f57f7c"); + + $this->assertEquals(array( + 'TxnID' => '25248203', + 'Domain' => 'your_merchant_id', + 'StatDate' => '2018-01-28 15:53:19', + 'StatCode' => '00', + 'VrfKey' => 'f56d5ea9932861454b7fd69851f57f7c'), + $response->getData()); + } + + public function testReversalSuccess() + { + $httpResponse = $this->getMockHttpResponse('ReversalSuccess.txt'); + $response = new ReversalResponse($this->getMockRequest(), $httpResponse->getBody()); + + $this->assertTrue($response->isSuccessful()); + $this->assertSame('Success', $response->getMessage()); + } + + public function testRefundFailure() + { + $httpResponse = $this->getMockHttpResponse('ReversalFailure.txt'); + $response = new ReversalResponse($this->getMockRequest(), $httpResponse->getBody()); + + $this->assertFalse($response->isSuccessful()); + $this->assertSame('Forbidden transaction', $response->getMessage()); + } +} diff --git a/tests/Mock/PartialRefundError.txt b/tests/Mock/PartialRefundError.txt new file mode 100644 index 0000000..9a28c43 --- /dev/null +++ b/tests/Mock/PartialRefundError.txt @@ -0,0 +1,11 @@ +HTTP/1.1 200 OK +Date: Sun, 28 Jan 2018 11:21:38 GMT +Server: Apache +Content-Length: 93 +Connection: close +Content-Type: application/json + +{ + "error_code": "PR011", + "error_desc": "Exceed refund amount for this transaction." +} \ No newline at end of file diff --git a/tests/Mock/PartialRefundPending.txt b/tests/Mock/PartialRefundPending.txt new file mode 100644 index 0000000..6e17388 --- /dev/null +++ b/tests/Mock/PartialRefundPending.txt @@ -0,0 +1,17 @@ +HTTP/1.1 200 OK +Date: Sun, 28 Jan 2018 11:21:38 GMT +Server: Apache +Content-Length: 119 +Connection: close +Content-Type: application/json + +{ + "RefundType": "P", + "MerchantID": "your_merchant_id", + "RefID": "merchant_refund_ref_id", + "RefundID": 19, + "TxnID": 25248208, + "Amount": "10.00", + "Status": "pending", + "Signature": "816071a1a72a260b87d7570beb0a670a" +} \ No newline at end of file diff --git a/tests/Mock/ReversalFailure.txt b/tests/Mock/ReversalFailure.txt new file mode 100644 index 0000000..167b56b --- /dev/null +++ b/tests/Mock/ReversalFailure.txt @@ -0,0 +1,12 @@ +HTTP/1.1 200 OK +Date: Sun, 28 Jan 2018 11:21:38 GMT +Server: Apache +Content-Length: 119 +Connection: close +Content-Type: text/plain; charset=utf-8 + +TxnID=25248203 +Domain=your_merchant_id +StatDate=2018-01-28 15:53:19 +StatCode=16 +VrfKey=3917697f5dd0cda28ba7408eb5d07e62 \ No newline at end of file diff --git a/tests/Mock/ReversalSuccess.txt b/tests/Mock/ReversalSuccess.txt new file mode 100644 index 0000000..a799e7d --- /dev/null +++ b/tests/Mock/ReversalSuccess.txt @@ -0,0 +1,12 @@ +HTTP/1.1 200 OK +Date: Sun, 28 Jan 2018 11:21:38 GMT +Server: Apache +Content-Length: 119 +Connection: close +Content-Type: text/plain; charset=utf-8 + +TxnID=25248203 +Domain=your_merchant_id +StatDate=2018-01-28 15:53:19 +StatCode=00 +VrfKey=f56d5ea9932861454b7fd69851f57f7c \ No newline at end of file