PayHandler.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace common\pay\rtw;
  3. use backend\models\Config;
  4. use backend\models\Deposit;
  5. use backend\models\SyncDesposit;
  6. use common\pay\BasePayHandler;
  7. use Yii;
  8. use yii\helpers\VarDumper;
  9. class PayHandler extends BasePayHandler
  10. {
  11. public $payUrl;
  12. public $merId;
  13. public $comkey;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function init()
  18. {
  19. parent::init();
  20. if ($this->payUrl == null) {
  21. $this->payUrl = Yii::$app->params['rtw.payUrl'];
  22. }
  23. if ($this->merId == null) {
  24. $this->merId = Yii::$app->params['rtw.merId'];
  25. }
  26. if ($this->comkey == null) {
  27. $this->comkey = Yii::$app->params['rtw.key'];
  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. $data['merchantNo'] = $this->merId;
  39. $data['merchantOrderNo'] = $deposit['order_sn'];
  40. $data['orderAmount'] = $deposit['rmb']; // 金额
  41. $data['requestIp'] = isset($params['ip']) ? $params['ip'] : Utils::getClientIp();
  42. $data['returnUrl'] = $this->returnUrl; // 支付完成返回地址
  43. $data['notifyUrl'] = $this->notifyUrl; // 回调地址
  44. $data['orderTitle'] = $deposit['order_sn'];
  45. $data['service'] = 'service.business.gate_way';
  46. $data['bankCode'] = $params['bankCode'];
  47. $data['cardType'] = 'DC';
  48. $data['sign'] = PayUtils::makeSign($data, $this->comkey);
  49. Yii::warning('支付请求参数,' . VarDumper::dumpAsString($data), __METHOD__);
  50. $res= PayUtils::sendPost($this->payUrl, $data);
  51. Yii::warning('支付请求结果,' . VarDumper::dumpAsString($tmp), __METHOD__);
  52. $tmp = json_decode($res, true);
  53. if ($tmp['code'] != 1) {
  54. return $res;
  55. }
  56. $result = $tmp['result'];
  57. Yii::warning('支付请求结果,' . VarDumper::dumpAsString($result['payInfo']), __METHOD__);
  58. return $result['payInfo'];
  59. }
  60. /**
  61. * @param array $data
  62. * @return bool
  63. */
  64. public function handleNotify($data)
  65. {
  66. Yii::warning('支付异步通知参数,' . var_export($data, true), __METHOD__);
  67. $res = $data['result'];
  68. $res['orderAmount'] = sprintf('%.2f', $res['orderAmount']);
  69. $notifySign = $res['sign'];
  70. unset($res['sign']);
  71. Yii::warning('支付异步通知参数2222,' . var_export($res, true), __METHOD__);
  72. $sign = PayUtils::makeSign($res, $this->comkey);
  73. if ($sign == $notifySign) {
  74. if ($data['code'] == '1') {
  75. $merOrderId = trim($res['merchantOrderNo']);
  76. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  77. if ($reuslt['type'] != 1 && $res['tradeStatus'] == 'PAY_SUCCESS') {
  78. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  79. $configData = Config::find()->asArray()->one();
  80. if ($configData['auto_deposit'] == 1 && $res) {
  81. $syncDespositModel = new SyncDesposit();
  82. $syncDespositModel->login = $reuslt['login'];
  83. $syncDespositModel->amount = $reuslt['amount'];
  84. $syncDespositModel->comment = 'Deposit';
  85. $syncDespositModel->memo = $merOrderId;
  86. $syncDespositModel->type = 2;
  87. $syncDespositModel->in_time = time();
  88. $syncDespositModel->save();
  89. }
  90. return true;
  91. }
  92. }
  93. }
  94. Yii::warning('支付异步通知签名失败,' . $sign.'----resign:'.$notifySign, __METHOD__);
  95. return false;
  96. }
  97. public function outNotify($success)
  98. {
  99. if ($success == true) {
  100. return "success";
  101. } else {
  102. return "FAIL";
  103. }
  104. }
  105. /**
  106. * @param array $data
  107. * @return bool
  108. */
  109. public function handleReturn($data)
  110. {
  111. Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  112. if (isset($data['sign']) && trim($data['sign']) !== '') {
  113. $signData = [];
  114. foreach (['customerid', 'status', 'sdpayno', 'sdorderno', 'total_fee', 'paytype'] as $field) {
  115. $signData[$field] = isset($data[$field]) ? $data[$field] : null;
  116. }
  117. $sign = PayUtils::makeSign($signData, $this->comkey);
  118. if ($sign == $data['sign']) {
  119. if ($data['status'] == '1') {
  120. $merOrderId = trim($data['sdorderno']);
  121. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  122. if ($reuslt) {
  123. return true;
  124. }
  125. }
  126. }
  127. }
  128. return false;
  129. }
  130. public function outReturn($success)
  131. {
  132. }
  133. }