From b07eacde23adf6b5d3ec3dec81f98a482f3432ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Thu, 1 Feb 2018 11:56:23 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E5=AE=8C=E5=96=84=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=A1=88=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Test/config.php | 4 ++-- Test/pay-close-order.php | 37 +++++++++++++++++++++++++++++++ Test/pay-download-bill.php | 40 +++++++++++++++++++++++++++++++++ Test/pay-order-create.php | 45 ++++++++++++++++++++++++++++++++++++++ Test/pay-order-query.php | 40 +++++++++++++++++++++++++++++++++ Test/pay-refund-create.php | 41 ++++++++++++++++++++++++++++++++++ Test/pay-refund-query.php | 41 ++++++++++++++++++++++++++++++++++ WeChat/Pay.php | 27 ++++++++++++++++++----- 8 files changed, 267 insertions(+), 8 deletions(-) create mode 100644 Test/pay-close-order.php create mode 100644 Test/pay-download-bill.php create mode 100644 Test/pay-order-create.php create mode 100644 Test/pay-order-query.php create mode 100644 Test/pay-refund-create.php create mode 100644 Test/pay-refund-query.php diff --git a/Test/config.php b/Test/config.php index 07c8688..732a81f 100644 --- a/Test/config.php +++ b/Test/config.php @@ -18,8 +18,8 @@ return [ 'appsecret' => '71308e96a204296c57d7cd4b21b883e8', 'encodingaeskey' => 'BJIUzE0gqlWy0GxfPp4J1oPTBmOrNDIGPNav1YFH5Z5', // 配置商户支付参数 - 'mch_id' => "1235704602", - 'mch_key' => 'IKI4kpHjU94ji3oqre5zYaQMwLHuZPmj', + 'mch_id' => "1332187001", + 'mch_key' => '11bd3d66d85f322a1e803cb587d18c3f', // 配置商户支付双向证书目录 'ssl_key' => '', 'ssl_cer' => '', diff --git a/Test/pay-close-order.php b/Test/pay-close-order.php new file mode 100644 index 0000000..3a31ddc --- /dev/null +++ b/Test/pay-close-order.php @@ -0,0 +1,37 @@ +closeOrder($options); + + var_export($result); + +} catch (Exception $e) { + + // 出错啦,处理下吧 + echo $e->getMessage() . PHP_EOL; + +} \ No newline at end of file diff --git a/Test/pay-download-bill.php b/Test/pay-download-bill.php new file mode 100644 index 0000000..c0181fb --- /dev/null +++ b/Test/pay-download-bill.php @@ -0,0 +1,40 @@ + '20171001', + 'bill_type' => 'ALL', + ]; + $result = $wechat->billDownload($options); + + var_export($result); + +} catch (Exception $e) { + + // 出错啦,处理下吧 + echo $e->getMessage() . PHP_EOL; + +} \ No newline at end of file diff --git a/Test/pay-order-create.php b/Test/pay-order-create.php new file mode 100644 index 0000000..9d4b5e7 --- /dev/null +++ b/Test/pay-order-create.php @@ -0,0 +1,45 @@ + '测试商品', + 'out_trade_no' => time(), + 'total_fee' => '1', + 'openid' => 'o38gpszoJoC9oJYz3UHHf6bEp0Lo', + 'trade_type' => 'JSAPI', + 'notify_url' => 'http://a.com/text.html', + 'spbill_create_ip' => '127.0.0.1', + ]; + $result = $wechat->createOrder($options); + + var_export($result); + +} catch (Exception $e) { + + // 出错啦,处理下吧 + echo $e->getMessage() . PHP_EOL; + +} \ No newline at end of file diff --git a/Test/pay-order-query.php b/Test/pay-order-query.php new file mode 100644 index 0000000..d993488 --- /dev/null +++ b/Test/pay-order-query.php @@ -0,0 +1,40 @@ + '1008450740201411110005820873', +// 'out_trade_no' => '商户订单号', + ]; + $result = $wechat->queryOrder($options); + + var_export($result); + +} catch (Exception $e) { + + // 出错啦,处理下吧 + echo $e->getMessage() . PHP_EOL; + +} \ No newline at end of file diff --git a/Test/pay-refund-create.php b/Test/pay-refund-create.php new file mode 100644 index 0000000..b344a0c --- /dev/null +++ b/Test/pay-refund-create.php @@ -0,0 +1,41 @@ + '1008450740201411110005820873', + 'total_fee' => '1', + 'refund_fee' => '1', + ]; + $result = $wechat->refund($options); + + var_export($result); + +} catch (Exception $e) { + + // 出错啦,处理下吧 + echo $e->getMessage() . PHP_EOL; + +} \ No newline at end of file diff --git a/Test/pay-refund-query.php b/Test/pay-refund-query.php new file mode 100644 index 0000000..b344a0c --- /dev/null +++ b/Test/pay-refund-query.php @@ -0,0 +1,41 @@ + '1008450740201411110005820873', + 'total_fee' => '1', + 'refund_fee' => '1', + ]; + $result = $wechat->refund($options); + + var_export($result); + +} catch (Exception $e) { + + // 出错啦,处理下吧 + echo $e->getMessage() . PHP_EOL; + +} \ No newline at end of file diff --git a/WeChat/Pay.php b/WeChat/Pay.php index e86940b..8303228 100644 --- a/WeChat/Pay.php +++ b/WeChat/Pay.php @@ -63,8 +63,9 @@ class Pay * 统一下单 * @param array $options * @return array + * @throws InvalidResponseException */ - public function order(array $options) + public function createOrder(array $options) { $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; return $this->callPostApi($url, $options); @@ -74,6 +75,7 @@ class Pay * 查询订单 * @param array $options * @return array + * @throws InvalidResponseException */ public function queryOrder(array $options) { @@ -85,6 +87,7 @@ class Pay * 关闭订单 * @param string $out_trade_no 商户订单号 * @return array + * @throws InvalidResponseException */ public function closeOrder($out_trade_no) { @@ -96,6 +99,7 @@ class Pay * 申请退款 * @param array $options * @return array + * @throws InvalidResponseException */ public function refund(array $options) { @@ -107,6 +111,7 @@ class Pay * 查询退款 * @param array $options * @return array + * @throws InvalidResponseException */ public function queryRefund(array $options) { @@ -118,6 +123,7 @@ class Pay * 交易保障 * @param array $options * @return array + * @throws InvalidResponseException */ public function report(array $options) { @@ -129,6 +135,7 @@ class Pay * 授权码查询openid * @param string $authCode 扫码支付授权码,设备读取用户微信中的条码或者二维码信息 * @return array + * @throws InvalidResponseException */ public function queryAuthCode($authCode) { @@ -140,6 +147,7 @@ class Pay * 转换短链接 * @param string $longUrl 需要转换的URL,签名用原串,传输需URLencode * @return array + * @throws InvalidResponseException */ public function shortUrl($longUrl) { @@ -151,8 +159,9 @@ class Pay * 下载对账单 * @param array $options * @return array + * @throws InvalidResponseException */ - public function bill(array $options) + public function billDownload(array $options) { $url = 'https://api.mch.weixin.qq.com/pay/downloadbill'; return $this->callPostApi($url, $options); @@ -163,6 +172,7 @@ class Pay * 拉取订单评价数据 * @param array $options * @return array + * @throws InvalidResponseException */ public function billCommtent(array $options) { @@ -195,8 +205,8 @@ class Pay { ksort($data); list($key, $str) = [$this->config->get('mch_key'), '']; - foreach ($data as $key => $value) { - $str .= "{$key}={$value}&"; + foreach ($data as $k => $v) { + $str .= "{$k}={$v}&"; } return strtoupper(hash_hmac('SHA256', "{$str}key={$key}", $key)); } @@ -207,13 +217,14 @@ class Pay * @param array $data 接口参数 * @param bool $isCert 是否需要使用双向证书 * @return array + * @throws InvalidResponseException */ public function callPostApi($url, array $data, $isCert = false) { $option = []; if ($isCert) { foreach (['ssl_cer', 'ssl_key'] as $key) { - if (empty($options['ssl_cer'])) { + if (empty($options[$key])) { throw new InvalidArgumentException("Missing Config -- [{$key}]", '0'); } } @@ -223,6 +234,10 @@ class Pay $params = $this->params->merge($data); $params['sign_type'] = 'HMAC-SHA256'; $params['sign'] = $this->getPaySign($params); - return Tools::xml2arr(Tools::post($url, Tools::arr2xml($params), $option)); + $result = Tools::xml2arr(Tools::post($url, Tools::arr2xml($params), $option)); + if ($result['return_code'] !== 'SUCCESS') { + throw new InvalidResponseException($result['return_msg'], '0'); + } + return $result; } } \ No newline at end of file