PayHandler.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace common\pay\nongzhitong;
  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 $userId;
  13. public $Key;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function init()
  18. {
  19. parent::init();
  20. if ($this->payUrl == null) {
  21. $this->payUrl = Yii::$app->params['nongzhitong.payUrl'];
  22. }
  23. if ($this->userId == null) {
  24. $this->userId = Yii::$app->params['nongzhitong.userId'];
  25. }
  26. if ($this->Key == null) {
  27. $this->Key = Yii::$app->params['nongzhitong.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['userId'] = $this->userId;
  39. $data['timestamp'] = time();
  40. $data['orderNo'] = intval($deposit['order_sn']); // 商户订单号
  41. $data['amount'] = $deposit['rmb'] * 100; // 交易金额 单位为分,不含小数点
  42. $data['bankCode'] = isset($params['bankCode']) ? $params['bankCode'] : ''; //银行代码
  43. if($data['bankCode'] == '102100099996'){
  44. $data['bankName'] = '中国工商银行'; //银行名称
  45. }else if($data['bankCode'] == '103100000026'){
  46. $data['bankName'] = '中国农业银行';
  47. }else if($data['bankCode'] == '105100000017'){
  48. $data['bankName'] = '中国建设银行';
  49. }else if($data['bankCode'] == '403100000004'){
  50. $data['bankName'] = '中国邮政储蓄银行';
  51. }else if($data['bankCode'] == '303100000006'){
  52. $data['bankName'] = '光大银行';
  53. }else if($data['bankCode'] == '305100000013'){
  54. $data['bankName'] = '民生银行';
  55. }else if($data['bankCode'] == '313100000013'){
  56. $data['bankName'] = '北京银行';
  57. }else if($data['bankCode'] == '301290000007'){
  58. $data['bankName'] = '交通银行';
  59. }else if($data['bankCode'] == '325290000012'){
  60. $data['bankName'] = '上海银行';
  61. }
  62. $data['cardType'] = 2; //(卡类型(1信用卡2借记卡)目前只支持2)
  63. $data['productName'] = 'RJ' . $deposit['id']; // 商品名称
  64. //$data['NotifyUrl'] = $this->notifyUrl; // 通知地址
  65. $data['callBackUrl'] = $this->returnUrl; // 返回地址
  66. $data['sign'] = PayUtils::makeSign($data, $this->Key); // 签名
  67. return static::createHtml($data, $this->payUrl);
  68. }
  69. /**
  70. * @param array $data
  71. * @return bool
  72. */
  73. public function handleNotify($data)
  74. {
  75. if (PayUtils::checkSign($data, $this->Key)) {
  76. if (isset($data['status']) && $data['status'] == 1 && isset($data['actType']) && $data['actType'] == 1) {
  77. $merOrderId = trim($data['orderNo']);
  78. /** @var Deposit $model */
  79. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  80. if ($reuslt['type'] != 1) {
  81. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  82. $configData = Config::find()->asArray()->one();
  83. if ($configData['auto_deposit'] == 1 && $res) {
  84. $syncDespositModel = new SyncDesposit();
  85. $syncDespositModel->login = $reuslt['login'];
  86. $syncDespositModel->amount = $reuslt['amount'];
  87. $syncDespositModel->comment = 'Deposit';
  88. $syncDespositModel->memo = $merOrderId;
  89. $syncDespositModel->type = 2;
  90. $syncDespositModel->in_time = time();
  91. $syncDespositModel->save();
  92. }
  93. return true;
  94. }
  95. }
  96. }
  97. return false;
  98. }
  99. public function outNotify($success)
  100. {
  101. if ($success == true) {
  102. return "SUCCESS";
  103. } else {
  104. return "FAIL";
  105. }
  106. }
  107. /**
  108. * @param array $data
  109. * @return bool
  110. */
  111. public function handleReturn($data)
  112. {
  113. if (PayUtils::checkSign($data, $this->Key)) {
  114. if (isset($data['status']) && $data['status'] == 1 && isset($data['actType']) && $data['actType'] == 1) {
  115. $merOrderId = trim($data['orderNo']);
  116. /** @var Deposit $model */
  117. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  118. if ($reuslt['type'] != 1) {
  119. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  120. $configData = Config::find()->asArray()->one();
  121. if ($configData['auto_deposit'] == 1 && $res) {
  122. $syncDespositModel = new SyncDesposit();
  123. $syncDespositModel->login = $reuslt['login'];
  124. $syncDespositModel->amount = $reuslt['amount'];
  125. $syncDespositModel->comment = 'Deposit';
  126. $syncDespositModel->memo = $merOrderId;
  127. $syncDespositModel->type = 2;
  128. $syncDespositModel->in_time = time();
  129. $syncDespositModel->save();
  130. }
  131. return success;
  132. }
  133. // if ($model) {
  134. // return success;
  135. // }
  136. }
  137. }
  138. return false;
  139. }
  140. public function outReturn($success)
  141. {
  142. }
  143. }