PayHandler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace common\pay\chuanghui;
  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 $privateKey;
  14. public $publicKey;
  15. /**
  16. * @inheritdoc
  17. */
  18. public function init()
  19. {
  20. parent::init();
  21. if ($this->payUrl == null) {
  22. $this->payUrl = Yii::$app->params['chuanghui.payUrl'];
  23. }
  24. if ($this->merId == null) {
  25. $this->merId = Yii::$app->params['chuanghui.merId'];
  26. }
  27. if ($this->privateKey == null) {
  28. $this->privateKey = __DIR__ . '/prem/chuanghui_private.pem';
  29. }
  30. if ($this->publicKey == null) {
  31. $this->publicKey = __DIR__ . '/prem/chuanghui_public.pem';
  32. }
  33. }
  34. /**
  35. * @param array $deposit
  36. * @param array $params
  37. * @return string
  38. */
  39. public function outPay($deposit, $params = [])
  40. {
  41. $data = [];
  42. $data['merId'] = $this->merId; //商户号
  43. $data['orderNo'] = $deposit['order_sn']; //订单号
  44. $data['amount'] = $deposit['rmb']; //金额
  45. $data['payType'] = $params['bankCode']; //支付类型
  46. $data['goodsName'] = $deposit['order_sn']; //商品名称
  47. $data['returnUrl'] = $this->returnUrl; //支付完成返回地址
  48. $data['notifyUrl'] = $this->notifyUrl; //回调地址
  49. $data['sign'] = PayUtils::makeSign($data, $this->privateKey);
  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. // Yii::warning('支付异步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  60. if (isset($data['sign']) && trim($data['sign']) !== '') {
  61. $tt = print_r($data,true);
  62. file_put_contents('1023.txt',$tt);
  63. if (PayUtils::verify($data, $this->publicKey)) {
  64. if ($data['status'] == '1') {
  65. file_put_contents('1024.txt','回调成功');
  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 success;
  82. }
  83. }
  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. if (PayUtils::verify($data, $this->publicKey)) {
  106. if ($data['status'] == '1') {
  107. $merOrderId = trim($data['orderNo']);
  108. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  109. if ($reuslt) {
  110. return success;
  111. }
  112. }
  113. }
  114. }
  115. return false;
  116. }
  117. public function outReturn($success)
  118. {
  119. }
  120. }