PayHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace common\pay\huanqiu;
  3. use backend\models\Config;
  4. use backend\models\Deposit;
  5. use backend\models\SyncDesposit;
  6. use backend\models\Member;
  7. use backend\helpers\MailHelper;
  8. use common\pay\BasePayHandler;
  9. use Yii;
  10. use yii\helpers\VarDumper;
  11. class PayHandler extends BasePayHandler
  12. {
  13. public $payUrl; //网关地址
  14. public $merId; //mid
  15. public $privateKey; //商户私钥
  16. public $appId; //appid
  17. /**
  18. * @inheritdoc
  19. */
  20. public function init()
  21. {
  22. parent::init();
  23. if ($this->payUrl == null) {
  24. $this->payUrl = Yii::$app->params['huanqiu.payUrl']; //网关地址
  25. }
  26. if ($this->merId == null) {
  27. $this->merId = Yii::$app->params['huanqiu.merId']; //商户号
  28. }
  29. if ($this->appId == null) {
  30. $this->appId = Yii::$app->params['huanqiu.appId']; //商户账号
  31. }
  32. if ($this->privateKey == null) {
  33. $this->privateKey = Yii::$app->params['huanqiu.key']; //商户私钥
  34. }
  35. }
  36. /**
  37. * @param array $deposit
  38. * @param array $params
  39. * @return string
  40. */
  41. public function outPay($deposit, $params = [])
  42. {
  43. $data['p1'] = $deposit['amount']; //订单金额(美元)
  44. $data['p2'] = $this->merId; //商户号
  45. $data['p3'] = $deposit['order_sn']; //订单号码
  46. $data['timestamp'] = time(); //时间戳
  47. $result = PayUtils::sendPost($this->payUrl, $data,$this->appId, $this->privateKey); //入金请求
  48. // 对返回的数据进行转化
  49. $result_array = json_decode($result,true);
  50. return $result_array;
  51. }
  52. /**
  53. * @param array $data
  54. * @return bool
  55. */
  56. public function handleNotify($data)
  57. {
  58. $string = print_r($data,true);
  59. file_put_contents('postshuju.txt',$string);
  60. if (isset($data['sign']) && trim($data['sign']) !== '') {
  61. if (PayUtils::checkSign($data,$this->privateKey)) {
  62. file_put_contents('huidiao.txt','验签成功');
  63. $merOrderId = trim($data['merchantOrderNo']); //商戶的订单号码
  64. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  65. if ($reuslt['type'] != 1) {
  66. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  67. $configData = Config::find()->asArray()->one();
  68. if ($configData['auto_deposit'] == 1 && $res) {
  69. $syncDespositModel = new SyncDesposit();
  70. $syncDespositModel->login = $reuslt['login'];
  71. $syncDespositModel->amount = $reuslt['amount'];
  72. $syncDespositModel->comment = 'Deposit';
  73. $syncDespositModel->memo = $merOrderId;
  74. $syncDespositModel->type = 2;
  75. $syncDespositModel->in_time = time();
  76. $syncDespositModel->save();
  77. }
  78. }
  79. return true;
  80. }else{
  81. return false;
  82. }
  83. }
  84. return false;
  85. }
  86. public function outNotify($success)
  87. {
  88. if ($success == true) {
  89. return json(['code'=>'200']);
  90. } else {
  91. return "fail";
  92. }
  93. }
  94. /**
  95. * @param array $data
  96. * @return bool
  97. */
  98. public function handleReturn($data)
  99. {
  100. if (isset($data['sign']) && trim($data['sign']) !== '') {
  101. if (PayUtils::checkSign($data,$this->privateKey)) {
  102. if ($data['respCode'] == '00') {
  103. $merOrderId = trim($data['orderId']);
  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. }