PayHandler.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace common\pay\eagle;
  3. use backend\models\Config;
  4. use backend\models\Deposit;
  5. use backend\models\SyncDesposit;
  6. use backend\models\Member;
  7. use backend\helpers\MailHelper;
  8. use common\pay\BasePayHandler;
  9. use Yii;
  10. use yii\helpers\VarDumper;
  11. class PayHandler extends BasePayHandler
  12. {
  13. public $payUrl; //网关地址
  14. public $merId; //mid
  15. public $privateKey; //商户私钥
  16. /**
  17. * @inheritdoc
  18. */
  19. public function init()
  20. {
  21. parent::init();
  22. if ($this->payUrl == null) {
  23. $this->payUrl = Yii::$app->params['eagle.payUrl']; //网关地址
  24. }
  25. if ($this->merId == null) {
  26. $this->merId = Yii::$app->params['eagle.merId']; //商户号码
  27. }
  28. if ($this->privateKey == null) {
  29. $this->privateKey = Yii::$app->params['eagle.Key']; //商户私钥
  30. }
  31. }
  32. /**
  33. * @param array $deposit
  34. * @param array $params
  35. * @return string
  36. */
  37. public function outPay($deposit, $params = [])
  38. {
  39. $data['merchId'] = $this->merId; //商户号码
  40. $data['time'] = time(); //交易时间
  41. $data['userId'] = $params['login']; //用户唯一标识
  42. $data['coin'] = 'USDT'; //订单号码
  43. $data['sign'] = PayUtils::makeSign($data,$this->privateKey);
  44. $result = PayUtils::sendPost($this->payUrl.'recharge_address', $data); //发送数据
  45. // 对返回的数据进行转化
  46. $result_array = json_decode($result,true);
  47. $member_id = $params['member_id']; // 通过用户id拿到用户的信息 获取到用户的邮箱地址
  48. $tt = print_r($result_array,true);
  49. file_put_contents('1101.txt',$tt);
  50. if($result_array['code']==0){
  51. $res = Member::updateAll(['address1' => $result_array['data'][0]['address']], "id = $member_id");
  52. return PayHandler::createGetHtml($this->payUrl,$result_array['data'][0]['address']);
  53. }else{
  54. return $result_array['message'];
  55. }
  56. }
  57. /**
  58. * @param array $data
  59. * @return bool
  60. */
  61. public function handleNotify($data)
  62. {
  63. $string = print_r($data,true);
  64. file_put_contents('postshuju.txt',$string);
  65. if (isset($data['sign']) && trim($data['sign']) !== '') {
  66. if (PayUtils::checkSign($data,$this->privateKey)) {
  67. file_put_contents('huidiao.txt','验签成功');
  68. if ($data['respCode'] == '00') {
  69. $merOrderId = trim($data['orderId']); //data函数
  70. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  71. if ($reuslt['type'] != 1) {
  72. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  73. $configData = Config::find()->asArray()->one();
  74. if ($configData['auto_deposit'] == 1 && $res) {
  75. $syncDespositModel = new SyncDesposit();
  76. $syncDespositModel->login = $reuslt['login'];
  77. $syncDespositModel->amount = $reuslt['amount'];
  78. $syncDespositModel->comment = 'Deposit';
  79. $syncDespositModel->memo = $merOrderId;
  80. $syncDespositModel->type = 2;
  81. $syncDespositModel->in_time = time();
  82. $syncDespositModel->save();
  83. }
  84. }
  85. return true;
  86. }else{
  87. return false;
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. public function outNotify($success)
  94. {
  95. if ($success == true) {
  96. return "ok";
  97. } else {
  98. return "fail";
  99. }
  100. }
  101. /**
  102. * @param array $data
  103. * @return bool
  104. */
  105. public function handleReturn($data)
  106. {
  107. //Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  108. if (isset($data['sign']) && trim($data['sign']) !== '') {
  109. if (PayUtils::checkSign($data,$this->privateKey)) {
  110. if ($data['respCode'] == '00') {
  111. $merOrderId = trim($data['orderId']);
  112. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  113. if ($reuslt) {
  114. return true;
  115. }
  116. }
  117. }
  118. }
  119. return false;
  120. }
  121. public function outReturn($success)
  122. {
  123. }
  124. // 生成html模板文件
  125. public static function createGetHtml($url,$address2)
  126. {
  127. $html = <<<eot
  128. <div style="border: 1px solid green;width:800px;height: 400px;margin:10% auto;text-align: center;border-radius:30px;padding-top:70px; ">
  129. <h4>重要通知</h4>
  130. <p>你的钱包绑定成功.</p>
  131. <span style="color:red;" id="address2"></span>
  132. </div>
  133. <script src="/ui/js/jquery.1.9.1.min.js"></script>
  134. <script src="/ui/js/jquery-qrcode-0.14.0.min.js"></script>
  135. <script>
  136. $('#address2').qrcode({
  137. render: "canvas",
  138. width: 100,
  139. height: 100,
  140. text: "{$url}{$address2}"
  141. });
  142. </script>
  143. eot;
  144. return $html;
  145. }
  146. }