PayHandler.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace common\pay\payplat;
  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 $Key;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function init()
  18. {
  19. parent::init();
  20. if ($this->payUrl == null) {
  21. $this->payUrl = Yii::$app->params['payplat.payUrl'];
  22. }
  23. if ($this->merId == null) {
  24. $this->merId = Yii::$app->params['payplat.merId'];
  25. }
  26. if ($this->Key == null) {
  27. $this->Key = Yii::$app->params['payplat.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['userNo'] = $this->merId; //商户号码
  39. if($params['bankCode']=='zhifubao'){
  40. $data['payType'] = 'alipay_bb';
  41. }else{
  42. $data['payType'] = 'wy_bb'; //商户平台用户唯一标识
  43. }
  44. $data['price'] = $deposit['rmb']; //订单金额
  45. $data['sdOrderNo'] = $deposit['order_sn']; //商户订单号码
  46. $data['sdReturnUrl'] = $this->returnUrl; //支付完成返回地址(同步地址)
  47. $data['sdNotifyUrl'] = $this->notifyUrl; //异步回调地址
  48. $data['secret'] = $this->Key; //签名的秘钥
  49. $data['sign'] = PayUtils::makeSign($data);
  50. $result = static::createHtml($data, $this->payUrl);
  51. return $result;
  52. }
  53. /**
  54. * @param array $data
  55. * @return bool
  56. */
  57. public function handleNotify($data)
  58. {
  59. $data['secret'] = $this->Key;
  60. $tt = print_r($data,true);
  61. file_put_contents('payplat_data.txt',$tt);
  62. if (isset($data['sign']) && trim($data['sign']) !== '') {
  63. if (PayUtils::checkSign($data)) {
  64. file_put_contents('payplat_success.txt','验签成功');
  65. if($data['payStatus'] == '1'){
  66. $merOrderId = trim($data['orderNo']);
  67. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  68. if ($reuslt['type'] != 1) {
  69. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  70. $configData = Config::find()->asArray()->one();
  71. if ($configData['auto_deposit'] == 1 && $res) {
  72. $syncDespositModel = new SyncDesposit();
  73. $syncDespositModel->login = $reuslt['login'];
  74. $syncDespositModel->amount = $reuslt['amount'];
  75. $syncDespositModel->comment = 'Deposit';
  76. $syncDespositModel->memo = $merOrderId; //存储入金表的订单id
  77. $syncDespositModel->type = 2;
  78. $syncDespositModel->in_time = time();
  79. $syncDespositModel->save();
  80. }
  81. return true;
  82. }
  83. }else{
  84. return false;
  85. }
  86. }else{
  87. return false;
  88. }
  89. }
  90. return false;
  91. }
  92. public function outNotify($success)
  93. {
  94. if ($success == true) {
  95. return 'success';
  96. } else {
  97. // 返回的数据格式
  98. return "FAIL";
  99. }
  100. }
  101. /**
  102. * @param array $data
  103. * @return bool
  104. */
  105. public function handleReturn($data)
  106. {
  107. //Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  108. if (isset($data['sign']) && trim($data['sign']) !== '') {
  109. $data['secret'] = $this->Key;
  110. if (PayUtils::checkSign($data)) {
  111. if ($data['payStatus'] == '1') {
  112. $merOrderId = trim($data['orderNo']);
  113. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  114. if ($reuslt) {
  115. return true;
  116. }
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. public function outReturn($success)
  123. {
  124. }
  125. }