PayHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace common\pay\huiying;
  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 $comkey;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function init()
  18. {
  19. parent::init();
  20. if ($this->payUrl == null) {
  21. $this->payUrl = Yii::$app->params['huiying.payUrl'];
  22. }
  23. if ($this->merId == null) {
  24. $this->merId = Yii::$app->params['huiying.merId'];
  25. }
  26. if ($this->comkey == null) {
  27. $this->comkey = Yii::$app->params['huiying.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['p0_Cmd'] = 'Buy';
  38. $data['p1_MerId'] = $this->merId;
  39. $data['p2_Order'] = $deposit['order_sn'];
  40. $data['p3_Amt'] = $deposit['rmb'];
  41. $data['p4_Cur'] = "CNY";
  42. $data['p5_Pid'] = $deposit['order_sn'];
  43. $data['p6_Pcat'] = $deposit['order_sn'];
  44. $data['p7_Pdesc'] = $deposit['order_sn'];
  45. $data['p8_Url'] = 'http://pay.sure-stock.com/pay/notify/9';
  46. $data['p9_SAF'] = "0";
  47. $data['pa_MP'] = $deposit['order_sn'];
  48. $data['pd_FrpId'] = $params['bankCode'];
  49. $data['pr_NeedResponse'] = "1";
  50. Yii::warning('支付请求参数,' . VarDumper::dumpAsString($data), __METHOD__);
  51. #调用签名函数生成签名串
  52. $hmac = PayUtils::getReqHmacString($data, $this->merId, $this->comkey);
  53. $data['hmac'] = $hmac;
  54. $result = static::createHtml($data, $this->payUrl);
  55. Yii::warning('支付请求结果,' . VarDumper::dumpAsString($result), __METHOD__);
  56. return $result;
  57. }
  58. /**
  59. * @param array $data
  60. * @return bool
  61. */
  62. public function handleNotify($data)
  63. {
  64. Yii::warning('支付异步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  65. # 解析返回参数.
  66. $bRet = PayUtils::CheckHmac($data, $this->merId, $this->comkey);
  67. # 校验码正确.
  68. if($bRet){
  69. if($data['r1_Code'] == "1" && $data['r9_BType'] == "2"){
  70. $merOrderId = trim($data['r6_Order']);
  71. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  72. if ($reuslt['type'] != 1) {
  73. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  74. $configData = Config::find()->asArray()->one();
  75. if ($configData['auto_deposit'] == 1 && $res) {
  76. $syncDespositModel = new SyncDesposit();
  77. $syncDespositModel->login = $reuslt['login'];
  78. $syncDespositModel->amount = $reuslt['amount'];
  79. $syncDespositModel->comment = 'Deposit';
  80. $syncDespositModel->memo = $merOrderId;
  81. $syncDespositModel->type = 2;
  82. $syncDespositModel->in_time = time();
  83. $syncDespositModel->save();
  84. }
  85. return true;
  86. }
  87. }
  88. }
  89. return false;
  90. }
  91. public function outNotify($success)
  92. {
  93. if ($success == true) {
  94. return "success";
  95. } else {
  96. return "fail";
  97. }
  98. }
  99. /**
  100. * @param array $data
  101. * @return bool
  102. */
  103. public function handleReturn($data)
  104. {
  105. return $this->handleNotify($data);
  106. }
  107. public function outReturn($success)
  108. {
  109. }
  110. }