PayHandler.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace common\pay\globalpay;
  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['globalpay.payUrl'];
  23. }
  24. if ($this->confirmUrl == null) {
  25. $this->confirmUrl = Yii::$app->params['globalpay.confirmUrl'];
  26. }
  27. if ($this->merId == null) {
  28. $this->merId = Yii::$app->params['globalpay.merId'];
  29. }
  30. if ($this->Key == null) {
  31. $this->Key = Yii::$app->params['globalpay.Key'];
  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['mchNo'] = $this->merId; //商户号码
  43. $data['customerNo'] = $params['login']; //商户平台用户唯一标识
  44. $data['orderNo'] = $deposit['order_sn']; //订单号码
  45. $data['amount'] = $deposit['rmb'] * 100; //订单金额
  46. $data['currency'] = 'CNY'; //币种
  47. $data['returnUrl'] = $this->returnUrl; //支付完成返回地址(这个是必须有的)
  48. $data['notifyUrl'] = $this->notifyUrl; //异步回调地址
  49. $data['payType'] = 1; //支付类型
  50. $data['signType'] = 'MD5'; //签名类型
  51. $data['sign'] = PayUtils::makeSign($data,$this->Key);
  52. $result = PayUtils::send($data, $this->payUrl);
  53. if (isset($result["code"]) && $result["code"] == 200) {
  54. $order_id = $result["data"]["orderId"];
  55. $result['pay_url'] = $this->confirmUrl.$order_id;
  56. return $result;
  57. }else{
  58. return $result;
  59. }
  60. }
  61. /**
  62. * @param array $data
  63. * @return bool
  64. */
  65. public function handleNotify($data)
  66. {
  67. $tt = print_r($data,true);
  68. file_put_contents('globalpay_data.txt',$tt);
  69. if (isset($data['sign']) && trim($data['sign']) !== '') {
  70. if (PayUtils::verify($data, $this->Key)) {
  71. file_put_contents('globalpay_success.txt','验签成功');
  72. $merOrderId = trim($data['orderNo']);
  73. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  74. if ($reuslt['type'] != 1) {
  75. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  76. $configData = Config::find()->asArray()->one();
  77. if ($configData['auto_deposit'] == 1 && $res) {
  78. $syncDespositModel = new SyncDesposit();
  79. $syncDespositModel->login = $reuslt['login'];
  80. $syncDespositModel->amount = $reuslt['amount'];
  81. $syncDespositModel->comment = 'Deposit';
  82. $syncDespositModel->memo = $merOrderId; //存储入金表的订单id
  83. $syncDespositModel->type = 2;
  84. $syncDespositModel->in_time = time();
  85. $syncDespositModel->save();
  86. }
  87. return success;
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. public function outNotify($success)
  94. {
  95. if ($success == true) {
  96. return '{"code":200}';
  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. if (PayUtils::verify($data, $this->publicKey)) {
  111. if ($data['status'] == '1') {
  112. $merOrderId = trim($data['orderNo']);
  113. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  114. if ($reuslt) {
  115. return success;
  116. }
  117. }
  118. }
  119. }
  120. return false;
  121. }
  122. public function outReturn($success)
  123. {
  124. }
  125. }