PayHandler.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace common\pay\duigong;
  3. use backend\models\Config;
  4. use backend\models\Deposit;
  5. use backend\models\SyncDesposit;
  6. use common\pay\BasePayHandler;
  7. use backend\models\PublicAccount;
  8. use Yii;
  9. use yii\helpers\VarDumper;
  10. class PayHandler extends BasePayHandler
  11. {
  12. public $payUrl;
  13. public $merId;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function init()
  18. {
  19. parent::init();
  20. if ($this->payUrl == null) {
  21. $this->payUrl ='';
  22. }
  23. if ($this->merId == null) {
  24. $this->merId ='';
  25. }
  26. }
  27. /**
  28. * @param array $deposit
  29. * @param array $params
  30. * @return string
  31. */
  32. public function outPay($deposit, $params = [])
  33. {
  34. $money = sprintf("%.2f",$deposit['rmb']);
  35. $PublicAccount = new PublicAccount();
  36. $result = $PublicAccount->getAccountConfig();
  37. if($result['id']){
  38. PublicAccount::updateAll([
  39. 'is_used'=> 1,
  40. ], "id =".$result['id']);
  41. $result_account = $result['account']; // 卡号
  42. $result_name = $result['name']; // 收款姓名
  43. $result_bank = $result['bank']; // 收款开户行
  44. return PayHandler::createGetHtml($money,$result_account,$result_name,$result_bank);
  45. }else{
  46. return '该通道已关闭';
  47. }
  48. }
  49. /**
  50. * @param array $data
  51. * @return bool
  52. */
  53. public function handleNotify($data)
  54. {
  55. // Yii::warning('支付异步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  56. if (isset($data['sign']) && trim($data['sign']) !== '') {
  57. if (PayUtils::verify($data, $this->publicKey)) {
  58. if ($data['status'] == '1') {
  59. $merOrderId = trim($data['orderNo']);
  60. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  61. if ($reuslt['type'] != 1) {
  62. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  63. $configData = Config::find()->asArray()->one();
  64. if ($configData['auto_deposit'] == 1 && $res) {
  65. $syncDespositModel = new SyncDesposit();
  66. $syncDespositModel->login = $reuslt['login'];
  67. $syncDespositModel->amount = $reuslt['amount'];
  68. $syncDespositModel->comment = 'Deposit';
  69. $syncDespositModel->memo = $merOrderId; //存储入金表的订单id
  70. $syncDespositModel->type = 2;
  71. $syncDespositModel->in_time = time();
  72. $syncDespositModel->save();
  73. }
  74. return success;
  75. }
  76. }
  77. }
  78. }
  79. return false;
  80. }
  81. public function outNotify($success)
  82. {
  83. if ($success == true) {
  84. return "success";
  85. } else {
  86. // 返回的数据格式
  87. return "FAIL";
  88. }
  89. }
  90. /**
  91. * @param array $data
  92. * @return bool
  93. */
  94. public function handleReturn($data)
  95. {
  96. //Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  97. if (isset($data['sign']) && trim($data['sign']) !== '') {
  98. if (PayUtils::verify($data, $this->publicKey)) {
  99. if ($data['status'] == '1') {
  100. $merOrderId = trim($data['orderNo']);
  101. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  102. if ($reuslt) {
  103. return success;
  104. }
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. public function outReturn($success)
  111. {
  112. }
  113. // 生成html模板文件
  114. public static function createGetHtml($money,$result_account,$result_name,$result_bank)
  115. {
  116. $html = <<<eot
  117. <div style="border: 1px solid green;width:500px;height: 200px;margin:10% auto;text-align:left;border-radius:30px;padding:70px; ">
  118. <h4 style="text-align: center;">请转账到如下账户:</h4>
  119. <p>订单金额为:<span style="color:red;">{$money}元</span></p>
  120. <p>收款账号为:{$result_account}</p>
  121. <p>收款姓名为:$result_name</p>
  122. <p>收款开户行为:{$result_bank}</p>
  123. </div>
  124. eot;
  125. return $html;
  126. }
  127. }