Refund.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace JiaLeo\Payment\Unionpay;
  3. use JiaLeo\Payment\Common\PaymentException;
  4. class Refund extends BaseUnionpay
  5. {
  6. public $transUrl;
  7. /**
  8. * WebPay constructor.
  9. */
  10. public function __construct($config)
  11. {
  12. parent::__construct($config);
  13. //判断是否测试环境
  14. if ($config['is_test']) {
  15. $this->transUrl = 'https://gateway.test.95516.com/gateway/api/backTransReq.do';
  16. } else {
  17. $this->transUrl = 'https://gateway.95516.com/gateway/api/backTransReq.do';
  18. }
  19. }
  20. /**
  21. * 退款
  22. * @return mixed
  23. * @throws PaymentException
  24. */
  25. public function handle($get_params)
  26. {
  27. //参数配置
  28. $params = [
  29. 'version' => '5.0.0', //版本号
  30. 'encoding' => 'utf-8', //编码方式
  31. 'txnType' => '04', //交易类型
  32. 'txnSubType' => '00', //交易子类
  33. 'bizType' => '000201', //业务类型
  34. 'signMethod' => '01', //签名方法
  35. 'channelType' => '08', //渠道类型,07-PC,08-手机
  36. 'accessType' => '0', //接入类型
  37. 'currencyCode' => '156', //交易币种,境内商户固定156
  38. ];
  39. $params['merId'] = $this->config['mer_id'];
  40. $params['txnTime'] = date('YmdHis');
  41. $params = array_merge($params, $get_params);
  42. //签名
  43. $cert_path = $this->config['private_key_path'];
  44. $cert_pwd = $this->config['private_key_pwd'];
  45. //获取证书key
  46. $certId = Utils\Rsa::getCertId($cert_path, $cert_pwd);
  47. $params['certId'] = $certId;
  48. //签名
  49. $params['signature'] = Utils\Rsa::getParamsSignatureWithRSA($params, $cert_path, $cert_pwd);;
  50. //异步提交---后台通知地址
  51. $result = \JiaLeo\Payment\Common\Curl::post($this->transUrl, $params);
  52. //返回示例
  53. //accessType=0&bizType=000201&encoding=utf-8&merId=777290058143059
  54. //&orderId=149648615778501&origQryId=201706031828290366408&queryId=201706031835570406088
  55. //&respCode=00&respMsg=成功[0000000]&signMethod=01&txnAmt=1&txnSubType=00
  56. //&txnTime=20170603183557&txnType=04&version=5.0.0&certId=68759585097
  57. //&signature=C6m1JFv59rNmB/tiS4Q1YIAcg3Vv+U4odNUkzqlt3+D+AASmtWS4bg1jfJ3EidXxPTF+kMuk1Iy2gGzkgvfbn9BWEqEeYCV8sktusaTfOO6o2Obbz8HLRpu4zkLfWbVBA6aQ7nSrdpRl7RHO9ToFBcDdUcM07vWzGQ79cAELmrgxuvnp966/eNuQFu5jPHj2hFndduWAKM0+EIbIy/n1vcIk6kiGJGsXVcBeNoqsgjPMMMQWF2j3jTAmvQvnx7SWmwgtWE/LYSmNonehvJQv1CFa/fKkiiMIdd8XsMvJv6zbDQumzJz3lQ67mWMRq+YDBEyH+onzwSjO4IWXeJz9ig==
  58. if (!$result) {
  59. throw new PaymentException('请求银联接口错误!');
  60. }
  61. $res_array=$this->strToArray($result);
  62. if (!(is_array($res_array) && isset($res_array['respCode']))) {
  63. throw new PaymentException('请求银联接口错误!');
  64. }
  65. if ($res_array['respCode'] != '00') {
  66. throw new PaymentException('退款失败!原因:' . $res_array['respMsg']);
  67. }
  68. //验签
  69. $res = \JiaLeo\Payment\Unionpay\Utils\Rsa::verify($res_array,$this->config['cert_dir']);
  70. if(!$res){
  71. throw new PaymentException('签名验证失败!');
  72. }
  73. return $res_array;
  74. }
  75. public function strToArray($data)
  76. {
  77. $temp = array();
  78. $arr = explode('&',$data);
  79. foreach($arr as $key => $v){
  80. $str = explode('=',$v);
  81. $temp[$str[0]] = $str[1];
  82. }
  83. return $temp;
  84. }
  85. }