PayHandler.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace common\pay\wanxin;
  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 $merId; //商户编码
  13. public $secretKey; //机构密钥
  14. /**
  15. * @inheritdoc
  16. */
  17. public function init()
  18. {
  19. parent::init();
  20. if ($this->payUrl == null) {
  21. $this->payUrl = Yii::$app->params['wanxin.payUrl']; //支付地址
  22. }
  23. if ($this->merId == null) {
  24. $this->merId = Yii::$app->params['wanxin.merId']; //商户编码
  25. }
  26. if ($this->secretKey == null) {
  27. $this->secretKey = Yii::$app->params['wanxin.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 = [];
  38. //请求参数的组装
  39. $data['p0_Cmd'] = 'Buy'; //业务类型
  40. $data['p1_MerId'] = $this->merId; //商户编码
  41. $data['p2_Order'] = $deposit['order_sn']; //商户订单号
  42. $data['p3_Amt'] = sprintf("%.2f",$deposit['rmb']); //订单总金额
  43. $data['p4_Cur'] = 'CNY'; //交易币种
  44. $data['p8_Url'] = $this->notifyUrl; //异步通知地址
  45. $data['pd_FrpId'] = 'gateway'; //通道编码(网关支付)
  46. $data['pr_NeedResponse'] = '1'; //应答机制(固定值为1)
  47. $data['hmac'] = PayUtils::makeSign($data, $this->secretKey); // 签名
  48. return static::createHtml($data, $this->payUrl);
  49. }
  50. /**
  51. * @param array $data
  52. * @return bool
  53. */
  54. public function handleNotify($data)
  55. {
  56. if (PayUtils::checkSign($data, $this->secretKey)) {
  57. if (isset($data['r1_Code']) && $data['r1_Code'] == 1) {
  58. $merOrderId = trim($data['r6_Order']);
  59. /** @var Deposit $model */
  60. $reuslt = Deposit::find()->where(['order_sn' => $merOrderId])->asArray()->limit(1)->one();
  61. if ($reuslt['type'] != 1) {
  62. $res = Deposit::updateAll(['type' => 1], "order_sn = $merOrderId");
  63. $configData = Config::find()->asArray()->one();
  64. if ($configData['auto_deposit'] == 1 && $res) {
  65. $syncDespositModel = new SyncDesposit();
  66. $syncDespositModel->login = $reuslt['login'];
  67. $syncDespositModel->amount = $reuslt['amount'];
  68. $syncDespositModel->comment = 'Deposit';
  69. $syncDespositModel->memo = $merOrderId;
  70. $syncDespositModel->type = 2;
  71. $syncDespositModel->in_time = time();
  72. $syncDespositModel->save();
  73. }
  74. return true;
  75. }
  76. }
  77. }
  78. return false;
  79. }
  80. public function outNotify($success)
  81. {
  82. if ($success == true) {
  83. return "SUCCESS";
  84. } else {
  85. return "FAIL";
  86. }
  87. }
  88. /**
  89. * @param array $data
  90. * @return bool
  91. */
  92. public function handleReturn($data)
  93. {
  94. if (PayUtils::checkSign( $data, $this->secretKey)) {
  95. if (isset($data['r1_Code']) && $data['r1_Code'] == 1) {
  96. $merOrderId = trim($data['r6_Order']);
  97. /** @var Deposit $model */
  98. $model = Deposit::find()->where(['id' => $merOrderId])->asArray()->limit(1)->one();
  99. if ($model) {
  100. return true;
  101. }
  102. }
  103. }
  104. return false;
  105. }
  106. public function outReturn($success)
  107. {
  108. }
  109. }