PayHandler.php 4.4 KB

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