PayController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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'] == 9) {
  29. $data['bankCode'] = isset($data['huiyingBankCode']) ? trim($data['huiyingBankCode']) : '';
  30. }
  31. if ($data['payType'] == 10) {
  32. $data['bankCode'] = isset($data['chuanghuiBankCode']) ? trim($data['chuanghuiBankCode']) : '';
  33. }
  34. if ($data['payType'] == 11) {
  35. $data['bankCode'] = isset($data['nongzhitongBankCode']) ? trim($data['nongzhitongBankCode']) : '';
  36. }
  37. $model->setAttributes($data);
  38. if ($model->outPay()) {
  39. $result = $model->getOutPayResult();
  40. if ($result['type'] == 'html') {
  41. if ($data['payType'] == 3) {
  42. return $this->sandPayHtmlHandler($result['html']);
  43. } elseif($data['payType'] == 7) {
  44. for ($i = 0; $i <= 31; ++$i) {
  45. $result['html'] = str_replace(chr($i), "", $result['html']);
  46. }
  47. $result['html'] = str_replace(chr(127), "", $result['html']);
  48. if (0 === strpos(bin2hex($result['html']), 'efbbbf')) {
  49. $result['html'] = substr($result['html'], 3);
  50. }
  51. $tmp = json_decode($result['html'], true);
  52. $arr = parse_url($tmp['qr_src']);
  53. $queryParts = explode('&', $arr['query']);
  54. $params = array();
  55. foreach ($queryParts as $param)
  56. {
  57. $item = explode('=', $param);
  58. $params[$item[0]] = $item[1];
  59. }
  60. $orderAmount = $params['amount'];
  61. //return $this->redirect($tmp['qr_src']);
  62. $qr_img = "https://chart.googleapis.com/chart?chs=250x250&cht=qr&chl=bitcoin:{$tmp['payee_address']}?amount={$orderAmount}";
  63. $data['qr_img'] = $qr_img;
  64. $data['amount'] = $orderAmount;
  65. $data['payee_address'] = $tmp['payee_address'];
  66. return $this->render('ctypepay',['data' => $data, ]);
  67. }
  68. return $result['html'];
  69. } else {
  70. throw new BadRequestHttpException('支付异常');
  71. }
  72. } else {
  73. throw new BadRequestHttpException(json_encode($model->getErrors(), 320));
  74. }
  75. }
  76. throw new BadRequestHttpException('Bad Request');
  77. }
  78. public function actionSucceed()
  79. {
  80. return $this->render('succeed');
  81. }
  82. public function actionFailed()
  83. {
  84. return $this->render('failed');
  85. }
  86. public function sandPayHtmlHandler($result)
  87. {
  88. $jqueryJs = STATIC_URL.'/ui/js/sand/jquery-1.7.2.min.js';
  89. $paymentJs = STATIC_URL.'/ui/js/sand/paymentjs.js';
  90. $html = <<<eot
  91. <html>
  92. <head>
  93. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  94. <meta name="renderer" content="webkit" />
  95. <script type="text/javascript" src="{$jqueryJs}"></script>
  96. <script type="text/javascript" src="{$paymentJs}"></script>
  97. <script>
  98. function wap_pay() {
  99. var responseText = $("#credential").text();
  100. paymentjs.createPayment(responseText, function(result, err) {
  101. });
  102. }
  103. </script>
  104. </head>
  105. <body>
  106. <div style="display: none" >
  107. <p id="credential">{$result}</p>
  108. </div>
  109. <script>
  110. window.onload=function(){
  111. wap_pay();
  112. //window.location.href = "http://www.jb51.net";
  113. };
  114. </script>
  115. </body>
  116. </html>
  117. eot;
  118. return $html;
  119. }
  120. }