PayHandler.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace common\pay\yilai;
  3. use backend\models\Config;
  4. use backend\models\Deposit;
  5. use backend\models\SyncDesposit;
  6. use common\helpers\Utils;
  7. use common\pay\BasePayHandler;
  8. use Yii;
  9. class PayHandler extends BasePayHandler
  10. {
  11. public $payUrl;
  12. public $merNo;
  13. public $secretKey;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function init()
  18. {
  19. parent::init();
  20. if ($this->payUrl == null) {
  21. $this->payUrl = Yii::$app->params['yilai.payUrl'];
  22. }
  23. if ($this->merNo == null) {
  24. $this->merNo = Yii::$app->params['yilai.merNo'];
  25. }
  26. if ($this->secretKey == null) {
  27. $this->secretKey = Yii::$app->params['yilai.secretKey'];
  28. }
  29. }
  30. /**
  31. * @param array $deposit
  32. * @param array $params
  33. * @return string
  34. */
  35. public function outPay($deposit, $params = [])
  36. {
  37. $data = [];
  38. //提交的参数
  39. $data['mch_id'] = $this->merNo; //商户号码
  40. $data['order_num'] = $deposit['order_sn']; //订单号码
  41. $data['pay_amount'] = $deposit['rmb'] * 100; //订单金额
  42. $data['notify_url'] = $this->notifyUrl; //异步地址
  43. $data['return_url'] = $this->returnUrl; //同步地址,留空即可
  44. $data['ext'] = $deposit['order_sn']; //备注信息
  45. $data['pay_type'] = 'pay_wyzf'; //网银支付
  46. $data['bank_code'] = $params['bankCode']; //网银支付(额外参数:什么银行)
  47. $data['sign'] = PayUtils::makeSign($data, $this->secretKey); // 签名
  48. return self::createGetHtml($data, $this->payUrl);
  49. }
  50. /**
  51. * @param array $data
  52. * @return bool
  53. */
  54. public function handleNotify($data)
  55. {
  56. if (PayUtils::checkSign($data, $this->secretKey)) {
  57. if ( isset($data['pay_status']) && $data['pay_status'] == "success" ) {
  58. $merOrderId = trim($data['order_num']);
  59. /** @var Deposit $model */
  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;
  70. $syncDespositModel->type = 2;
  71. $syncDespositModel->in_time = time();
  72. $syncDespositModel->save();
  73. }
  74. }
  75. return true;
  76. }
  77. }
  78. return false;
  79. }
  80. public function outNotify($success)
  81. {
  82. if ($success == true) {
  83. return "success";
  84. } else {
  85. return "FAIL";
  86. }
  87. }
  88. /**
  89. * @param array $data
  90. * @return bool
  91. */
  92. public function handleReturn($data)
  93. {
  94. if (PayUtils::checkSign($data, $this->secretKey)) {
  95. if (isset($data['pay_status']) && $data['pay_status'] == "success") {
  96. $merOrderId = trim($data['order_num']);
  97. /** @var Deposit $model */
  98. $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
  99. if ($model) {
  100. return true;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. public function outReturn($success)
  107. {
  108. }
  109. public static function createGetHtml($params, $action)
  110. {
  111. $encodeType = isset ($params ['encoding']) ? $params ['encoding'] : 'UTF-8';
  112. $html = <<<eot
  113. <html>
  114. <head>
  115. <meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />
  116. </head>
  117. <body onload="javascript:document.pay_form.submit();">
  118. <form id="pay_form" name="pay_form" action="{$action}" method="get">
  119. eot;
  120. foreach ($params as $key => $value) {
  121. $html .= " <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
  122. }
  123. $html .= <<<eot
  124. <!-- <input type="submit" type="hidden">-->
  125. </form>
  126. </body>
  127. </html>
  128. eot;
  129. return $html;
  130. }
  131. }