diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5c9d57f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +# Created by .ignore support plugin (hsz.mobi) +vendor +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..153e483 --- /dev/null +++ b/composer.json @@ -0,0 +1,34 @@ +{ + "name": "ngocnh/omnipay-onepay", + "type": "ngocnh-library", + "description": "OnePay gateway for Omnipay payment processing library", + "keywords": [ + "gateway", + "merchant", + "omnipay", + "pay", + "onepay", + "noidia", + "quocte", + "onepay", + "payment", + "paypal", + "purchase" + ], + "authors": [ + { + "name": "xu ding", + "email": "thedilab@gmail.com", + } + ], + "minimum-stability": "stable", + "autoload": { + "psr-4": { "Omnipay\\OnePay\\" : "src/" } + }, + "require": { + "omnipay/common": "~2.0" + }, + "require-dev": { + "omnipay/tests": "~2.0" + } +} diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php new file mode 100755 index 0000000..525b5b6 --- /dev/null +++ b/src/Message/AbstractRequest.php @@ -0,0 +1,162 @@ +getParameter('vpcAccessCode'); + } + + + public function setVpcAccessCode($vpcAccessCode) + { + return $this->setParameter('vpcAccessCode', $vpcAccessCode); + } + + + public function getVpcMerchant() + { + return $this->getParameter('vpcMerchant'); + } + + + public function setVpcMerchant($vpcMerchant) + { + return $this->setParameter('vpcMerchant', $vpcMerchant); + } + + + public function getSecureHash() + { + return $this->getParameter('secureHash'); + } + + + public function setSecureHash($secureHash) + { + return $this->setParameter('secureHash', $secureHash); + } + + + public function getVpcUser() + { + return $this->getParameter('vpcUser'); + } + + + public function setVpcUser($vpcUser) + { + return $this->setParameter('vpcUser', $vpcUser); + } + + + public function getVpcPassword() + { + return $this->getParameter('vpcPassword'); + } + + + public function setVpcPassword($vpcPassword) + { + return $this->setParameter('vpcPassword', $vpcPassword); + } + + + public function getVpc_MerchTxnRef() + { + return $this->getParameter('vpc_MerchTxnRef'); + } + + + public function setVpc_MerchTxnRef($value) + { + return $this->setParameter('vpc_MerchTxnRef', $value); + } + + + protected function getBaseData() + { + return [ + 'vpc_Merchant' => $this->getVpcMerchant(), + 'vpc_AccessCode' => $this->getVpcAccessCode(), + ]; + } + + + public function sendData($data) + { + $url = $this->getEndpoint() . '?' . http_build_query($data, '', '&'); + $httpResponse = $this->httpClient->get($url)->send(); + + return $this->createResponse($httpResponse->getBody()); + } + + + protected function getEndpoint() + { + return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; + } + + + protected function createResponse($data) + { + return $this->response = new Response($this, $data); + } + + + public function generateDataWithChecksum($data) + { + // sắp xếp dữ liệu theo thứ tự a-z trước khi nối lại + // arrange array data a-z before make a hash + ksort($data); + // Remove the Virtual Payment Client URL from the parameter hash as we + // do not want to send these fields to the Virtual Payment Client. + // bỏ giá trị url và nút submit ra khỏi mảng dữ liệu + unset( $data["virtualPaymentClientURL"] ); + unset( $data["SubButL"] ); + unset( $data["vpc_order_id"] ); + + //$stringHashData = $SECURE_SECRET; *****************************Khởi tạo chuỗi dữ liệu mã hóa trống***************************** + $stringHashData = ""; + + foreach ($data as $key => $value) { + // create the md5 input and URL leaving out any fields that have no value + // tạo chuỗi đầu dữ liệu những tham số có dữ liệu + if (strlen($value) > 0) { + //$stringHashData .= $value; *****************************sử dụng cả tên và giá trị tham số để mã hóa***************************** + if (( strlen($value) > 0 ) && ( ( substr($key, 0, 4) == "vpc_" ) || ( substr($key, 0, + 5) == "user_" ) ) + ) { + $stringHashData .= $key . "=" . $value . "&"; + } + } + } + //*****************************xóa ký tự & ở thừa ở cuối chuỗi dữ liệu mã hóa***************************** + $stringHashData = rtrim($stringHashData, "&"); + // Create the secure hash and append it to the Virtual Payment Client Data if + // the merchant secret has been provided. + + // thêm giá trị chuỗi mã hóa dữ liệu được tạo ra ở trên vào cuối url + //$vpcURL .= "&vpc_SecureHash=" . strtoupper(md5($stringHashData)); + // *****************************Thay hàm mã hóa dữ liệu***************************** + $data['vpc_SecureHash'] = strtoupper(hash_hmac('SHA256', $stringHashData, pack('H*', $this->getSecureHash()))); + + return $data; + } + +} diff --git a/src/Message/FetchQuocTeResponse.php b/src/Message/FetchQuocTeResponse.php new file mode 100755 index 0000000..dc3a457 --- /dev/null +++ b/src/Message/FetchQuocTeResponse.php @@ -0,0 +1,96 @@ +request = $request; + parse_str($data, $this->data); + } + + + public function isSuccessful() + { + if (isset( $this->data['vpc_DRExists'] ) && $this->data['vpc_DRExists'] == 'Y' && isset( $this->data['vpc_TxnResponseCode'] ) && $this->data['vpc_TxnResponseCode'] == '0') { + return true; + } elseif ( ! isset( $this->data['vpc_DRExists'] ) && isset( $this->data['vpc_TxnResponseCode'] ) && $this->data['vpc_TxnResponseCode'] == '0') { + return true; + } elseif (isset( $this->data['vpc_ResponseCode'] ) && $this->data['vpc_ResponseCode'] == '0') { + return true; + } + + return false; + } + + + /** + * @return string + */ + public function getMessage() + { + if (isset( $this->data['vpc_DRExists'] ) && $this->data['vpc_DRExists'] == 'N') { + return "Không tồn tại giao dịch"; + } else { + if (isset( $this->data['vpc_TxnResponseCode'] )) { + return $this->getResponseDescription($this->data['vpc_TxnResponseCode']); + } + + return isset( $this->data['vpc_Message'] ) ? $this->data['vpc_Message'] : ''; + } + } + + + /** + * @return string + */ + protected function getResponseDescription($responseCode) + { + switch ($responseCode) { + case "0" : + $result = "Giao dịch thành công - Approved"; + break; + case "300" : + $result = "Giao dịch đang chờ - Pending"; + break; + default : + $result = "Giao dịch không thanh toán thành công - Failured"; + } + + return $result; + } + +} diff --git a/src/Message/NoiDiaCompletePurchaseRequest.php b/src/Message/NoiDiaCompletePurchaseRequest.php new file mode 100755 index 0000000..dc749c6 --- /dev/null +++ b/src/Message/NoiDiaCompletePurchaseRequest.php @@ -0,0 +1,119 @@ +getBaseData(); + $data['vpc_MerchTxnRef'] = $this->getVpc_MerchTxnRef(); + + return $data; + } + + + public function getConfirmReference() + { + $dataConfirm = [ ]; + + if ($this->checkHash()) { + $dataConfirm['responsecode'] = 1; + $dataConfirm['desc'] = 'confirm-success'; + } else { + $dataConfirm['responsecode'] = 0; + $dataConfirm['desc'] = 'confirm-fail'; + } + + return $dataConfirm; + } + + + public function sendData($data) + { + $httpResponse = $this->httpClient->post($this->getEndpoint(), null, $data //$this->encodeData($data) + )->send(); + + return $this->response = new FetchResponse($this, $httpResponse->getBody()); + } + + + /** + * Encode absurd name value pair format + */ + public function encodeData(array $data) + { + $output = [ ]; + foreach ($data as $key => $value) { + $output[] = $key . '[' . strlen($value) . ']=' . $value; + } + + return implode('&', $output); + } + + + protected function checkHash() + { + + $data = $this->httpRequest->request->all(); + + // get and remove the vpc_TxnResponseCode code from the response fields as we + // do not want to include this field in the hash calculation + $vpc_Txn_Secure_Hash = $data['vpc_SecureHash']; + unset ( $data['vpc_SecureHash'] ); + + // set a flag to indicate if hash has been validated + $hashValidated = false; + $SECURE_SECRET = $this->getSecureHash(); + + if (strlen($SECURE_SECRET) > 0 && $data['vpc_TxnResponseCode'] != "7" && $data['vpc_TxnResponseCode'] != "No Value Returned") { + + //$stringHashData = $SECURE_SECRET; + //*****************************khởi tạo chuỗi mã hóa rỗng***************************** + $stringHashData = ""; + + // sort all the incoming vpc response fields and leave out any with no value + foreach ($data as $key => $value) { + // if ($key != "vpc_SecureHash" or strlen($value) > 0) { + // $stringHashData .= $value; + // } + // *****************************chỉ lấy các tham số bắt đầu bằng "vpc_" hoặc "user_" và khác trống và không phải chuỗi hash code trả về***************************** + if ($key != "vpc_SecureHash" && ( strlen($value) > 0 ) && ( ( substr($key, 0, + 4) == "vpc_" ) || ( substr($key, 0, 5) == "user_" ) ) + ) { + $stringHashData .= $key . "=" . $value . "&"; + } + } + // *****************************Xóa dấu & thừa cuối chuỗi dữ liệu***************************** + $stringHashData = rtrim($stringHashData, "&"); + + // if (strtoupper ( $vpc_Txn_Secure_Hash ) == strtoupper ( md5 ( $stringHashData ) )) { + // *****************************Thay hàm tạo chuỗi mã hóa***************************** + if (strtoupper($vpc_Txn_Secure_Hash) == strtoupper(hash_hmac('SHA256', $stringHashData, + pack('H*', $SECURE_SECRET))) + ) { + // Secure Hash validation succeeded, add a data field to be displayed + // later. + $hashValidated = true; + } else { + // Secure Hash validation failed, add a data field to be displayed later. + } + } else { + // Secure Hash was not validated, add a data field to be displayed later. + } + + return $hashValidated; + } + +} diff --git a/src/Message/NoiDiaFetchRequest.php b/src/Message/NoiDiaFetchRequest.php new file mode 100755 index 0000000..04e3d13 --- /dev/null +++ b/src/Message/NoiDiaFetchRequest.php @@ -0,0 +1,45 @@ +getBaseData(); + + $data['vpc_Version'] = $this::API_VERSION; + $data['vpc_Command'] = 'queryDR'; // method + + if (empty( $this->getVpcPassword() ) && empty( $this->getVpcUser() )) { + throw new InvalidRequestException("The vpcUser or vpcPassword parameter is required"); + } + + $data['vpc_User'] = $this->getVpcUser(); + $data['vpc_Password'] = $this->getVpcPassword(); + + $data['vpc_MerchTxnRef'] = $this->getVpc_MerchTxnRef(); + + return $data; + } + + + public function sendData($data) + { + $httpResponse = $this->httpClient->post($this->getEndpoint(), null, $data)->send(); // method POST + + return $this->response = new FetchResponse($this, $httpResponse->getBody()); + } + +} diff --git a/src/Message/NoiDiaPurchaseRequest.php b/src/Message/NoiDiaPurchaseRequest.php new file mode 100755 index 0000000..0431e6d --- /dev/null +++ b/src/Message/NoiDiaPurchaseRequest.php @@ -0,0 +1,41 @@ +validate('amount'); + + $data = [ + 'vpc_order_id' => $this->getTransactionId(), + 'Title' => 'VPC 3-Party', + 'virtualPaymentClientURL' => $this->getEndpoint(), + 'vpc_Version' => $this::API_VERSION, + 'vpc_Command' => 'pay', + 'vpc_MerchTxnRef' => date('YmdHis') . rand(), + 'vpc_OrderInfo' => "Order_" . $this->getTransactionId() . "_" . time(), + 'vpc_Amount' => $this->getAmount(), + 'vpc_Locale' => $this->httpRequest->getLocale(), + 'vpc_ReturnURL' => $this->getReturnUrl(), + 'vpc_TicketNo' => $this->httpRequest->getClientIp(), + 'vpc_Currency' => $this->getCurrency() + ]; + + return array_merge($data, $this->getBaseData()); + } + + + public function sendData($data) + { + $data = http_build_query($this->generateDataWithChecksum($data), '', '&'); + + return $this->response = new NoiDiaPurchaseResponse($this, $data); + } + +} diff --git a/src/Message/NoiDiaPurchaseResponse.php b/src/Message/NoiDiaPurchaseResponse.php new file mode 100755 index 0000000..ceb7bab --- /dev/null +++ b/src/Message/NoiDiaPurchaseResponse.php @@ -0,0 +1,45 @@ +getCheckoutEndpoint() . '?' . http_build_query($this->data, '', '&'); + } + + + public function getRedirectMethod() + { + return 'POST'; + } + + + public function getRedirectData() + { + return null; + } + + + protected function getCheckoutEndpoint() + { + return $this->getRequest()->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; + } +} diff --git a/src/Message/QuocTeCompletePurchaseRequest.php b/src/Message/QuocTeCompletePurchaseRequest.php new file mode 100755 index 0000000..23ece97 --- /dev/null +++ b/src/Message/QuocTeCompletePurchaseRequest.php @@ -0,0 +1,17 @@ +httpClient->post($this->getEndpoint(), null, $data //$this->encodeData($data) + )->send(); + + return $this->response = new FetchQuocTeResponse($this, $httpResponse->getBody()); + } + + + /** + * Encode absurd name value pair format + */ + public function encodeData(array $data) + { + $output = [ ]; + foreach ($data as $key => $value) { + if (strlen($value) > 0 && $key != 'Title') { + $output[] = urlencode($key) . '=' . urlencode($value); + } + } + + return implode('&', $output); + } + +} diff --git a/src/Message/QuocTePurchaseRequest.php b/src/Message/QuocTePurchaseRequest.php new file mode 100755 index 0000000..488aaad --- /dev/null +++ b/src/Message/QuocTePurchaseRequest.php @@ -0,0 +1,47 @@ +validate('amount'); + + $data = [ + 'vpc_order_id' => $this->getTransactionId(), + 'Title' => 'VPC 3-Party', + 'virtualPaymentClientURL' => $this->getEndpoint(), + 'vpc_Version' => $this::API_VERSION, + 'vpc_Command' => 'pay', + 'vpc_MerchTxnRef' => date('YmdHis') . rand(), + 'vpc_OrderInfo' => "Order_" . $this->getTransactionId() . "_" . time(), + 'vpc_Amount' => $this->getAmount(), + 'vpc_Locale' => $this->httpRequest->getLocale(), + 'vpc_ReturnURL' => $this->getReturnUrl(), + 'AgainLink' => urlencode($this->httpRequest->server->get('HTTP_REFERER')), + //$this->getCancelUrl(), + 'vpc_TicketNo' => $this->httpRequest->getClientIp(), + ]; + + return array_merge($data, $this->getBaseData()); + } + + + public function sendData($data) + { + $data = http_build_query($this->generateDataWithChecksum($data), '', '&'); + + return $this->response = new QuocTePurchaseResponse($this, $data); + } + +} diff --git a/src/Message/QuocTePurchaseResponse.php b/src/Message/QuocTePurchaseResponse.php new file mode 100755 index 0000000..9444809 --- /dev/null +++ b/src/Message/QuocTePurchaseResponse.php @@ -0,0 +1,72 @@ + 'Giao dịch thành công - Transaction is successful', + '1' => 'Ngân hàng phát hành thẻ không cấp phép. Vui lòng liên hệ ngân hàng - Bank system reject', + '2' => 'Ngân hàng phát hành thẻ không cấp phép. Vui lòng liên hệ ngân hàng - Bank Declined Transaction', + '3' => 'Cổng thanh toán không nhận được kết quả trả về từ ngân hàng phát hành thẻ - No Reply from Bank', + '4' => 'Thẻ hết hạn sử dụng - Expired Card', + '5' => 'Thẻ không đủ hạn mức hoặc tài khoản không đủ số dư thanh toán. - Insufficient funds', + '6' => 'Lỗi từ ngân hàng phát hành thẻ. - Error Communicating with Bank', + '7' => 'Lỗi phát sinh trong quá trình xử lý giao dịch - Payment Server System Error', + '8' => 'Ngân hàng phát hành thẻ không hỗ trợ giao dịch Internet - Transaction Type Not Supported', + '9' => 'Ngân hàng phát hành thẻ từ chối giao dịch - Bank declined transaction (Do not contact Bank)', + 'A' => 'Thẻ hết hạn/Thẻ bị khóa - Transaction Aborted', + 'C' => 'Thẻ chưa đăng ký sử dụng dịch vụ - Transaction Cancelled', + 'D' => 'Ngày phát hành/Hết hạn không đúng - Deferred transaction has been received and is awaiting processing', + 'B' => 'Không xác thực được 3D - 3D Secure Authentication failed', + 'W' => 'Không xác thực được 3D - 3D Secure Authentication failed', + 'F' => 'Không xác thực được 3D - 3D Secure Authentication failed', + 'I' => 'Khong - Card Security Code verification failed', + 'R' => 'Giao dịch quá số lần cho phép - Reached limit of retry attempts allowed', + 'S' => 'Duplicate SessionID (OrderInfo)', + 'T' => 'Address Verification Failed', + 'U' => 'Card Security Code Failed', + 'V' => 'Address Verification and Card Security Code Failed', + '99' => 'Người dùng hủy giao dịch - User Cancel', + 'X' => 'Giao dịch thất bại - Failured' + ]; + + + public function isRedirect() + { + return true; + } + + + public function getRedirectUrl() + { + return $this->getCheckoutEndpoint() . '?' . http_build_query($this->data, '', '&'); + } + + + public function getRedirectMethod() + { + return 'POST'; + } + + + public function getRedirectData() + { + return null; + } + + + protected function getCheckoutEndpoint() + { + return $this->getRequest()->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; + } +} diff --git a/src/Message/Response.php b/src/Message/Response.php new file mode 100755 index 0000000..15fd67a --- /dev/null +++ b/src/Message/Response.php @@ -0,0 +1,97 @@ + 'Giao dịch thành công - Approved', + '1' => 'Ngân hàng từ chối giao dịch - Bank Declined', + '3' => 'Mã đơn vị không tồn tại - Merchant not exist', + '4' => 'Không đúng access code - Invalid access code', + '5' => 'Số tiền không hợp lệ - Invalid amount', + '6' => 'Mã tiền tệ không tồn tại - Invalid currency code', + '7' => 'Lỗi không xác định - Unspecified Failure', + '8' => 'Số thẻ không đúng - Invalid card Number', + '9' => 'Tên chủ thẻ không đúng - Invalid card name', + '10' => 'Thẻ hết hạn/Thẻ bị khóa - Expired Card', + '11' => 'Thẻ chưa đăng ký sử dụng dịch vụ - Card Not Registed Service(internet banking)', + '12' => 'Ngày phát hành/Hết hạn không đúng - Invalid card date', + '13' => 'Vượt quá hạn mức thanh toán - Exist Amount', + '21' => 'Số tiền không đủ để thanh toán - Insufficient fund', + '99' => 'Người sủ dụng hủy giao dịch - User cancel', + 'X' => 'Giao dịch thất bại - Failured' + ]; + + + public function __construct(RequestInterface $request, $data) + { + $this->request = $request; + if ( ! is_array($data)) { + parse_str($data, $this->data); + } else { + $this->data = $data; + } + } + + + public function isSuccessful() + { + if (isset( $this->data['vpc_TxnResponseCode'] ) && $this->data['vpc_TxnResponseCode'] == '0') { + $result = true; + } elseif (isset( $this->data['vpc_ResponseCode'] ) && $this->data['vpc_ResponseCode'] == '0') { + $result = true; + } else { + $result = false; + } + + return $result; + } + + + /** + * To capture , refund , ... + * + * @return mixed + */ + public function getTransactionReference() + { + foreach ([ 'vpc_MerchTxnRef', 'vpc_TransactionNo' ] as $key) { + if (isset( $this->data[$key] )) { + return $this->data[$key]; + } + } + } + + + /** + * @return string + */ + public function getMessage() + { + if (isset( $this->data['vpc_TxnResponseCode'] )) { + return $this->getResponseDescription($this->data['vpc_TxnResponseCode']); + } elseif (isset( $this->data['vpc_ResponseCode'] )) { + return $this->getResponseDescription($this->data['vpc_ResponseCode']); + } else { + return $this->data['vpc_Message']; + } + } + + + protected function getResponseDescription($responseCode) + { + if (array_key_exists($responseCode, $this->transactionStatus)) { + return $this->transactionStatus[$responseCode]; + } + + return $this->transactionStatus['X']; + } +} diff --git a/src/NoiDiaGateway.php b/src/NoiDiaGateway.php new file mode 100755 index 0000000..6069c13 --- /dev/null +++ b/src/NoiDiaGateway.php @@ -0,0 +1,145 @@ + '', + 'vpcMerchant' => '', + 'secureHash' => '', + 'vpcUser' => '', + 'vpcPassword' => '', + 'testMode' => false, + ]; + } + + + public function getVpcAccessCode() + { + return $this->getParameter('vpcAccessCode'); + } + + + public function setVpcAccessCode($vpcAccessCode) + { + return $this->setParameter('vpcAccessCode', $vpcAccessCode); + } + + + public function getVpcMerchant() + { + return $this->getParameter('vpcMerchant'); + } + + + public function setVpcMerchant($vpcMerchant) + { + return $this->setParameter('vpcMerchant', $vpcMerchant); + } + + + public function getSecureHash() + { + return $this->getParameter('secureHash'); + } + + + public function setSecureHash($secureHash) + { + return $this->setParameter('secureHash', $secureHash); + } + + + public function getVpcUser() + { + return $this->getParameter('vpcUser'); + } + + + public function setVpcUser($vpcUser) + { + return $this->setParameter('vpcUser', $vpcUser); + } + + + public function getVpcPassword() + { + return $this->getParameter('vpcPassword'); + } + + + public function setVpcPassword($vpcPassword) + { + return $this->setParameter('vpcPassword', $vpcPassword); + } + + + public function getTestMode() + { + return $this->getParameter('testMode'); + } + + + public function setTestMode($value) + { + return $this->setParameter('testMode', $value); + } + + + public function purchase(array $parameters = [ ]) + { + return $this->createRequest('\Omnipay\OnePay\Message\NoiDiaPurchaseRequest', $parameters); + } + + + public function completePurchase(array $parameters = [ ]) + { + return $this->createRequest('\Omnipay\OnePay\Message\NoiDiaFetchRequest', $parameters); + } + + + public function fetchCheckout(array $parameters = [ ]) + { + return $this->createRequest('\Omnipay\OnePay\Message\NoiDiaFetchRequest', $parameters); + } + + + /** + * TODO should move to AbstractGateway + * + * Create a response object using existing parameters from return url , redirect url + * + * @param string $class The response class name, ex: \Omnipay\Payflow\Message\Response + * @param array $parameters , ex: ["action" => "return", "vpc_TxnResponseCode" => 5, "vpc_Message" => "Amount is + * invalid"] + * + * @return object, ex: \Omnipay\Common\Message\Response + */ + public function createResponse($class, array $parameters, $type) + { + return new $class(call_user_func_array([ $this, $type ], [ $parameters ]), $parameters); + } + + + public function getResponse(array $parameters = [ ], $type = 'purchase') + { + return $this->createResponse('\Omnipay\OnePay\Message\Response', $parameters, $type); + } +} diff --git a/src/QuocTeGateway.php b/src/QuocTeGateway.php new file mode 100755 index 0000000..978b29e --- /dev/null +++ b/src/QuocTeGateway.php @@ -0,0 +1,42 @@ +createRequest('\Omnipay\OnePay\Message\QuocTePurchaseRequest', $parameters); + } + + + public function completePurchase(array $parameters = [ ]) + { + return $this->createRequest('\Omnipay\OnePay\Message\QuocTeFetchRequest', $parameters); + } + + + public function fetchCheckout(array $parameters = [ ]) + { + return $this->createRequest('\Omnipay\OnePay\Message\QuocTeFetchRequest', $parameters); + } + + + public function getResponse(array $parameters = [ ], $type = 'purchase') + { + return $this->createResponse('\Omnipay\OnePay\Message\QuocTePurchaseResponse', $parameters, $type); + } + +} diff --git a/tests/Message/NoiDiaFetchRequestTest.php b/tests/Message/NoiDiaFetchRequestTest.php new file mode 100755 index 0000000..b15afef --- /dev/null +++ b/tests/Message/NoiDiaFetchRequestTest.php @@ -0,0 +1,53 @@ +request = new NoiDiaFetchRequest($this->getHttpClient(), $this->getHttpRequest()); + + $this->options = [ + 'vpcMerchant' => 'ONEPAY', + 'vpcAccessCode' => 'D67342C2', + 'secureHash' => 'A3EFDFABA8653DF2342E8DAC29B51AF0', + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'testMode' => true, + 'vpc_MerchTxnRef' => '2413' + ]; + + $this->request->initialize($this->options); + } + + + public function testGetData() + { + $data = $this->request->getData(); + + $this->assertSame('ONEPAY', $data['vpc_Merchant']); + $this->assertSame('D67342C2', $data['vpc_AccessCode']); + $this->assertSame('1', $data['vpc_Version']); + $this->assertSame('queryDR', $data['vpc_Command']); + $this->assertSame('op01', $data['vpc_User']); + $this->assertSame('op123456', $data['vpc_Password']); + $this->assertSame('2413', $data['vpc_MerchTxnRef']); + } +} diff --git a/tests/Message/NoiDiaPurchaseRequestTest.php b/tests/Message/NoiDiaPurchaseRequestTest.php new file mode 100755 index 0000000..1f0d6a8 --- /dev/null +++ b/tests/Message/NoiDiaPurchaseRequestTest.php @@ -0,0 +1,92 @@ +request = new NoiDiaPurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); + + $this->options = [ + 'vpcMerchant' => 'ONEPAY', + 'vpcAccessCode' => 'D67342C2', + 'secureHash' => 'A3EFDFABA8653DF2342E8DAC29B51AF0', + 'testMode' => true, + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'returnUrl' => 'http://truonghoang.cool/app_dev.php/backend/process_transaction.html/1431785?client_key=94bc04c3760620d537b6717abd53ff3e&action=return', + 'amount' => '1000', + 'currency' => 'VND', + 'transactionId' => '1431785' + ]; + + $this->request->initialize($this->options); + } + + + public function testGetData() + { +// $this->request->setVpc_MerchTxnRef('33333333333333333'); + + $expected = [ + 'vpc_Merchant' => 'ONEPAY', + 'vpc_AccessCode' => 'D67342C2', + 'vpc_order_id' => '1431785', + 'Title' => 'VPC 3-Party', + 'vpc_Version' => '2', + 'vpc_Command' => 'pay', + 'virtualPaymentClientURL' => $this->testGetEndpoint(), +// 'vpc_MerchTxnRef' => '33333333333333333', +// 'vpc_OrderInfo' => "Order_1431786_11111", + 'vpc_Amount' => '1000', + 'vpc_Locale' => $this->getHttpRequest()->getLocale(), + 'vpc_ReturnURL' => 'http://truonghoang.cool/app_dev.php/backend/process_transaction.html/1431785?client_key=94bc04c3760620d537b6717abd53ff3e&action=return', + 'vpc_TicketNo' => $this->getHttpRequest()->getClientIp(), + 'vpc_Currency' => 'VND' + + ]; + + $requetData = $this->request->getData(); + + $this->assertNotNull($requetData['vpc_MerchTxnRef']); + $this->assertNotNull($requetData['vpc_OrderInfo']); + + // exclude by random property + unset( $requetData['vpc_OrderInfo'] ); + unset( $requetData['vpc_MerchTxnRef'] ); + + $this->assertEquals($expected, $requetData); + } + + + public function testSendData() + { + $this->testGetData(); + + $data = $this->request->generateDataWithChecksum($this->request->getData()); + + $this->assertArrayHasKey('vpc_SecureHash', $data); + } + + + public function testGetEndpoint() + { + $reflectionOfUser = new \ReflectionClass('\Omnipay\OnePay\Message\NoiDiaPurchaseRequest'); + $method = $reflectionOfUser->getMethod('getEndpoint'); + $method->setAccessible(true); + + $this->assertEquals('https://mtf.onepay.vn/onecomm-pay/vpc.op', $method->invokeArgs($this->request, [ ])); + + return 'https://mtf.onepay.vn/onecomm-pay/vpc.op'; + } +} diff --git a/tests/Message/NoiDiaPurchaseResponseTest.php b/tests/Message/NoiDiaPurchaseResponseTest.php new file mode 100755 index 0000000..1f823a7 --- /dev/null +++ b/tests/Message/NoiDiaPurchaseResponseTest.php @@ -0,0 +1,52 @@ + 'ONEPAY', + 'vpc_AccessCode' => 'D67342C2', + 'vpc_order_id' => '1431785', + 'Title' => 'VPC 3-Party', + 'vpc_Version' => '2', + 'vpc_Command' => 'pay', + 'vpc_MerchTxnRef' => '33333333333333333', + 'vpc_OrderInfo' => "Order_1431785__11111", + 'vpc_Amount' => '1000', + 'vpc_Locale' => 'vn', + 'vpc_ReturnURL' => 'http://truonghoang.cool/app_dev.php/backend/process_transaction.html/1431785?client_key=94bc04c3760620d537b6717abd53ff3e&action=return', + 'vpc_TicketNo' => '192.168.0.1', + 'vpc_Currency' => 'VND', + 'vpc_SecureHash' => '44444444444444444' + ]; + + // response should decode URL format data + $this->response = new NoiDiaPurchaseResponse($this->getMockRequest(), http_build_query($expected)); + + $this->assertEquals($expected, $this->response->getData()); + } + + + public function testGetRedirectUrl() + { + $this->testConstruct(); + + $request = $this->getMockRequest(); + $request->shouldReceive('getTestMode')->once()->andReturn(true); + $this->assertFalse($this->response->isSuccessful()); + $this->assertTrue($this->response->isRedirect()); + $this->assertNull($this->response->getRedirectData()); + $this->assertSame('https://mtf.onepay.vn/onecomm-pay/vpc.op?' . http_build_query($this->response->getData(), '', + '&'), $this->response->getRedirectUrl()); + $this->assertSame('POST', $this->response->getRedirectMethod()); + } +} diff --git a/tests/Message/QuocTeFetchRequestTest.php b/tests/Message/QuocTeFetchRequestTest.php new file mode 100755 index 0000000..49b2545 --- /dev/null +++ b/tests/Message/QuocTeFetchRequestTest.php @@ -0,0 +1,55 @@ +request = new QuocTeFetchRequest($this->getHttpClient(), $this->getHttpRequest()); + + $this->options = [ + 'vpcMerchant' => 'TESTONEPAY', + 'vpcAccessCode' => '6BEB2546', + 'testMode' => true, + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'vpc_MerchTxnRef' => 'GDEAXIEM_41382,4523317014' + ]; + + $this->request->initialize($this->options); + } + + + public function testGetData() + { + $this->request->initialize($this->options); + + $data = $this->request->getData(); + + $this->assertSame('TESTONEPAY', $data['vpc_Merchant']); + $this->assertSame('6BEB2546', $data['vpc_AccessCode']); + $this->assertSame('1', $data['vpc_Version']); + $this->assertSame('queryDR', $data['vpc_Command']); + $this->assertSame('op01', $data['vpc_User']); + $this->assertSame('op123456', $data['vpc_Password']); + $this->assertSame('GDEAXIEM_41382,4523317014', $data['vpc_MerchTxnRef']); + } +} diff --git a/tests/Message/QuocTePurchaseRequestTest.php b/tests/Message/QuocTePurchaseRequestTest.php new file mode 100755 index 0000000..3dc2b03 --- /dev/null +++ b/tests/Message/QuocTePurchaseRequestTest.php @@ -0,0 +1,92 @@ +request = new QuocTePurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); + + $this->options = [ + 'vpcMerchant' => 'TESTONEPAY', + 'vpcAccessCode' => '6BEB2546', + 'secureHash' => 'A3EFDFABA8653DF2342E8DAC29B51AF0', + 'testMode' => true, + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'returnUrl' => 'http://truonghoang.cool/app_dev.php/backend/process_transaction.html/1431786?client_key=94bc04c3760620d537b6717abd53ff3e&action=return', + 'amount' => '1000', + 'currency' => 'VND', + 'transactionId' => '1431786' + ]; + + $this->request->initialize($this->options); + } + + + public function testGetData() + { +// $this->request->setVpc_MerchTxnRef('3333333333333333344444'); + + $expected = [ + 'vpc_Merchant' => 'TESTONEPAY', + 'vpc_AccessCode' => '6BEB2546', + 'vpc_order_id' => '1431786', + 'Title' => 'VPC 3-Party', + 'vpc_Version' => '2', + 'vpc_Command' => 'pay', + 'virtualPaymentClientURL' => $this->testGetEndpoint(), +// 'vpc_MerchTxnRef' => '3333333333333333344444', +// 'vpc_OrderInfo' => "Order_1431786_22222", + 'vpc_Amount' => '1000', + 'vpc_Locale' => $this->getHttpRequest()->getLocale(), + 'vpc_ReturnURL' => 'http://truonghoang.cool/app_dev.php/backend/process_transaction.html/1431786?client_key=94bc04c3760620d537b6717abd53ff3e&action=return', + 'vpc_TicketNo' => $this->getHttpRequest()->getClientIp(), + 'AgainLink' => urlencode($this->getHttpRequest()->server->get('HTTP_REFERER')) + + ]; + + $requetData = $this->request->getData(); + + $this->assertNotNull($requetData['vpc_MerchTxnRef']); + $this->assertNotNull($requetData['vpc_OrderInfo']); + + // exclude by random property + unset( $requetData['vpc_OrderInfo'] ); + unset( $requetData['vpc_MerchTxnRef'] ); + + $this->assertEquals($expected, $requetData); + } + + + public function testSendData() + { + $this->testGetData(); + + $data = $this->request->generateDataWithChecksum($this->request->getData()); + + $this->assertArrayHasKey('vpc_SecureHash', $data); + } + + + public function testGetEndpoint() + { + $reflectionOfUser = new \ReflectionClass('\Omnipay\OnePay\Message\QuocTePurchaseRequest'); + $method = $reflectionOfUser->getMethod('getEndpoint'); + $method->setAccessible(true); + + $this->assertEquals('https://mtf.onepay.vn/vpcpay/vpcpay.op', $method->invokeArgs($this->request, [ ])); + + return 'https://mtf.onepay.vn/vpcpay/vpcpay.op'; + } +} diff --git a/tests/Message/QuocTePurchaseResponseTest.php b/tests/Message/QuocTePurchaseResponseTest.php new file mode 100755 index 0000000..eaeac7b --- /dev/null +++ b/tests/Message/QuocTePurchaseResponseTest.php @@ -0,0 +1,51 @@ + 'TESTONEPAY', + 'vpc_AccessCode' => '6BEB2546', + 'vpc_order_id' => '1431786', + 'Title' => 'VPC 3-Party', + 'vpc_Version' => '2', + 'vpc_Command' => 'pay', + 'vpc_MerchTxnRef' => '3333333333333333344444', + 'vpc_OrderInfo' => "Order_1431785_22222", + 'vpc_Amount' => '1000', + 'vpc_Locale' => 'vn', + 'vpc_ReturnURL' => 'http://truonghoang.cool/app_dev.php/backend/process_transaction.html/1431786?client_key=94bc04c3760620d537b6717abd53ff3e&action=return', + 'vpc_TicketNo' => '192.168.0.2', + 'vpc_SecureHash' => '44444444444444444' + ]; + + // response should decode URL format data + $this->response = new NoiDiaPurchaseResponse($this->getMockRequest(), http_build_query($expected)); + + $this->assertEquals($expected, $this->response->getData()); + } + + + public function testGetRedirectUrl() + { + $this->testConstruct(); + + $request = $this->getMockRequest(); + $request->shouldReceive('getTestMode')->once()->andReturn(true); + $this->assertFalse($this->response->isSuccessful()); + $this->assertTrue($this->response->isRedirect()); + $this->assertNull($this->response->getRedirectData()); + $this->assertSame('https://mtf.onepay.vn/onecomm-pay/vpc.op?' . http_build_query($this->response->getData(), '', + '&'), $this->response->getRedirectUrl()); + $this->assertSame('POST', $this->response->getRedirectMethod()); + } +} diff --git a/tests/Message/ResponseTest.php b/tests/Message/ResponseTest.php new file mode 100755 index 0000000..ec6c0f5 --- /dev/null +++ b/tests/Message/ResponseTest.php @@ -0,0 +1,37 @@ +getMockRequest(), 'example=value&foo=bar'); + $this->assertEquals([ 'example' => 'value', 'foo' => 'bar' ], $response->getData()); + } + + + public function testPurchaseSuccess() + { + $httpResponse = $this->getMockHttpResponse('NoiDiaPurchaseSuccess.txt'); + $response = new Response($this->getMockRequest(), $httpResponse->getBody()); + + $this->assertTrue($response->isSuccessful()); + $this->assertSame('2413', $response->getTransactionReference()); + $this->assertEquals('Giao dịch thành công - Approved', $response->getMessage()); + } + + + public function testPurchaseFailure() + { + $httpResponse = $this->getMockHttpResponse('NoiDiaPurchaseFailure.txt'); + $response = new Response($this->getMockRequest(), $httpResponse->getBody()); + + $this->assertFalse($response->isSuccessful()); + $this->assertSame('Field AgainLink value is invalid.', $response->getMessage()); + } +} diff --git a/tests/Mock/NoiDiaFetchFailure.txt b/tests/Mock/NoiDiaFetchFailure.txt new file mode 100755 index 0000000..e956d0d --- /dev/null +++ b/tests/Mock/NoiDiaFetchFailure.txt @@ -0,0 +1,7 @@ +HTTP/1.1 200 OK +Date: 05 Apr 2007 23:44:11 GMT +Server: Apache +Connection: close +Content-Type: text/plain; charset=utf-8 + +vpc_DRExists=Y&vpc_AdditionData=970407&vpc_Amount=84000000&vpc_Command=pay&vpc_CurrencyCode=VND&vpc_Locale=vn&vpc_MerchTxnRef=2013042215193440019&vpc_Merchant=ONEPAY&vpc_Message=Transaction was not successful&vpc_OrderInfo=6SE4A1&vpc_TransactionNo=1432021&vpc_TxnResponseCode=100&vpc_Version= \ No newline at end of file diff --git a/tests/Mock/NoiDiaFetchSuccess.txt b/tests/Mock/NoiDiaFetchSuccess.txt new file mode 100755 index 0000000..43a9254 --- /dev/null +++ b/tests/Mock/NoiDiaFetchSuccess.txt @@ -0,0 +1,7 @@ +HTTP/1.1 200 OK +Date: 05 Apr 2015 23:44:11 GMT +Server: Apache +Connection: close +Content-Type: text/plain; charset=utf-8 + +vpc_DRExists=Y&vpc_AdditionData=686868&vpc_Amount=1000&vpc_Command=pay&vpc_CurrencyCode=VND&vpc_Locale=vn&vpc_MerchTxnRef=2413&vpc_Merchant=ONEPAY&vpc_Message=Transaction was successful&vpc_OrderInfo=2413&vpc_TransactionNo=1431785&vpc_TxnResponseCode=0&vpc_Version=2 \ No newline at end of file diff --git a/tests/Mock/NoiDiaPurchaseFailure.txt b/tests/Mock/NoiDiaPurchaseFailure.txt new file mode 100755 index 0000000..72b3cbe --- /dev/null +++ b/tests/Mock/NoiDiaPurchaseFailure.txt @@ -0,0 +1,8 @@ +HTTP/1.1 200 OK +Date: Fri, 15 Feb 2013 19:21:05 GMT +Server: Apache +Content-Length: 292 +Connection: close +Content-Type: text/plain; charset=utf-8 + +http://truonghoang.cool/app_dev.php/backend/process_transaction.html/24?client_key=94bc04c3760620d537b6717abd53ff3e&action=return?vpc_AccessCode=6BEB2546&vpc_Command=pay&vpc_OrderInfo=Order_24_1434339624&vpc_Message=Field+AgainLink+value+is+invalid.&Title=VPC+3-Party&vpc_Version=2&client_refer=http%3A%2F%2Ftruonghoang.cool%2Fapp_dev.php%2Fbackend%2F&vpc_Locale=vn&vpc_TicketNo=192.168.0.1&vpc_Merchant=TESTONEPAY&vpc_Amount=100&vpc_Currency=VND&vpc_SecureHash=15A776D4163F685B22CF981C96C4990EDDE888AD29A6A31E1C5C193555017DF0&vpc_MerchTxnRef=20150615104024530985749 \ No newline at end of file diff --git a/tests/Mock/NoiDiaPurchaseSuccess.txt b/tests/Mock/NoiDiaPurchaseSuccess.txt new file mode 100755 index 0000000..2bc578a --- /dev/null +++ b/tests/Mock/NoiDiaPurchaseSuccess.txt @@ -0,0 +1,7 @@ +HTTP/1.1 200 OK +Date: Fri, 15 Feb 2015 19:19:21 GMT +Server: Apache +Connection: close +Content-Type: text/plain; charset=utf-8 + +vpc_DRExists=Y&vpc_AdditionData=686868&vpc_Amount=1000&vpc_Command=pay&vpc_CurrencyCode=VND&vpc_Locale=vn&vpc_MerchTxnRef=2413&vpc_Merchant=ONEPAY&vpc_Message=Transaction was successful&vpc_OrderInfo=2413&vpc_TransactionNo=1431785&vpc_TxnResponseCode=0&vpc_Version=2 \ No newline at end of file diff --git a/tests/Mock/QuocTeFetchFailure.txt b/tests/Mock/QuocTeFetchFailure.txt new file mode 100755 index 0000000..e956d0d --- /dev/null +++ b/tests/Mock/QuocTeFetchFailure.txt @@ -0,0 +1,7 @@ +HTTP/1.1 200 OK +Date: 05 Apr 2007 23:44:11 GMT +Server: Apache +Connection: close +Content-Type: text/plain; charset=utf-8 + +vpc_DRExists=Y&vpc_AdditionData=970407&vpc_Amount=84000000&vpc_Command=pay&vpc_CurrencyCode=VND&vpc_Locale=vn&vpc_MerchTxnRef=2013042215193440019&vpc_Merchant=ONEPAY&vpc_Message=Transaction was not successful&vpc_OrderInfo=6SE4A1&vpc_TransactionNo=1432021&vpc_TxnResponseCode=100&vpc_Version= \ No newline at end of file diff --git a/tests/Mock/QuocTeFetchSuccess.txt b/tests/Mock/QuocTeFetchSuccess.txt new file mode 100755 index 0000000..df8798e --- /dev/null +++ b/tests/Mock/QuocTeFetchSuccess.txt @@ -0,0 +1,7 @@ +HTTP/1.1 200 OK +Date: 05 Apr 2015 23:44:11 GMT +Server: Apache +Connection: close +Content-Type: text/plain; charset=utf-8 + +vpc_DRExists=Y&vpc_AdditionData=686868&vpc_Amount=100000100&vpc_Command=pay&vpc_CurrencyCode=VND&vpc_Locale=vn&vpc_MerchTxnRef=2413&vpc_Merchant=ONEPAY&vpc_Message=Transaction was successful&vpc_OrderInfo=2413&vpc_TransactionNo=1431785&vpc_TxnResponseCode=0&vpc_Version=2 \ No newline at end of file diff --git a/tests/Mock/QuocTePurchaseFailure.txt b/tests/Mock/QuocTePurchaseFailure.txt new file mode 100755 index 0000000..859dde1 --- /dev/null +++ b/tests/Mock/QuocTePurchaseFailure.txt @@ -0,0 +1,8 @@ +HTTP/1.1 200 OK +Date:Tue, 23 Jun 2015 04:30:09 GMT +Server: Apache +Content-Length: 5830 +Connection: close +Content-Type: text/plain; charset=utf-8 + +http://truonghoang.cool/app_dev.php/backend/process_transaction.html/24?client_key=94bc04c3760620d537b6717abd53ff3e&action=return?vpc_AccessCode=6BEB2546&vpc_Command=pay&vpc_OrderInfo=Order_24_1434339624&vpc_Message=Field+AgainLink+value+is+invalid.&Title=VPC+3-Party&vpc_Version=2&client_refer=http%3A%2F%2Ftruonghoang.cool%2Fapp_dev.php%2Fbackend%2F&vpc_Locale=vn&vpc_TicketNo=192.168.0.1&vpc_Merchant=TESTONEPAY&vpc_Amount=100&vpc_Currency=VND&vpc_SecureHash=15A776D4163F685B22CF981C96C4990EDDE888AD29A6A31E1C5C193555017DF0&vpc_MerchTxnRef=20150615104024530985749 \ No newline at end of file diff --git a/tests/Mock/QuocTePurchaseSuccess.txt b/tests/Mock/QuocTePurchaseSuccess.txt new file mode 100755 index 0000000..0c1a0b1 --- /dev/null +++ b/tests/Mock/QuocTePurchaseSuccess.txt @@ -0,0 +1,7 @@ +HTTP/1.1 200 OK +Date: Fri, 15 Feb 2015 19:19:21 GMT +Server: Apache +Connection: close +Content-Type: text/plain; charset=utf-8 + +vpc_DRExists=Y&vpc_AdditionData=686868&vpc_Amount=100000100&vpc_Command=pay&vpc_CurrencyCode=VND&vpc_Locale=vn&vpc_MerchTxnRef=2413&vpc_Merchant=ONEPAY&vpc_Message=Transaction was successful&vpc_OrderInfo=2413&vpc_TransactionNo=1431785&vpc_TxnResponseCode=0&vpc_Version=2 \ No newline at end of file diff --git a/tests/NoiDiaGatewayTest.php b/tests/NoiDiaGatewayTest.php new file mode 100755 index 0000000..821ae4c --- /dev/null +++ b/tests/NoiDiaGatewayTest.php @@ -0,0 +1,143 @@ +gateway = new NoiDiaGateway($this->getHttpClient(), $this->getHttpRequest()); + + $this->options = [ + 'vpcMerchant' => 'ONEPAY', + 'vpcAccessCode' => 'D67342C2', + 'secureHash' => 'A3EFDFABA8653DF2342E8DAC29B51AF0', + 'testMode' => true, + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'returnUrl' => 'http://truonghoang.cool/app_dev.php/backend/process_transaction.html/1431785?client_key=94bc04c3760620d537b6717abd53ff3e&action=return', + 'amount' => '1000', + 'currency' => 'VND', + 'transactionId' => '1431785' + ]; + } + + + public function testPurchaseSuccess() + { + $response = $this->gateway->purchase($this->options)->send(); + +// $this->assertEquals('https://mtf.onepay.vn/onecomm-pay/vpc.op?' . http_build_query($this->options, '', '&'), $response->getRedirectUrl()); + + $this->assertInstanceOf('\Omnipay\OnePay\Message\NoiDiaPurchaseResponse', $response); + + $this->assertTrue($response->isRedirect()); + + //mock data after redirect request + $this->setMockHttpResponse('NoiDiaPurchaseSuccess.txt'); +// $re = $this->getMockHttpResponse('NoiDiaPurchaseSuccess.txt'); +// Debug::dump($re->getBody(),5);die; + // process return data + $response = $this->gateway->createResponse('\Omnipay\OnePay\Message\Response', [ 'vpc_TxnResponseCode' => 0 ], + 'purchase'); + + $this->assertTrue($response->isSuccessful()); + + // send to complete + $this->testFetchSuccess(); + } + + + public function testPurchaseFailure() + { + $response = $this->gateway->purchase($this->options)->send(); + + $this->assertInstanceOf('\Omnipay\OnePay\Message\NoiDiaPurchaseResponse', $response); + + $this->assertTrue($response->isRedirect()); + + //mock data after redirect request + $this->setMockHttpResponse('NoiDiaPurchaseFailure.txt'); + + $response = $this->gateway->createResponse('\Omnipay\OnePay\Message\Response', + [ 'vpc_Message' => 'Field AgainLink value is invalid.' ], 'purchase'); + + $this->assertFalse($response->isSuccessful()); + + $this->assertSame('Field AgainLink value is invalid.', $response->getMessage()); + } + + + public function testFetchSuccess() + { + $this->setMockHttpResponse('NoiDiaFetchSuccess.txt'); + + $options = [ + 'vpcMerchant' => 'ONEPAY', + 'vpcAccessCode' => 'D67342C2', + 'secureHash' => 'A3EFDFABA8653DF2342E8DAC29B51AF0', + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'testMode' => true, + 'vpc_MerchTxnRef' => '2413' + ]; + + $request = $this->gateway->fetchCheckout($options); + + $this->assertInstanceOf('\Omnipay\OnePay\Message\NoiDiaFetchRequest', $request); + + $this->assertSame('2413', $request->getVpc_MerchTxnRef()); + + $response = $request->send(); + + $this->assertTrue($response->isSuccessful()); + + $this->assertSame('Giao dịch thành công - Approved', $response->getMessage()); + + return $response; + } + + + public function testFetchFailure() + { + $this->setMockHttpResponse('NoiDiaFetchFailure.txt'); + + $options = [ + 'vpcMerchant' => 'ONEPAY', + 'vpcAccessCode' => 'D67342C2', + 'secureHash' => 'A3EFDFABA8653DF2342E8DAC29B51AF0', + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'testMode' => true, + 'vpc_MerchTxnRef' => '2013042215193440019' + ]; + + $request = $this->gateway->fetchCheckout($options); + + $this->assertInstanceOf('\Omnipay\OnePay\Message\NoiDiaFetchRequest', $request); + $this->assertSame('2013042215193440019', $request->getVpc_MerchTxnRef()); + + $response = $request->send(); + $this->assertFalse($response->isSuccessful()); + + $this->assertSame('Giao dịch không thanh toán thành công - Failured', $response->getMessage()); + + return $response; + } +} diff --git a/tests/QuocTeGatewayTest.php b/tests/QuocTeGatewayTest.php new file mode 100755 index 0000000..dcd913c --- /dev/null +++ b/tests/QuocTeGatewayTest.php @@ -0,0 +1,135 @@ +gateway = new QuocTeGateway($this->getHttpClient(), $this->getHttpRequest()); + + $this->options = [ + 'vpcMerchant' => 'TESTONEPAY', + 'vpcAccessCode' => '6BEB2546', + 'secureHash' => 'A3EFDFABA8653DF2342E8DAC29B51AF0', + 'testMode' => true, + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'returnUrl' => 'http://truonghoang.cool/app_dev.php/backend/process_transaction.html/1431785?client_key=94bc04c3760620d537b6717abd53ff3e&action=return', + 'amount' => '1000', + 'currency' => 'VND', + 'transactionId' => '1431786' + ]; + } + + + public function testPurchaseSuccess() + { + $response = $this->gateway->purchase($this->options)->send(); + + $this->assertInstanceOf('\Omnipay\OnePay\Message\QuocTePurchaseResponse', $response); + +// $this->assertEquals('https://mtf.onepay.vn/vpcpay/vpcpay.op?' . http_build_query($this->options, '', '&'), $response->getRedirectUrl()); + + $this->assertTrue($response->isRedirect()); + + //mock data after redirect request + $this->setMockHttpResponse('QuocTePurchaseSuccess.txt'); + + $response = $this->gateway->createResponse('\Omnipay\OnePay\Message\QuocTePurchaseResponse', + [ 'vpc_TxnResponseCode' => 0 ], 'purchase'); + + $this->assertTrue($response->isRedirect()); + $this->assertTrue($response->isSuccessful()); + + $this->assertInstanceOf('\Omnipay\OnePay\Message\QuocTePurchaseResponse', $response); + + $this->assertTrue($response->isSuccessful());; + } + + + public function testPurchaseFailure() + { + $response = $this->gateway->purchase($this->options)->send(); + + $this->assertInstanceOf('\Omnipay\OnePay\Message\QuocTePurchaseResponse', $response); + + $this->assertTrue($response->isRedirect()); + + //mock data after redirect request + $this->setMockHttpResponse('QuocTePurchaseFailure.txt'); + + $response = $this->gateway->createResponse('\Omnipay\OnePay\Message\QuocTePurchaseResponse', + [ 'vpc_Message' => 'Field AgainLink value is invalid.' ], 'purchase'); + + $this->assertFalse($response->isSuccessful()); + + $this->assertSame('Field AgainLink value is invalid.', $response->getMessage()); + } + + + public function testFetchSuccess() + { + $this->setMockHttpResponse('QuocTeFetchSuccess.txt'); + + $options = [ + 'vpcMerchant' => 'TESTONEPAY', + 'vpcAccessCode' => '6BEB2546', + 'testMode' => true, + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'vpc_MerchTxnRef' => 'GDEAXIEM_41382,4523317014', + ]; + + $request = $this->gateway->fetchCheckout($options); + + $this->assertInstanceOf('\Omnipay\OnePay\Message\QuocTeFetchRequest', $request); + $this->assertSame('GDEAXIEM_41382,4523317014', $request->getVpc_MerchTxnRef()); + + $response = $request->send(); + $this->assertTrue($response->isSuccessful()); + + $this->assertSame('Transaction Successful', $response->getMessage()); + } + + + public function testFetchFailure() + { + $this->setMockHttpResponse('QuocTeFetchFailure.txt'); + + $options = [ + 'vpcMerchant' => 'TESTONEPAY', + 'vpcAccessCode' => 'D67342C2', + 'testMode' => true, + 'vpcUser' => 'op01', + 'vpcPassword' => 'op123456', + 'vpc_MerchTxnRef' => 'GDEAXIEM_41382,4523317014', + ]; + + $request = $this->gateway->fetchCheckout($options); + + $this->assertInstanceOf('\Omnipay\OnePay\Message\QuocTeFetchRequest', $request); + $this->assertSame('GDEAXIEM_41382,4523317014', $request->getVpc_MerchTxnRef()); + + $response = $request->send(); + $this->assertFalse($response->isSuccessful()); + + $this->assertSame('Unable to be determined', $response->getMessage()); + } +}