PayHandler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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, $data); //对数据判断,然后发生邮件
  45. // 对返回的数据进行转化
  46. $result_array = json_decode($result,true);
  47. $member_id = $params['member_id']; // 通过用户id拿到用户的信息 获取到用户的邮箱地址
  48. if($result_array['code']==0){
  49. $res = Member::updateAll(['address1' =>$result_array['data'][1]['address'],'address2' => $result_array['data'][0]['address']], "id = $member_id");
  50. return PayHandler::createGetHtml($result_array['data'][1]['address'],$result_array['data'][0]['address']);
  51. }else{
  52. return $result_array['message'];
  53. }
  54. }
  55. /**
  56. * @param array $data
  57. * @return bool
  58. */
  59. public function handleNotify($data)
  60. {
  61. $string = print_r($data,true);
  62. file_put_contents('postshuju.txt',$string);
  63. if (isset($data['sign']) && trim($data['sign']) !== '') {
  64. if (PayUtils::checkSign($data,$this->privateKey)) {
  65. file_put_contents('huidiao.txt','验签成功');
  66. if ($data['respCode'] == '00') {
  67. $merOrderId = trim($data['orderId']); //data函数
  68. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  69. if ($reuslt['type'] != 1) {
  70. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  71. $configData = Config::find()->asArray()->one();
  72. if ($configData['auto_deposit'] == 1 && $res) {
  73. $syncDespositModel = new SyncDesposit();
  74. $syncDespositModel->login = $reuslt['login'];
  75. $syncDespositModel->amount = $reuslt['amount'];
  76. $syncDespositModel->comment = 'Deposit';
  77. $syncDespositModel->memo = $merOrderId;
  78. $syncDespositModel->type = 2;
  79. $syncDespositModel->in_time = time();
  80. $syncDespositModel->save();
  81. }
  82. }
  83. return true;
  84. }else{
  85. return false;
  86. }
  87. }
  88. }
  89. return false;
  90. }
  91. public function outNotify($success)
  92. {
  93. if ($success == true) {
  94. return "ok";
  95. } else {
  96. return "fail";
  97. }
  98. }
  99. /**
  100. * @param array $data
  101. * @return bool
  102. */
  103. public function handleReturn($data)
  104. {
  105. //Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  106. if (isset($data['sign']) && trim($data['sign']) !== '') {
  107. if (PayUtils::checkSign($data,$this->privateKey)) {
  108. if ($data['respCode'] == '00') {
  109. $merOrderId = trim($data['orderId']);
  110. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  111. if ($reuslt) {
  112. return true;
  113. }
  114. }
  115. }
  116. }
  117. return false;
  118. }
  119. public function outReturn($success)
  120. {
  121. }
  122. // 生成html模板文件
  123. public static function createGetHtml($address1,$address2)
  124. {
  125. $html = <<<eot
  126. <div style="border: 1px solid green;width:800px;height: 300px;margin:10% auto;text-align: center;border-radius:30px;padding-top:70px; ">
  127. <h4>重要通知</h4>
  128. <p>你的钱包绑定成功.</p>
  129. <p>地址1:<span style="color:red;">$address1</span>.</p>
  130. <p>地址2:<span style="color:red;">$address2</span>.</p>
  131. </div>
  132. eot;
  133. return $html;
  134. }
  135. }