Refund.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | WeChatDeveloper
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2023 ThinkAdmin [ thinkadmin.top ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免责声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/WeChatDeveloper
  13. // | github 代码仓库:https://github.com/zoujingli/WeChatDeveloper
  14. // +----------------------------------------------------------------------
  15. namespace WePay;
  16. use WeChat\Contracts\BasicWePay;
  17. use WeChat\Contracts\Tools;
  18. use WeChat\Exceptions\InvalidDecryptException;
  19. use WeChat\Exceptions\InvalidResponseException;
  20. /**
  21. * 微信商户退款
  22. * Class Refund
  23. * @package WePay
  24. */
  25. class Refund extends BasicWePay
  26. {
  27. /**
  28. * 创建退款订单
  29. * @param array $options
  30. * @return array
  31. * @throws \WeChat\Exceptions\InvalidResponseException
  32. * @throws \WeChat\Exceptions\LocalCacheException
  33. */
  34. public function create(array $options)
  35. {
  36. $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
  37. return $this->callPostApi($url, $options, true);
  38. }
  39. /**
  40. * 查询退款
  41. * @param array $options
  42. * @return array
  43. * @throws \WeChat\Exceptions\InvalidResponseException
  44. * @throws \WeChat\Exceptions\LocalCacheException
  45. */
  46. public function query(array $options)
  47. {
  48. $url = 'https://api.mch.weixin.qq.com/pay/refundquery';
  49. return $this->callPostApi($url, $options);
  50. }
  51. /**
  52. * 获取退款通知
  53. * @param string $xml
  54. * @return array
  55. * @throws \WeChat\Exceptions\InvalidDecryptException
  56. * @throws \WeChat\Exceptions\InvalidResponseException
  57. */
  58. public function getNotify($xml = '')
  59. {
  60. $data = Tools::xml2arr(empty($xml) ? Tools::getRawInput() : $xml);
  61. if (!isset($data['return_code']) || $data['return_code'] !== 'SUCCESS') {
  62. throw new InvalidResponseException('获取退款通知XML失败!');
  63. }
  64. try {
  65. $key = md5($this->config->get('mch_key'));
  66. $decrypt = base64_decode($data['req_info']);
  67. $response = openssl_decrypt($decrypt, 'aes-256-ecb', $key, OPENSSL_RAW_DATA);
  68. $data['result'] = Tools::xml2arr($response);
  69. return $data;
  70. } catch (\Exception $exception) {
  71. throw new InvalidDecryptException($exception->getMessage(), $exception->getCode());
  72. }
  73. }
  74. }