PayHandler.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace common\pay\qianwanbao;
  3. use backend\models\Config;
  4. use backend\models\Deposit;
  5. use backend\models\SyncDesposit;
  6. use common\helpers\Utils;
  7. use common\pay\BasePayHandler;
  8. use Yii;
  9. class PayHandler extends BasePayHandler
  10. {
  11. public $payUrl; //支付地址
  12. public $merNo; //商户编码
  13. public $platformNo; //机构编码
  14. public $secretKey; //机构密钥
  15. /**
  16. * @inheritdoc
  17. */
  18. public function init()
  19. {
  20. parent::init();
  21. if ($this->payUrl == null) {
  22. $this->payUrl = Yii::$app->params['qianwanbao.payUrl']; //支付地址
  23. }
  24. if ($this->merNo == null) {
  25. $this->merNo = Yii::$app->params['qianwanbao.merNo']; //商户编码
  26. }
  27. if ($this->platformNo == null) {
  28. $this->platformNo = Yii::$app->params['qianwanbao.platformNo']; //机构编码
  29. }
  30. if ($this->secretKey == null) {
  31. $this->secretKey = Yii::$app->params['qianwanbao.secretKey']; //机构密钥
  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. //接口公告报文
  43. $data['version'] = '1.0.0'; //SDK版本号
  44. $data['tranType'] = '11000'; //网关支付类型
  45. $data['platformNo'] = $this->platformNo; //机构平台编码
  46. $data['merNo'] = $this->merNo; //商户编码
  47. $data['signType'] = 'MD5'; //签名方式:MD5
  48. // 请求应答参数
  49. $data['service'] = 'pay'; //服务类型
  50. $data['orderAmount'] = $deposit['rmb'] * 100; //订单总金额
  51. $data['subject'] = $deposit['order_sn']; //订单标题(目前使用的是订单号)
  52. $data['merOrderNo'] = $deposit['order_sn']; //订单编码
  53. $data['frontUrl'] = $this->returnUrl; //同步跳转地址(网管支付必填)
  54. $data['backUrl'] = $this->notifyUrl; //异步通知地址
  55. $data['signature'] = PayUtils::makeSign($data, $this->secretKey); // 签名
  56. return static::createHtml($data, $this->payUrl);
  57. }
  58. /**
  59. * @param array $data
  60. * @return bool
  61. */
  62. public function handleNotify($data)
  63. {
  64. if (PayUtils::checkSign($data, $this->secretKey)) {
  65. if (isset($data['status']) && $data['status'] == 1) {
  66. $merOrderId = trim($data['merOrderNo']);
  67. /** @var Deposit $model */
  68. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  69. if ($reuslt['type'] != 1) {
  70. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  71. $configData = Config::find()->asArray()->one();
  72. if ($configData['auto_deposit'] == 1 && $res) {
  73. $syncDespositModel = new SyncDesposit();
  74. $syncDespositModel->login = $reuslt['login'];
  75. $syncDespositModel->amount = $reuslt['amount'];
  76. $syncDespositModel->comment = 'Deposit';
  77. $syncDespositModel->memo = $merOrderId;
  78. $syncDespositModel->type = 2;
  79. $syncDespositModel->in_time = time();
  80. $syncDespositModel->save();
  81. }
  82. return true;
  83. }
  84. }
  85. }
  86. return false;
  87. }
  88. public function outNotify($success)
  89. {
  90. if ($success == true) {
  91. return "SUCCESS";
  92. } else {
  93. return "FAIL";
  94. }
  95. }
  96. /**
  97. * @param array $data
  98. * @return bool
  99. */
  100. public function handleReturn($data)
  101. {
  102. if (PayUtils::checkSign($data, $this->secretKey)) {
  103. if (isset($data['status']) && $data['status'] == 1) {
  104. $merOrderId = trim($data['merOrderNo']);
  105. /** @var Deposit $model */
  106. $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
  107. if ($model) {
  108. return true;
  109. }
  110. }
  111. }
  112. return false;
  113. }
  114. public function outReturn($success)
  115. {
  116. }
  117. }