From 53f3dce3dd77e8244f160439c87cd500f99e1df4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Fri, 28 Apr 2023 19:45:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9B=B4=E5=A4=9A=E7=9B=B4?= =?UTF-8?q?=E8=BF=9E=E5=95=86=E6=88=B7=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WePayV3/Contracts/BasicWePay.php | 13 +++-- WePayV3/Order.php | 90 ++++++++++++++++++++++++++++++-- WePayV3/Refund.php | 2 +- 3 files changed, 95 insertions(+), 10 deletions(-) diff --git a/WePayV3/Contracts/BasicWePay.php b/WePayV3/Contracts/BasicWePay.php index f177e4c..95b5aa9 100644 --- a/WePayV3/Contracts/BasicWePay.php +++ b/WePayV3/Contracts/BasicWePay.php @@ -135,24 +135,28 @@ abstract class BasicWePay * @param string $method 请求访问 * @param string $pathinfo 请求路由 * @param string $jsondata 请求数据 - * @param bool $verify 是否验证 - * @return array + * @param boolean $verify 是否验证 + * @param boolean $isjson 返回JSON + * @return array|string * @throws \WeChat\Exceptions\InvalidResponseException */ - public function doRequest($method, $pathinfo, $jsondata = '', $verify = false) + public function doRequest($method, $pathinfo, $jsondata = '', $verify = false, $isjson = true) { list($time, $nonce) = [time(), uniqid() . rand(1000, 9999)]; $signstr = join("\n", [$method, $pathinfo, $time, $nonce, $jsondata, '']); + // 生成数据签名TOKEN $token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"', $this->config['mch_id'], $nonce, $time, $this->config['cert_serial'], $this->signBuild($signstr) ); + list($header, $content) = $this->_doRequestCurl($method, $this->base . $pathinfo, [ 'data' => $jsondata, 'header' => [ "Accept: application/json", "Content-Type: application/json", 'User-Agent: https://thinkadmin.top', "Authorization: WECHATPAY2-SHA256-RSA2048 {$token}", ], ]); + if ($verify) { $headers = []; foreach (explode("\n", $header) as $line) { @@ -171,7 +175,8 @@ abstract class BasicWePay throw new InvalidResponseException($exception->getMessage(), $exception->getCode()); } } - return json_decode($content, true); + + return $isjson ? json_decode($content, true) : $content; } /** diff --git a/WePayV3/Order.php b/WePayV3/Order.php index c3d5dbd..02e5439 100644 --- a/WePayV3/Order.php +++ b/WePayV3/Order.php @@ -22,7 +22,7 @@ use WePayV3\Contracts\BasicWePay; use WePayV3\Contracts\DecryptAes; /** - * 订单支付接口 + * 直连商户 | 订单支付接口 * Class Order * @package WePayV3 */ @@ -39,6 +39,7 @@ class Order extends BasicWePay * @param array $data 支付参数 * @return array * @throws \WeChat\Exceptions\InvalidResponseException + * @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_1.shtml */ public function create($type, $data) { @@ -73,18 +74,32 @@ class Order extends BasicWePay /** * 支付订单查询 - * @param string $orderNo 订单单号 + * @param string $tradeNo 订单单号 * @return array * @throws \WeChat\Exceptions\InvalidResponseException + * @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml */ - public function query($orderNo) + public function query($tradeNo) { - $pathinfo = "/v3/pay/transactions/out-trade-no/{$orderNo}"; + $pathinfo = "/v3/pay/transactions/out-trade-no/{$tradeNo}"; return $this->doRequest('GET', "{$pathinfo}?mchid={$this->config['mch_id']}", '', true); } /** - * 支付通知 + * 关闭支付订单 + * @param string $tradeNo 订单单号 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + */ + public function close($tradeNo) + { + $data = ['mchid' => $this->config['mch_id']]; + $path = "/v3/pay/transactions/out-trade-no/{$tradeNo}/close"; + return $this->doRequest('POST', $path, json_encode($data, JSON_UNESCAPED_UNICODE), true); + } + + /** + * 支付通知解析 * @return array * @throws \WeChat\Exceptions\InvalidDecryptException */ @@ -107,4 +122,69 @@ class Order extends BasicWePay } return $data; } + + /** + * 创建退款订单 + * @param array $data 退款参数 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_9.shtml + */ + public function createRefund($data) + { + $path = '/v3/refund/domestic/refunds'; + return $this->doRequest('POST', $path, json_encode($data, JSON_UNESCAPED_UNICODE), true); + } + + /** + * 退款订单查询 + * @param string $refundNo 退款单号 + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_10.shtml + */ + public function queryRefund($refundNo) + { + $path = "/v3/refund/domestic/refunds/{$refundNo}"; + return $this->doRequest('GET', $path, '', true); + } + + /** + * 申请交易账单 + * @param array|string $params + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_6.shtml + */ + public function tradeBill($params) + { + $path = '/v3/bill/tradebill?' . is_array($params) ? http_build_query($params) : $params; + return $this->doRequest('GET', $path, '', true); + } + + /** + * 申请资金账单 + * @param array|string $params + * @return array + * @throws \WeChat\Exceptions\InvalidResponseException + * @document https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_3_7.shtml + */ + public function fundflowBill($params) + { + $path = '/v3/bill/fundflowbill?' . is_array($params) ? http_build_query($params) : $params; + return $this->doRequest('GET', $path, '', true); + } + + /** + * 下载账单文件 + * @param string $fileurl + * @return string 二进制 Excel 内容 + * @throws \WeChat\Exceptions\InvalidResponseException + * @document https://pay.weixin.qq.com/wiki/doc/apiv3_partner/apis/chapter7_6_1.shtml + */ + public function downloadBill($fileurl) + { + $path = strstr($fileurl, '/v3/'); + return $this->doRequest('GET', $path, '', false, false); + } } diff --git a/WePayV3/Refund.php b/WePayV3/Refund.php index bae2563..eeb80a2 100644 --- a/WePayV3/Refund.php +++ b/WePayV3/Refund.php @@ -22,7 +22,7 @@ use WeChat\Exceptions\InvalidResponseException; use WePayV3\Contracts\BasicWePay; /** - * 订单退款接口 + * 电商接口 | 订单退款接口 * Class Refund * @package WePayV3 */