PayController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace backend\modules\user\controllers;
  3. use backend\models\forms\PayForm;
  4. use Yii;
  5. use yii\web\BadRequestHttpException;
  6. class PayController extends BaseController
  7. {
  8. /**
  9. * @return \yii\web\Response
  10. * @throws \Exception
  11. */
  12. public function actionOutpay()
  13. {
  14. $model = new PayForm();
  15. if (Yii::$app->getRequest()->getIsPost()) {
  16. $data = Yii::$app->getRequest()->post();
  17. $payMethod = isset($data['payMethod']) ? trim($data['payMethod']) : '';
  18. $data['payType'] = $model->getPayTypeByMethod($payMethod);
  19. if ($data['payType'] == 1) {
  20. $data['bankCode'] = isset($data['renrenBankCode']) ? trim($data['renrenBankCode']) : '';
  21. }
  22. if ($data['payType'] == 3) {
  23. $data['bankCode'] = isset($data['sandBankCode']) ? trim($data['sandBankCode']) : '';
  24. }
  25. if ($data['payType'] == 8) {
  26. $data['bankCode'] = isset($data['kexingBankCode']) ? trim($data['kexingBankCode']) : '';
  27. }
  28. if ($data['payType'] == 10) {
  29. $data['bankCode'] = isset($data['otczhifuBankCode']) ? trim($data['otczhifuBankCode']) : '';
  30. }
  31. if ($data['payType'] == 11) {
  32. $data['amount'] = 0.00;
  33. }
  34. if ($data['payType'] == 12) {
  35. $data['amount'] = 0.00;
  36. }
  37. if ($data['payType'] == 13) {
  38. $data['amount'] = 0.00;
  39. }
  40. if ($data['payType'] == 14) {
  41. $data['amount'] = 0.00;
  42. }
  43. if ($data['payType'] == 16) {
  44. $data['bankCode'] = isset($data['duisiBankCode']) ? trim($data['duisiBankCode']) : '';
  45. $amout_back = rand(1,70);
  46. $data['amount'] = sprintf("%.2f",$data['amount']) + $amout_back/100;
  47. }
  48. if ($data['payType'] == 17) {
  49. $data['bankCode'] = isset($data['duigongBankCode']) ? trim($data['duigongBankCode']) : '';
  50. $amout_back = rand(1,70);
  51. $data['amount'] = sprintf("%.2f",$data['amount']) + $amout_back/100;
  52. }
  53. if ($data['payType'] == 18) {
  54. $data['bankCode'] = isset($data['globalpayBankCode']) ? trim($data['globalpayBankCode']) : '';
  55. }
  56. if ($data['payType'] == 19) {
  57. $data['bankCode'] = isset($data['payplatBankCode']) ? trim($data['payplatBankCode']) : '';
  58. }
  59. //echo "<pre>";
  60. //var_dump($data);die;
  61. $model->setAttributes($data);
  62. if ($model->outPay()) {
  63. $result = $model->getOutPayResult();
  64. if ($result['type'] == 'html') {
  65. if ($data['payType'] == 3) {
  66. return $this->sandPayHtmlHandler($result['html']);
  67. } elseif($data['payType'] == 7) {
  68. for ($i = 0; $i <= 31; ++$i) {
  69. $result['html'] = str_replace(chr($i), "", $result['html']);
  70. }
  71. $result['html'] = str_replace(chr(127), "", $result['html']);
  72. if (0 === strpos(bin2hex($result['html']), 'efbbbf')) {
  73. $result['html'] = substr($result['html'], 3);
  74. }
  75. $tmp = json_decode($result['html'], true);
  76. $arr = parse_url($tmp['qr_src']);
  77. $queryParts = explode('&', $arr['query']);
  78. $params = array();
  79. foreach ($queryParts as $param)
  80. {
  81. $item = explode('=', $param);
  82. $params[$item[0]] = $item[1];
  83. }
  84. $orderAmount = $params['amount'];
  85. //return $this->redirect($tmp['qr_src']);
  86. $qr_img = "https://chart.googleapis.com/chart?chs=250x250&cht=qr&chl=bitcoin:{$tmp['payee_address']}?amount={$orderAmount}";
  87. $data['qr_img'] = $qr_img;
  88. $data['amount'] = $orderAmount;
  89. $data['payee_address'] = $tmp['payee_address'];
  90. return $this->render('ctypepay',['data' => $data, ]);
  91. }
  92. if($data['payType'] == 10){
  93. if($result['html']['code']==200){
  94. $url = $result['html']['data'];
  95. header("location:$url");
  96. exit;
  97. }else{
  98. echo $result['html']['msg'];
  99. exit;
  100. }
  101. }
  102. if($data['payType'] == 18){
  103. if($result['html']['code']==200){
  104. $url = $result['html']['pay_url'];
  105. header("location:$url");
  106. exit;
  107. }else{
  108. echo $result['html']['error'];
  109. exit;
  110. }
  111. }
  112. return $result['html'];
  113. } else {
  114. throw new BadRequestHttpException('支付异常');
  115. }
  116. } else {
  117. throw new BadRequestHttpException(json_encode($model->getErrors(), 320));
  118. }
  119. }
  120. throw new BadRequestHttpException('Bad Request');
  121. }
  122. public function actionSucceed()
  123. {
  124. return $this->render('succeed');
  125. }
  126. public function actionFailed()
  127. {
  128. return $this->render('failed');
  129. }
  130. public function sandPayHtmlHandler($result)
  131. {
  132. $jqueryJs = STATIC_URL.'/ui/js/sand/jquery-1.7.2.min.js';
  133. $paymentJs = STATIC_URL.'/ui/js/sand/paymentjs.js';
  134. $html = <<<eot
  135. <html>
  136. <head>
  137. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  138. <meta name="renderer" content="webkit" />
  139. <script type="text/javascript" src="{$jqueryJs}"></script>
  140. <script type="text/javascript" src="{$paymentJs}"></script>
  141. <script>
  142. function wap_pay() {
  143. var responseText = $("#credential").text();
  144. paymentjs.createPayment(responseText, function(result, err) {
  145. });
  146. }
  147. </script>
  148. </head>
  149. <body>
  150. <div style="display: none" >
  151. <p id="credential">{$result}</p>
  152. </div>
  153. <script>
  154. window.onload=function(){
  155. wap_pay();
  156. //window.location.href = "http://www.jb51.net";
  157. };
  158. </script>
  159. </body>
  160. </html>
  161. eot;
  162. return $html;
  163. }
  164. }