PayHandler.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. file_put_contents('sign.txt',$data['sign']);
  51. $result = static::createHtml($data, $this->payUrl);
  52. return $result;
  53. }
  54. /**
  55. * @param array $data
  56. * @return bool
  57. */
  58. public function handleNotify($data)
  59. {
  60. $data['secret'] = $this->Key;
  61. $tt = print_r($data,true);
  62. file_put_contents('payplat_data.txt',$tt);
  63. if (isset($data['sign']) && trim($data['sign']) !== '') {
  64. if (PayUtils::checkSign($data)) {
  65. file_put_contents('payplat_success.txt','验签成功');
  66. if($data['payStatus'] == '1'){
  67. $merOrderId = trim($data['sdOrderNo']);
  68. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  69. if ($reuslt['type'] != 1) {
  70. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  71. $configData = Config::find()->asArray()->one();
  72. if ($configData['auto_deposit'] == 1 && $res) {
  73. $syncDespositModel = new SyncDesposit();
  74. $syncDespositModel->login = $reuslt['login'];
  75. $syncDespositModel->amount = $reuslt['amount'];
  76. $syncDespositModel->comment = 'Deposit';
  77. $syncDespositModel->memo = $merOrderId; //存储入金表的订单id
  78. $syncDespositModel->type = 2;
  79. $syncDespositModel->in_time = time();
  80. $syncDespositModel->save();
  81. }
  82. return true;
  83. }
  84. }else{
  85. return false;
  86. }
  87. }else{
  88. return false;
  89. }
  90. }
  91. return false;
  92. }
  93. public function outNotify($success)
  94. {
  95. if ($success == true) {
  96. return 'success';
  97. } else {
  98. // 返回的数据格式
  99. return "FAIL";
  100. }
  101. }
  102. /**
  103. * @param array $data
  104. * @return bool
  105. */
  106. public function handleReturn($data)
  107. {
  108. //Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  109. if (isset($data['sign']) && trim($data['sign']) !== '') {
  110. $data['secret'] = $this->Key;
  111. if (PayUtils::checkSign($data)) {
  112. if ($data['payStatus'] == '1') {
  113. $merOrderId = trim($data['sdOrderNo']);
  114. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  115. if ($reuslt) {
  116. return true;
  117. }
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. public function outReturn($success)
  124. {
  125. }
  126. }