Refund.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 WePay;
  14. use WeChat\Contracts\BasicWePay;
  15. use WeChat\Contracts\Tools;
  16. use WeChat\Exceptions\InvalidDecryptException;
  17. use WeChat\Exceptions\InvalidResponseException;
  18. /**
  19. * 微信商户退款
  20. * Class Refund
  21. * @package WePay
  22. */
  23. class Refund extends BasicWePay
  24. {
  25. /**
  26. * 创建退款订单
  27. * @param array $options
  28. * @return array
  29. * @throws InvalidResponseException
  30. * @throws \WeChat\Exceptions\LocalCacheException
  31. */
  32. public function create(array $options)
  33. {
  34. $url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
  35. return $this->callPostApi($url, $options, true);
  36. }
  37. /**
  38. * 查询退款
  39. * @param array $options
  40. * @return array
  41. * @throws InvalidResponseException
  42. * @throws \WeChat\Exceptions\LocalCacheException
  43. */
  44. public function query(array $options)
  45. {
  46. $url = 'https://api.mch.weixin.qq.com/pay/refundquery';
  47. return $this->callPostApi($url, $options);
  48. }
  49. /**
  50. * 获取退款通知
  51. * @return array
  52. * @throws InvalidDecryptException
  53. * @throws InvalidResponseException
  54. */
  55. public function getNotify()
  56. {
  57. $data = Tools::xml2arr(file_get_contents("php://input"));
  58. if (!isset($data['return_code']) || $data['return_code'] !== 'SUCCESS') {
  59. throw new InvalidResponseException('获取退款通知XML失败!');
  60. }
  61. try {
  62. $key = md5($this->config->get('mch_key'));
  63. $decrypt = base64_decode($data['req_info']);
  64. $response = openssl_decrypt($decrypt, 'aes-256-ecb', $key, OPENSSL_RAW_DATA);
  65. $data['result'] = Tools::xml2arr($response);
  66. return $data;
  67. } catch (\Exception $exception) {
  68. throw new InvalidDecryptException($exception->getMessage(), $exception->getCode());
  69. }
  70. }
  71. }