DemoController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. file_put_contents('./text.txt',$vpc_MerchTxnRef);
  62. $params = array(
  63. 'vpc_Version' => '1',
  64. 'vpc_Command' => 'queryDR',
  65. 'vpc_MerchTxnRef' => $vpc_MerchTxnRef,
  66. 'vpc_User' => 'op01',
  67. 'vpc_Password' => 'op123456',
  68. );
  69. //if($this->gateway === 'Inland'){
  70. $vpcURL = 'https://mtf.onepay.vn/onecomm-pay/Vpcdps.op';
  71. $params['vpc_AccessCode'] = '6BEB2546';
  72. $params['vpc_Merchant'] = 'TESTONEPAY';
  73. // }else
  74. // {
  75. // $vpcURL = 'https://mtf.onepay.vn/vpcpay/Vpcdps.op';
  76. // $params['vpc_AccessCode'] = '6BEB2546';
  77. // $params['vpc_Merchant'] = 'TESTONEPAY';
  78. // }
  79. $postData = "";
  80. $ampersand = "";
  81. foreach ($params as $key => $value) {
  82. if (strlen($value) > 0) {
  83. $postData .= $ampersand . urlencode($key) . '=' . urlencode($value);
  84. $ampersand = "&";
  85. }
  86. }
  87. ob_start();
  88. $ch = curl_init();
  89. curl_setopt($ch, CURLOPT_URL, $vpcURL);
  90. curl_setopt($ch, CURLOPT_POST, 1);
  91. curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  92. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  93. curl_exec($ch);
  94. $response = ob_get_contents();
  95. ob_end_clean();
  96. $message = "";
  97. if (strchr($response, "<html>") || strchr($response, "<html>")) {
  98. $message = $response;
  99. } else {
  100. if (curl_error($ch))
  101. $message = "%s: s" . curl_errno($ch) . "<br/>" . curl_error($ch);
  102. }
  103. curl_close($ch);
  104. $map = array();
  105. if (strlen($message) == 0) {
  106. $pairArray = explode("&", $response);
  107. foreach ($pairArray as $pair) {
  108. $param = explode("=", $pair);
  109. $map[urldecode($param[0])] = urldecode($param[1]);
  110. }
  111. $message = $this->null2unknown($map, "vpc_Message");
  112. }
  113. $amount = $this->null2unknown($map, "vpc_Amount");
  114. $locale = $this->null2unknown($map, "vpc_Locale");
  115. $batchNo = $this->null2unknown($map, "vpc_BatchNo");
  116. $command = $this->null2unknown($map, "vpc_Command");
  117. $version = $this->null2unknown($map, "vpc_Version");
  118. $cardType = $this->null2unknown($map, "vpc_Card");
  119. $orderInfo = $this->null2unknown($map, "vpc_OrderInfo");
  120. $receiptNo = $this->null2unknown($map, "vpc_ReceiptNo");
  121. $merchantID = $this->null2unknown($map, "vpc_Merchant");
  122. $authorizeID = $this->null2unknown($map, "vpc_AuthorizeId");
  123. $transactionNo = $this->null2unknown($map, "vpc_TransactionNo");
  124. $acqResponseCode = $this->null2unknown($map, "vpc_AcqResponseCode");
  125. $txnResponseCode = $this->null2unknown($map, "vpc_TxnResponseCode");
  126. $drExists = $this->null2unknown($map, "vpc_DRExists");
  127. $multipleDRs = $this->null2unknown($map, "vpc_FoundMultipleDRs");
  128. $verType = $this->null2unknown($map, "vpc_VerType");
  129. $verStatus = $this->null2unknown($map, "vpc_VerStatus");
  130. $token = $this->null2unknown($map, "vpc_VerToken");
  131. $verSecurLevel = $this->null2unknown($map, "vpc_VerSecurityLevel");
  132. $enrolled = $this->null2unknown($map, "vpc_3DSenrolled");
  133. $xid = $this->null2unknown($map, "vpc_3DSXID");
  134. $acqECI = $this->null2unknown($map, "vpc_3DSECI");
  135. $authStatus = $this->null2unknown($map, "vpc_3DSstatus");
  136. $shopTransNo = $this->null2unknown($map, "vpc_ShopTransactionNo");
  137. $authorisedAmount = $this->null2unknown($map, "vpc_AuthorisedAmount");
  138. $capturedAmount = $this->null2unknown($map, "vpc_CapturedAmount");
  139. $refundedAmount = $this->null2unknown($map, "vpc_RefundedAmount");
  140. $ticketNumber = $this->null2unknown($map, "vpc_TicketNo");
  141. $errorTxt = "";
  142. if ($txnResponseCode == "7" || $txnResponseCode == "No Value Returned") {
  143. $errorTxt = "Error";
  144. }
  145. $transStatus = "";
  146. if ($txnResponseCode == "0") {
  147. $transStatus = "Giao dịch thành công";
  148. } elseif ($txnResponseCode != "0") {
  149. $transStatus = "Giao dịch thất bại";
  150. }
  151. $result = array(
  152. 'OnePay' => array(
  153. 'errorTxt' => $errorTxt,
  154. 'resCode' => $txnResponseCode,
  155. 'resDescription' => $this->getResponseDescription($txnResponseCode)
  156. )
  157. );
  158. return json_encode($result);
  159. }
  160. private function getResponseDescription($responseCode)
  161. {
  162. switch ($responseCode) {
  163. case "0" :
  164. $result = "Transaction Successful";
  165. break;
  166. case "?" :
  167. $result = "Transaction status is unknown";
  168. break;
  169. case "1" :
  170. $result = "Bank system reject";
  171. break;
  172. case "2" :
  173. $result = "Bank Declined Transaction";
  174. break;
  175. case "3" :
  176. $result = "No Reply from Bank";
  177. break;
  178. case "4" :
  179. $result = "Expired Card";
  180. break;
  181. case "5" :
  182. $result = "Insufficient funds";
  183. break;
  184. case "6" :
  185. $result = "Error Communicating with Bank";
  186. break;
  187. case "7" :
  188. $result = "Payment Server System Error";
  189. break;
  190. case "8" :
  191. $result = "Transaction Type Not Supported";
  192. break;
  193. case "9" :
  194. $result = "Bank declined transaction (Do not contact Bank)";
  195. break;
  196. case "A" :
  197. $result = "Transaction Aborted";
  198. break;
  199. case "C" :
  200. $result = "Transaction Cancelled";
  201. break;
  202. case "D" :
  203. $result = "Deferred transaction has been received and is awaiting processing";
  204. break;
  205. case "F" :
  206. $result = "3D Secure Authentication failed";
  207. break;
  208. case "I" :
  209. $result = "Card Security Code verification failed";
  210. break;
  211. case "L" :
  212. $result = "Shopping Transaction Locked (Please try the transaction again later)";
  213. break;
  214. case "N" :
  215. $result = "Cardholder is not enrolled in Authentication scheme";
  216. break;
  217. case "P" :
  218. $result = "Transaction has been received by the Payment Adaptor and is being processed";
  219. break;
  220. case "R" :
  221. $result = "Transaction was not processed - Reached limit of retry attempts allowed";
  222. break;
  223. case "S" :
  224. $result = "Duplicate SessionID (OrderInfo)";
  225. break;
  226. case "T" :
  227. $result = "Address Verification Failed";
  228. break;
  229. case "U" :
  230. $result = "Card Security Code Failed";
  231. break;
  232. case "V" :
  233. $result = "Address Verification and Card Security Code Failed";
  234. break;
  235. default :
  236. $result = "Unable to be determined";
  237. }
  238. return $result;
  239. }
  240. private function null2unknown($map, $key)
  241. {
  242. if (array_key_exists($key, $map)) {
  243. if (!is_null($map[$key])) {
  244. return $map[$key];
  245. }
  246. }
  247. return "No Value Returned";
  248. }
  249. public function actionIpay(){
  250. $data = array(
  251. 'ApiVersion' => '2.0',
  252. 'MerchantCode' => 'ID00001',
  253. 'PaymentId' => '71',
  254. 'Currency' => 'IDR',
  255. 'RefNo' => 'A00000001',
  256. 'Amount' => '3000',
  257. 'ProdDesc' => 'test',
  258. 'UserName' => 'Thoriq',
  259. 'UserEmail' => 'thoriq@ipay88.co.id',
  260. 'UserContact' => '08123123123',
  261. 'Lang' => 'UTF-8',
  262. 'ResponseURL' => 'http://16.163.228.151:8040/v1/demo/ipayments',
  263. 'BackendURL' => 'http://16.163.228.151:8040/v1/demo/ipay',
  264. 'ItemTransactions' => array(
  265. "Id" => "00001",
  266. "Name" => "Smartphone Wireless Charger",
  267. "Quantity" => "1",
  268. "Amount" => "3000",
  269. "ParentType" => "SELLER",
  270. "ParentId" => "SELLER123",
  271. ),
  272. 'ShippingAddress' => array(
  273. "FirstName" => "Techsupp",
  274. "LastName" => "Alpha",
  275. "Address" => "Jl. Test BlokRNo.1",
  276. "City" => "Jakarta",
  277. "State" => "DKI Jakarta",
  278. "PostalCode" => "18800",
  279. "Phone" => "08123456789",
  280. "CountryCode" => "1",
  281. ),
  282. 'BillingAddress' => array(
  283. "FirstName" => 'test',
  284. "LastName" => "test",
  285. "Address" => "test",
  286. "City" => "test",
  287. "State" => "test",
  288. "PostalCode" => "11480",
  289. "Phone" => "08788888888",
  290. "CountryCode" => "2222",
  291. ),
  292. 'Sellers' => array(
  293. "Id" => "SELLER123",
  294. "Name" => "Sellername",
  295. "LegalId" => "3274011001900001",
  296. "SellerIdNumber" => null,
  297. "Email" => "sellername@gmail.com",
  298. "Url" => "www.sunrise.com",
  299. "address" => array(
  300. "FirstName" => 'test',
  301. "LastName" => "test",
  302. "Address" => "test",
  303. "City" => "test",
  304. "State" => "test",
  305. "PostalCode" => "11480",
  306. "Phone" => "08788888888",
  307. "CountryCode" => "2222",
  308. ),
  309. ),
  310. "SettingField" => array(
  311. "Name" => 'TenorInstallment',
  312. "Value" => '3',
  313. ),
  314. );
  315. $MerchantKey = 'apple';
  316. $url = "https://sandbox.ipay88.co.id/ePayment/WebService/PaymentAPI/Checkout";
  317. $str = "||" . $MerchantKey . "||" . $data['MerchantCode'] . "||" . $data['RefNo'] . "||" . $data['Amount'] . "||" . $data['Currency'] . "||";
  318. $data['Signature'] = hash('sha256', $str);
  319. $res = $this->curl_post($url,json_encode($data));
  320. print_r($res);die;
  321. //return static::notice($res);
  322. }
  323. private function curl_post($url,$param){
  324. if (empty($url) || empty($param)) {
  325. return false;
  326. }
  327. $postUrl = $url;
  328. $curlPost = $param;
  329. $ch = curl_init();//初始化curl
  330. curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
  331. curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
  332. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  333. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  334. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  335. $data = curl_exec($ch);//运行curl
  336. curl_close($ch);
  337. print_r($data);die;
  338. return $data;
  339. }
  340. public function actionIpayments(){
  341. $data = file_get_contents('php://input');
  342. file_put_contents('./text1.txt',$data);
  343. }
  344. }