RemitHandler.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace common\pay\aotu;
  3. use backend\models\RemitOrder;
  4. use common\helpers\Utils;
  5. use common\pay\BaseRemitHandler;
  6. use Yii;
  7. use yii\httpclient\Client;
  8. class RemitHandler extends BaseRemitHandler
  9. {
  10. public $payUrl;
  11. public $MerchantID;
  12. public $secretKey;
  13. public $remitKey;
  14. /**
  15. * @inheritdoc
  16. */
  17. public function init()
  18. {
  19. parent::init();
  20. if ($this->payUrl == null) {
  21. $this->payUrl = Yii::$app->params['aotu.payUrl'];
  22. }
  23. if ($this->MerchantID == null) {
  24. $this->MerchantID = Yii::$app->params['aotu.MerchantID'];
  25. }
  26. if ($this->secretKey == null) {
  27. $this->secretKey = Yii::$app->params['aotu.secretKey'];
  28. }
  29. if ($this->remitKey == null) {
  30. $this->remitKey = Yii::$app->params['aotu.remitKey'];
  31. }
  32. }
  33. /**
  34. * @param array $remitOrder
  35. * @param array $params
  36. * @return array
  37. */
  38. public function outRemit($remitOrder, $params = [])
  39. {
  40. $data = [];
  41. $data['TradeType'] = 'bis.pay.payment'; // 交易类型
  42. $data['OperatorID'] = $this->MerchantID; // 操作员 默认商户编号
  43. $data['PayType'] = '0101'; // 支付类型 0101-单笔实时代付
  44. $data['TerminalType'] = 'PC'; // 终端类型
  45. $data['PayPWD'] = strtoupper(md5($params['password'] . $this->remitKey)); // 支付密码 (支付密码+工作密钥)MD5加密,商户必填
  46. $data['MerchantID'] = $this->MerchantID; // 商户编号
  47. // $data['BankID'] = $remitOrder['bank_code']; // 银行代码 存折必须填写
  48. $data['OrderID'] = $remitOrder['remit_no']; // 订单号
  49. $data['BankAccountType'] = '00'; // 银行账户类型 0银行卡,01存折,02信用卡。不填默认为银行卡00。
  50. $data['BankAccountNo'] = $remitOrder['bank_card_no']; // 银行账号
  51. $data['BankAccountName'] = $remitOrder['payee_name']; // 银行账户名称
  52. $data['Province'] = $remitOrder['bank_province']; // 开户行所在省
  53. $data['City'] = $remitOrder['bank_city']; // 开户行所在市 如北京市,不能填北京
  54. $data['BankName'] = $remitOrder['bank_name']; // 开户行名称
  55. $data['AccountProp'] = ($remitOrder['is_business'] == 1) ? '1' : '0'; // 账号属性 0-对私 1-对公
  56. // $data['CardType'] = ''; // 开户证件类型
  57. // $data['CardNum'] = ''; // 证件号码
  58. // $data['Tel'] = ''; // 手机号码(预留在银行的)
  59. if ($remitOrder['is_business'] == 1) {
  60. $data['BranchBankID'] = $remitOrder['bank_branch_code']; // 支行号 对公户必填
  61. }
  62. $data['DeviceID'] = ''; // 设备号
  63. $data['MachineIP'] = isset($params['ip']) ? $params['ip'] : Utils::getClientIp(); // 终端IP 订单生成的机器IP
  64. $data['NotifyUrl'] = $this->notifyUrl; // 通知地址
  65. $data['SubmitTime'] = date('YmdHis'); // 订单提交时间
  66. $data['Amt'] = $remitOrder['amount'] * 100; // 交易金额 单位为分,不含小数点
  67. // $data['Summary'] = ''; // 交易附言
  68. $data['Sign'] = PayUtils::makeSign($data, $this->secretKey); // 签名,详见数据签名说明
  69. $client = new Client();
  70. $response = $client->post($this->payUrl, $data)->send();
  71. Yii::warning("outRemit result[{$response->getStatusCode()}]: {$response->getContent()}", __METHOD__);
  72. $result = [];
  73. if ($response->getIsOk()) {
  74. $str = $response->getContent();
  75. parse_str($str, $arr);
  76. if (isset($arr['RetStatus']) && $arr['RetStatus'] == 0) {
  77. $result['pay_status'] = RemitOrder::STATUS_PAY_OUTER_PROCESS; // 外部打款中
  78. }
  79. $retMsg = '';
  80. isset($arr['OrderID']) && $result['remit_no'] = trim($arr['OrderID']); // 打款单号
  81. isset($arr['RetStatus']) && $result['ret_code'] = trim($arr['RetStatus']); // 返回状态码
  82. isset($arr['RetMsg']) && $retMsg .= trim($arr['RetMsg']) . ','; // 返回信息描述
  83. isset($arr['RetDesc']) && $retMsg .= trim($arr['RetDesc']) . ','; // 返回信息描述
  84. isset($arr['RetCode']) && $result['res_code'] = trim($arr['RetCode']); // 交易状态码
  85. $result['ret_msg'] = trim($retMsg, ',');
  86. }
  87. return $result;
  88. }
  89. /**
  90. * @param array $data
  91. * @return bool
  92. */
  93. public function handleNotify($data)
  94. {
  95. if (PayUtils::checkSign($data, $this->secretKey)) {
  96. if (isset($data['RetStatus']) && $data['RetStatus'] == 0 && isset($data['DealStatus']) && $data['DealStatus'] == 0
  97. && isset($data['Status']) && $data['Status'] == '02'
  98. ) {
  99. $merOrderId = trim($data['OrderID']);
  100. /** @var RemitOrder $model */
  101. $model = RemitOrder::findByRemitNo($merOrderId);
  102. if ($model) {
  103. // $model->type = 1;
  104. // $model->save(false);
  105. return true;
  106. }
  107. }
  108. }
  109. return false;
  110. }
  111. public function outNotify($success)
  112. {
  113. if ($success == true) {
  114. return "SUCCESS";
  115. } else {
  116. return "FAIL";
  117. }
  118. }
  119. }