PayHandler.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 $confirmUrl;
  13. public $merId;
  14. public $Key;
  15. /**
  16. * @inheritdoc
  17. */
  18. public function init()
  19. {
  20. parent::init();
  21. if ($this->payUrl == null) {
  22. $this->payUrl = Yii::$app->params['payplat.payUrl'];
  23. }
  24. if ($this->merId == null) {
  25. $this->merId = Yii::$app->params['payplat.merId'];
  26. }
  27. if ($this->Key == null) {
  28. $this->Key = Yii::$app->params['payplat.Key'];
  29. }
  30. }
  31. /**
  32. * @param array $deposit
  33. * @param array $params
  34. * @return string
  35. */
  36. public function outPay($deposit, $params = [])
  37. {
  38. $data = [];
  39. $data['userNo'] = $this->merId; //商户号码
  40. $data['payType'] = 'wypay'; //商户平台用户唯一标识
  41. $data['price'] = $deposit['rmb'] * 100; //订单金额
  42. $data['sdOrderNo'] = $deposit['order_sn']; //商户订单号码
  43. $data['sdReturnUrl'] = $this->returnUrl; //支付完成返回地址(同步地址)
  44. $data['sdNotifyUrl'] = $this->notifyUrl; //异步回调地址
  45. $data['secret'] = $this->Key; //签名的秘钥
  46. $data['sign'] = PayUtils::makeSign($data);
  47. $result = static::createHtml($data, $this->payUrl);
  48. return $result;
  49. }
  50. /**
  51. * @param array $data
  52. * @return bool
  53. */
  54. public function handleNotify($data)
  55. {
  56. $tt = print_r($data,true);
  57. file_put_contents('payplat_data.txt',$tt);
  58. $data['secret'] = $this->Key;
  59. if (isset($data['sign']) && trim($data['sign']) !== '') {
  60. if (PayUtils::verify($data)) {
  61. file_put_contents('payplat_success.txt','验签成功');
  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. }
  80. }
  81. return false;
  82. }
  83. public function outNotify($success)
  84. {
  85. if ($success == true) {
  86. return success;
  87. } else {
  88. // 返回的数据格式
  89. return "FAIL";
  90. }
  91. }
  92. /**
  93. * @param array $data
  94. * @return bool
  95. */
  96. public function handleReturn($data)
  97. {
  98. //Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  99. if (isset($data['sign']) && trim($data['sign']) !== '') {
  100. $data['secret'] = $this->Key;
  101. if (PayUtils::verify($data)) {
  102. if ($data['payStatus'] == '1') {
  103. $merOrderId = trim($data['orderNo']);
  104. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  105. if ($reuslt) {
  106. return true;
  107. }
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113. public function outReturn($success)
  114. {
  115. }
  116. }