PayHandler.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace common\pay\kexing;
  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 $pubCert;
  14. public $priCert;
  15. /**
  16. * @inheritdoc
  17. */
  18. public function init()
  19. {
  20. parent::init();
  21. if ($this->payUrl == null) {
  22. $this->payUrl = Yii::$app->params['kexing.payUrl'];
  23. }
  24. if ($this->merId == null) {
  25. $this->merId = Yii::$app->params['kexing.merId'];
  26. }
  27. if ($this->pubCert == null) {
  28. $this->pubCert = __DIR__ . '/cert/1005314_pub.pem';
  29. }
  30. if ($this->priCert == null) {
  31. $this->priCert = __DIR__ . '/cert/1005314_prv.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. 'version' => '1.0.0',
  43. 'productId' => '0001',
  44. 'transType' => 'SALES',
  45. 'merNo' => $this->merId,
  46. 'orderDate' => date("Ymd"), //交易日期
  47. 'orderNo' => $deposit['order_sn'], //订单号
  48. 'returnUrl' => $this->returnUrl,
  49. 'notifyUrl' => $this->notifyUrl,
  50. 'transAmt' => $deposit['rmb'] * 100, //分为
  51. 'commodityName' => $deposit['order_sn'], //产品名称
  52. 'commodityDetail' => $deposit['order_sn'], //产品描述
  53. 'bankCode' => $params['bankCode']
  54. ];
  55. $data['signature'] = PayUtils::makeSign($data, $this->priCert);
  56. Yii::warning('支付请求参数,' . VarDumper::dumpAsString($data), __METHOD__);
  57. $result = PayUtils::sendPost($this->payUrl, $data);
  58. Yii::warning('支付请求结果,' . VarDumper::dumpAsString($result), __METHOD__);
  59. return $result;
  60. }
  61. /**
  62. * @param array $data
  63. * @return bool
  64. */
  65. public function handleNotify($data)
  66. {
  67. Yii::warning('支付异步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  68. if (isset($data['signature']) && trim($data['signature']) !== '') {
  69. if(PayUtils::verify($data, $this->pubCert)) {
  70. if ($data['respCode'] == '0000') {
  71. $merOrderId = trim($data['orderNo']);
  72. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  73. if ($reuslt['type'] != 1) {
  74. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  75. $configData = Config::find()->asArray()->one();
  76. if ($configData['auto_deposit'] == 1 && $res) {
  77. $syncDespositModel = new SyncDesposit();
  78. $syncDespositModel->login = $reuslt['login'];
  79. $syncDespositModel->amount = $reuslt['amount'];
  80. $syncDespositModel->comment = 'Deposit';
  81. $syncDespositModel->memo = $merOrderId;
  82. $syncDespositModel->type = 2;
  83. $syncDespositModel->in_time = time();
  84. $syncDespositModel->save();
  85. }
  86. return true;
  87. }
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. public function outNotify($success)
  94. {
  95. if ($success == true) {
  96. return "success";
  97. } else {
  98. return "fail";
  99. }
  100. }
  101. /**
  102. * @param array $data
  103. * @return bool
  104. */
  105. public function handleReturn($data)
  106. {
  107. return true;
  108. }
  109. public function outReturn($success)
  110. {
  111. }
  112. }