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