PayController.php 3.7 KB

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