PayHandler.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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'] = sprintf("%.2f",$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. if (PayUtils::verify($data, $this->publicKey)) {
  62. if ($data['status'] == '1') {
  63. $merOrderId = trim($data['orderNo']);
  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; //存储入金表的订单id
  74. $syncDespositModel->type = 2;
  75. $syncDespositModel->in_time = time();
  76. $syncDespositModel->save();
  77. }
  78. return success;
  79. }
  80. }
  81. }
  82. }
  83. return false;
  84. }
  85. public function outNotify($success)
  86. {
  87. if ($success == true) {
  88. return "success";
  89. } else {
  90. // 返回的数据格式
  91. return "FAIL";
  92. }
  93. }
  94. /**
  95. * @param array $data
  96. * @return bool
  97. */
  98. public function handleReturn($data)
  99. {
  100. //Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  101. if (isset($data['sign']) && trim($data['sign']) !== '') {
  102. if (PayUtils::verify($data, $this->publicKey)) {
  103. if ($data['status'] == '1') {
  104. $merOrderId = trim($data['orderNo']);
  105. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  106. if ($reuslt) {
  107. return success;
  108. }
  109. }
  110. }
  111. }
  112. return false;
  113. }
  114. public function outReturn($success)
  115. {
  116. }
  117. }