PayHandler.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace common\pay\yilaitwo;
  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['yilaitwo.payUrl'];
  22. }
  23. if ($this->merNo == null) {
  24. $this->merNo = Yii::$app->params['yilaitwo.merNo'];
  25. }
  26. if ($this->secretKey == null) {
  27. $this->secretKey = Yii::$app->params['yilaitwo.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['sign'] = PayUtils::makeSign($data, $this->secretKey); // 签名
  47. return self::createGetHtml($data, $this->payUrl);
  48. }
  49. /**
  50. * @param array $data
  51. * @return bool
  52. */
  53. public function handleNotify($data)
  54. {
  55. if (PayUtils::checkSign($data, $this->secretKey)) {
  56. if ( isset($data['pay_status']) && $data['pay_status'] == "success" ) {
  57. $merOrderId = trim($data['order_num']);
  58. /** @var Deposit $model */
  59. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  60. if ($reuslt['type'] != 1) {
  61. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  62. $configData = Config::find()->asArray()->one();
  63. if ($configData['auto_deposit'] == 1 && $res) {
  64. $syncDespositModel = new SyncDesposit();
  65. $syncDespositModel->login = $reuslt['login'];
  66. $syncDespositModel->amount = $reuslt['amount'];
  67. $syncDespositModel->comment = 'Deposit';
  68. $syncDespositModel->memo = $merOrderId;
  69. $syncDespositModel->type = 2;
  70. $syncDespositModel->in_time = time();
  71. $syncDespositModel->save();
  72. }
  73. }
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. public function outNotify($success)
  80. {
  81. if ($success == true) {
  82. return "success";
  83. } else {
  84. return "FAIL";
  85. }
  86. }
  87. /**
  88. * @param array $data
  89. * @return bool
  90. */
  91. public function handleReturn($data)
  92. {
  93. if (PayUtils::checkSign($data, $this->secretKey)) {
  94. if (isset($data['pay_status']) && $data['pay_status'] == "success") {
  95. $merOrderId = trim($data['order_num']);
  96. /** @var Deposit $model */
  97. $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
  98. if ($model) {
  99. return true;
  100. }
  101. }
  102. }
  103. return false;
  104. }
  105. public function outReturn($success)
  106. {
  107. }
  108. public static function createGetHtml($params, $action)
  109. {
  110. $encodeType = isset ($params ['encoding']) ? $params ['encoding'] : 'UTF-8';
  111. $html = <<<eot
  112. <html>
  113. <head>
  114. <meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />
  115. </head>
  116. <body onload="javascript:document.pay_form.submit();">
  117. <form id="pay_form" name="pay_form" action="{$action}" method="get">
  118. eot;
  119. foreach ($params as $key => $value) {
  120. $html .= " <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";
  121. }
  122. $html .= <<<eot
  123. <!-- <input type="submit" type="hidden">-->
  124. </form>
  125. </body>
  126. </html>
  127. eot;
  128. return $html;
  129. }
  130. }