RemitHandler.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace common\pay\kexing;
  3. use backend\models\RemitOrder;
  4. use common\helpers\Utils;
  5. use common\pay\BaseRemitHandler;
  6. use Yii;
  7. use yii\helpers\VarDumper;
  8. use yii\httpclient\Client;
  9. class RemitHandler extends BaseRemitHandler
  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 $remitOrder
  36. * @param array $params
  37. * @return array
  38. */
  39. public function outRemit($remitOrder, $params = [])
  40. {
  41. $data = [
  42. 'version' => '1.0.0',
  43. 'productId' => '8001',
  44. 'transType' => 'PROXY_PAY',
  45. 'merNo' => $this->merId,
  46. 'orderDate' => date("Ymd"), //交易日期
  47. 'orderNo' => $remitOrder['remit_no'], //订单号
  48. 'notifyUrl' => $this->notifyUrl,
  49. 'transAmt' => $remitOrder['amount'] * 100, //分为
  50. 'commodityName' => $remitOrder['remit_no'], //产品名称
  51. //'phoneNo' => '',
  52. 'cardNo' => $remitOrder['bank_card_no'], // 银行账号
  53. 'cardName' => $remitOrder['payee_name'],
  54. //'cerdId' => '',
  55. ];
  56. $data['signature'] = PayUtils::makeSign($data, $this->priCert);
  57. Yii::warning('退款请求参数,' . VarDumper::dumpAsString($data), __METHOD__);
  58. $respon = PayUtils::sendPost($this->payUrl, $data);
  59. Yii::warning('退款返回结果,' . VarDumper::dumpAsString($respon), __METHOD__);
  60. $result = [];
  61. $temp = json_decode($respon,true);
  62. Yii::warning('退款返回参数1,' . VarDumper::dumpAsString($temp), __METHOD__);
  63. if ($temp['respCode'] == 'P000') {
  64. $result['pay_status'] = RemitOrder::STATUS_PAY_OUTER_PROCESS; // 外部打款中
  65. }
  66. $result['remit_no'] = trim($temp['orderNo']); // 打款单号
  67. $result['ret_code'] = trim($temp['respCode']); // 返回状态码
  68. $result['ret_msg'] = $temp['respDesc']; // 返回信息描述
  69. Yii::warning('退款返回参数2,' . VarDumper::dumpAsString($result), __METHOD__);
  70. return $result;
  71. }
  72. /**
  73. * @param array $data
  74. * @return bool
  75. */
  76. public function handleNotify($data)
  77. {
  78. Yii::warning('退款异步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  79. if (PayUtils::verify($data, $this->pubCert)) {
  80. if (isset($data['respCode']) && $data['respCode'] == '0000' ) {
  81. $merOrderId = trim($data['orderNo']);
  82. /** @var RemitOrder $model */
  83. $model = RemitOrder::findByRemitNo($merOrderId);
  84. if ($model) {
  85. $model->type = 1;
  86. $model->save(false);
  87. return true;
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. public function outNotify($success)
  94. {
  95. if ($success == true) {
  96. return "success";
  97. } else {
  98. return "fails";
  99. }
  100. }
  101. }