DemoController.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace backendApi\modules\v1\controllers;
  9. use backendApi\modules\v1\models\Admin;
  10. use backendApi\modules\v1\models\lists\user\IndexList;
  11. use common\models\AdLocation;
  12. use common\models\FlowBonus;
  13. use common\models\User;
  14. class DemoController extends BaseController
  15. {
  16. public $modelClass = User::class;
  17. public function actions()
  18. {
  19. return parent::actions(); // TODO: Change the autogenerated stub
  20. }
  21. /**
  22. * 支付demo
  23. * @return mixed
  24. * @throws \yii\web\HttpException
  25. */
  26. public function actionPay()
  27. {
  28. $money = \Yii::$app->request->post('money');
  29. $data['vpc_Currency'] = 'VND';
  30. $data['vpc_Version'] = 2;
  31. $data['vpc_Command'] = 'pay';
  32. $data['vpc_AccessCode'] = '6BEB2546';
  33. $data['vpc_Merchant'] = 'TESTONEPAY';
  34. $data['vpc_Locale'] = 'en';
  35. $data['vpc_ReturnURL'] = 'http://16.163.228.151:8040/v1/demo/payments';
  36. $data['vpc_MerchTxnRef'] = rand(10000, 99999);
  37. $data['vpc_OrderInfo'] = '123456';
  38. $data['vpc_Amount'] = $money*100;
  39. $data['vpc_TicketNo'] = $_SERVER ['REMOTE_ADDR'];
  40. $data['AgainLink'] = 'http://16.163.228.151:8040/v1/demo/payments'; //跳转回当前页面地址
  41. $data['Title'] = 'pay';
  42. ksort($data);
  43. $url = "https://mtf.onepay.vn/paygate/vpcpay.op?";
  44. $md5HashData = '';
  45. foreach ($data as $key => $value) {
  46. $url .= urlencode($key) . '=' . urlencode($value) . '&';
  47. if ((strlen($value) > 0) && ((substr($key, 0, 4) == "vpc_") || (substr($key, 0, 5) == "user_"))) {
  48. $md5HashData .= $key . "=" . $value . "&";
  49. }
  50. }
  51. $SECURE_SECRET = "6D0870CDE5F24F34F3915FB0045120DB";
  52. $md5HashData = rtrim($md5HashData, "&");
  53. $url .= "vpc_SecureHash=" . strtoupper(hash_hmac('SHA256', $md5HashData, pack('H*', $SECURE_SECRET)));
  54. // $listObj = new IndexList();
  55. // $res = $listObj->curl_get($url);
  56. // print_r($res);
  57. // die;
  58. return static::notice($url);
  59. }
  60. public function actionPayments($vpc_MerchTxnRef){
  61. $params = array(
  62. 'vpc_Version' => '1',
  63. 'vpc_Command' => 'queryDR',
  64. 'vpc_MerchTxnRef' => $vpc_MerchTxnRef,
  65. 'vpc_User' => 'op01',
  66. 'vpc_Password' => 'op123456',
  67. );
  68. //if($this->gateway === 'Inland'){
  69. $vpcURL = 'https://mtf.onepay.vn/onecomm-pay/Vpcdps.op';
  70. $params['vpc_AccessCode'] = 'D67342C2';
  71. $params['vpc_Merchant'] = 'ONEPAY';
  72. // }else
  73. // {
  74. // $vpcURL = 'https://mtf.onepay.vn/vpcpay/Vpcdps.op';
  75. // $params['vpc_AccessCode'] = '6BEB2546';
  76. // $params['vpc_Merchant'] = 'TESTONEPAY';
  77. // }
  78. $postData = "";
  79. $ampersand = "";
  80. foreach ($params as $key => $value) {
  81. if (strlen($value) > 0) {
  82. $postData .= $ampersand . urlencode($key) . '=' . urlencode($value);
  83. $ampersand = "&";
  84. }
  85. }
  86. ob_start();
  87. $ch = curl_init();
  88. curl_setopt($ch, CURLOPT_URL, $vpcURL);
  89. curl_setopt($ch, CURLOPT_POST, 1);
  90. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  91. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  92. curl_exec($ch);
  93. $response = ob_get_contents();
  94. ob_end_clean();
  95. $message = "";
  96. if (strchr($response, "<html>") || strchr($response, "<html>")) {
  97. $message = $response;
  98. } else {
  99. if (curl_error($ch))
  100. $message = "%s: s" . curl_errno($ch) . "<br/>" . curl_error($ch);
  101. }
  102. curl_close($ch);
  103. $map = array();
  104. if (strlen($message) == 0) {
  105. $pairArray = str_split("&", $response);
  106. foreach ($pairArray as $pair) {
  107. $param = str_split("=", $pair);
  108. $map[urldecode($param[0])] = urldecode($param[1]);
  109. }
  110. $message = $this->null2unknown($map, "vpc_Message");
  111. }
  112. $amount = $this->null2unknown($map, "vpc_Amount");
  113. $locale = $this->null2unknown($map, "vpc_Locale");
  114. $batchNo = $this->null2unknown($map, "vpc_BatchNo");
  115. $command = $this->null2unknown($map, "vpc_Command");
  116. $version = $this->null2unknown($map, "vpc_Version");
  117. $cardType = $this->null2unknown($map, "vpc_Card");
  118. $orderInfo = $this->null2unknown($map, "vpc_OrderInfo");
  119. $receiptNo = $this->null2unknown($map, "vpc_ReceiptNo");
  120. $merchantID = $this->null2unknown($map, "vpc_Merchant");
  121. $authorizeID = $this->null2unknown($map, "vpc_AuthorizeId");
  122. $transactionNo = $this->null2unknown($map, "vpc_TransactionNo");
  123. $acqResponseCode = $this->null2unknown($map, "vpc_AcqResponseCode");
  124. $txnResponseCode = $this->null2unknown($map, "vpc_TxnResponseCode");
  125. $drExists = $this->null2unknown($map, "vpc_DRExists");
  126. $multipleDRs = $this->null2unknown($map, "vpc_FoundMultipleDRs");
  127. $verType = $this->null2unknown($map, "vpc_VerType");
  128. $verStatus = $this->null2unknown($map, "vpc_VerStatus");
  129. $token = $this->null2unknown($map, "vpc_VerToken");
  130. $verSecurLevel = $this->null2unknown($map, "vpc_VerSecurityLevel");
  131. $enrolled = $this->null2unknown($map, "vpc_3DSenrolled");
  132. $xid = $this->null2unknown($map, "vpc_3DSXID");
  133. $acqECI = $this->null2unknown($map, "vpc_3DSECI");
  134. $authStatus = $this->null2unknown($map, "vpc_3DSstatus");
  135. $shopTransNo = $this->null2unknown($map, "vpc_ShopTransactionNo");
  136. $authorisedAmount = $this->null2unknown($map, "vpc_AuthorisedAmount");
  137. $capturedAmount = $this->null2unknown($map, "vpc_CapturedAmount");
  138. $refundedAmount = $this->null2unknown($map, "vpc_RefundedAmount");
  139. $ticketNumber = $this->null2unknown($map, "vpc_TicketNo");
  140. $errorTxt = "";
  141. if ($txnResponseCode == "7" || $txnResponseCode == "No Value Returned") {
  142. $errorTxt = "Error";
  143. }
  144. $transStatus = "";
  145. if ($txnResponseCode == "0") {
  146. $transStatus = "Giao dịch thành công";
  147. } elseif ($txnResponseCode != "0") {
  148. $transStatus = "Giao dịch thất bại";
  149. }
  150. $result = array(
  151. 'OnePay' => array(
  152. 'errorTxt' => $errorTxt,
  153. 'resCode' => $txnResponseCode,
  154. 'resDescription' => $this->getResponseDescription($txnResponseCode)
  155. )
  156. );
  157. return json_encode($result);
  158. }
  159. private function getResponseDescription($responseCode)
  160. {
  161. switch ($responseCode) {
  162. case "0" :
  163. $result = "Transaction Successful";
  164. break;
  165. case "?" :
  166. $result = "Transaction status is unknown";
  167. break;
  168. case "1" :
  169. $result = "Bank system reject";
  170. break;
  171. case "2" :
  172. $result = "Bank Declined Transaction";
  173. break;
  174. case "3" :
  175. $result = "No Reply from Bank";
  176. break;
  177. case "4" :
  178. $result = "Expired Card";
  179. break;
  180. case "5" :
  181. $result = "Insufficient funds";
  182. break;
  183. case "6" :
  184. $result = "Error Communicating with Bank";
  185. break;
  186. case "7" :
  187. $result = "Payment Server System Error";
  188. break;
  189. case "8" :
  190. $result = "Transaction Type Not Supported";
  191. break;
  192. case "9" :
  193. $result = "Bank declined transaction (Do not contact Bank)";
  194. break;
  195. case "A" :
  196. $result = "Transaction Aborted";
  197. break;
  198. case "C" :
  199. $result = "Transaction Cancelled";
  200. break;
  201. case "D" :
  202. $result = "Deferred transaction has been received and is awaiting processing";
  203. break;
  204. case "F" :
  205. $result = "3D Secure Authentication failed";
  206. break;
  207. case "I" :
  208. $result = "Card Security Code verification failed";
  209. break;
  210. case "L" :
  211. $result = "Shopping Transaction Locked (Please try the transaction again later)";
  212. break;
  213. case "N" :
  214. $result = "Cardholder is not enrolled in Authentication scheme";
  215. break;
  216. case "P" :
  217. $result = "Transaction has been received by the Payment Adaptor and is being processed";
  218. break;
  219. case "R" :
  220. $result = "Transaction was not processed - Reached limit of retry attempts allowed";
  221. break;
  222. case "S" :
  223. $result = "Duplicate SessionID (OrderInfo)";
  224. break;
  225. case "T" :
  226. $result = "Address Verification Failed";
  227. break;
  228. case "U" :
  229. $result = "Card Security Code Failed";
  230. break;
  231. case "V" :
  232. $result = "Address Verification and Card Security Code Failed";
  233. break;
  234. default :
  235. $result = "Unable to be determined";
  236. }
  237. return $result;
  238. }
  239. private function null2unknown($map, $key)
  240. {
  241. if (array_key_exists($key, $map)) {
  242. if (!is_null($map[$key])) {
  243. return $map[$key];
  244. }
  245. }
  246. return "No Value Returned";
  247. }
  248. }