Refund.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2020 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://think.ctolog.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/WeChatDeveloper
  12. // +----------------------------------------------------------------------
  13. namespace WePayV3;
  14. use WeChat\Contracts\Tools;
  15. use WeChat\Exceptions\InvalidDecryptException;
  16. use WeChat\Exceptions\InvalidResponseException;
  17. use WePayV3\Contracts\BasicWePay;
  18. /**
  19. * 订单退款接口
  20. * Class Refund
  21. * @package WePayV3
  22. */
  23. class Refund extends BasicWePay
  24. {
  25. /**
  26. * 创建退款订单
  27. * @param array $data 退款参数
  28. * @return array
  29. * @throws InvalidResponseException
  30. */
  31. public function create($data)
  32. {
  33. return $this->doRequest('POST', '/v3/ecommerce/refunds/apply', json_encode($data, JSON_UNESCAPED_UNICODE), true);
  34. }
  35. /**
  36. * 退款订单查询
  37. * @param string $refundNo 退款单号
  38. * @return array
  39. * @throws InvalidResponseException
  40. */
  41. public function query($refundNo)
  42. {
  43. $pathinfo = "/v3/ecommerce/refunds/out-refund-no/{$refundNo}";
  44. return $this->doRequest('GET', "{$pathinfo}?sub_mchid={$this->config['mch_id']}", '', true);
  45. }
  46. /**
  47. * 获取退款通知
  48. * @return array
  49. * @throws InvalidDecryptException
  50. * @throws InvalidResponseException
  51. */
  52. public function notify()
  53. {
  54. $data = Tools::xml2arr(file_get_contents("php://input"));
  55. if (!isset($data['return_code']) || $data['return_code'] !== 'SUCCESS') {
  56. throw new InvalidResponseException('获取退款通知XML失败!');
  57. }
  58. try {
  59. $key = md5($this->config['mch_v3_key']);
  60. $decrypt = base64_decode($data['req_info']);
  61. $response = openssl_decrypt($decrypt, 'aes-256-ecb', $key, OPENSSL_RAW_DATA);
  62. $data['result'] = Tools::xml2arr($response);
  63. return $data;
  64. } catch (\Exception $exception) {
  65. throw new InvalidDecryptException($exception->getMessage(), $exception->getCode());
  66. }
  67. }
  68. }