PayHandler.php 4.6 KB

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