1 | <?php |
||||
2 | |||||
3 | /* |
||||
4 | * This file is part of the overtrue/wechat. |
||||
5 | * |
||||
6 | * (c) overtrue <[email protected]> |
||||
7 | * |
||||
8 | * This source file is subject to the MIT license that is bundled |
||||
9 | * with this source code in the file LICENSE. |
||||
10 | */ |
||||
11 | |||||
12 | namespace EasyWeChat\Payment\Jssdk; |
||||
13 | |||||
14 | use EasyWeChat\BasicService\Jssdk\Client as JssdkClient; |
||||
15 | use EasyWeChat\Kernel\Support; |
||||
16 | |||||
17 | /** |
||||
18 | * Class Client. |
||||
19 | * |
||||
20 | * @author overtrue <[email protected]> |
||||
21 | */ |
||||
22 | class Client extends JssdkClient |
||||
23 | { |
||||
24 | /** |
||||
25 | * [WeixinJSBridge] Generate js config for payment. |
||||
26 | * |
||||
27 | * <pre> |
||||
28 | * WeixinJSBridge.invoke( |
||||
29 | * 'getBrandWCPayRequest', |
||||
30 | * ... |
||||
31 | * ); |
||||
32 | * </pre> |
||||
33 | * |
||||
34 | * @param string $prepayId |
||||
35 | * @param bool $json |
||||
36 | * |
||||
37 | * @return string|array |
||||
38 | */ |
||||
39 | 1 | public function bridgeConfig(string $prepayId, bool $json = true) |
|||
40 | { |
||||
41 | $params = [ |
||||
42 | 1 | 'appId' => $this->app['config']->sub_appid ?: $this->app['config']->app_id, |
|||
43 | 1 | 'timeStamp' => strval(time()), |
|||
44 | 1 | 'nonceStr' => uniqid(), |
|||
45 | 1 | 'package' => "prepay_id=$prepayId", |
|||
46 | 1 | 'signType' => 'MD5', |
|||
47 | ]; |
||||
48 | |||||
49 | 1 | $params['paySign'] = Support\generate_sign($params, $this->app['config']->key, 'md5'); |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
50 | |||||
51 | 1 | return $json ? json_encode($params) : $params; |
|||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * [JSSDK] Generate js config for payment. |
||||
56 | * |
||||
57 | * <pre> |
||||
58 | * wx.chooseWXPay({...}); |
||||
59 | * </pre> |
||||
60 | * |
||||
61 | * @param string $prepayId |
||||
62 | * |
||||
63 | * @return array |
||||
64 | */ |
||||
65 | 1 | public function sdkConfig(string $prepayId): array |
|||
66 | { |
||||
67 | 1 | $config = $this->bridgeConfig($prepayId, false); |
|||
68 | |||||
69 | 1 | $config['timestamp'] = $config['timeStamp']; |
|||
70 | 1 | unset($config['timeStamp']); |
|||
71 | |||||
72 | 1 | return $config; |
|||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * Generate app payment parameters. |
||||
77 | * |
||||
78 | * @param string $prepayId |
||||
79 | * |
||||
80 | * @return array |
||||
81 | */ |
||||
82 | 1 | public function appConfig(string $prepayId): array |
|||
83 | { |
||||
84 | $params = [ |
||||
85 | 1 | 'appid' => $this->app['config']->app_id, |
|||
86 | 1 | 'partnerid' => $this->app['config']->mch_id, |
|||
87 | 1 | 'prepayid' => $prepayId, |
|||
88 | 1 | 'noncestr' => uniqid(), |
|||
89 | 1 | 'timestamp' => time(), |
|||
90 | 1 | 'package' => 'Sign=WXPay', |
|||
91 | ]; |
||||
92 | |||||
93 | 1 | $params['sign'] = Support\generate_sign($params, $this->app['config']->key); |
|||
0 ignored issues
–
show
The function
generate_sign was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
94 | |||||
95 | 1 | return $params; |
|||
96 | } |
||||
97 | |||||
98 | /** |
||||
99 | * Generate js config for share user address. |
||||
100 | * |
||||
101 | * @param string $accessToken |
||||
102 | * @param bool $json |
||||
103 | * |
||||
104 | * @return string|array |
||||
105 | */ |
||||
106 | 1 | public function shareAddressConfig(string $accessToken, bool $json = true) |
|||
107 | { |
||||
108 | $params = [ |
||||
109 | 1 | 'appId' => $this->app['config']->app_id, |
|||
110 | 1 | 'scope' => 'jsapi_address', |
|||
111 | 1 | 'timeStamp' => strval(time()), |
|||
112 | 1 | 'nonceStr' => uniqid(), |
|||
113 | 1 | 'signType' => 'SHA1', |
|||
114 | ]; |
||||
115 | |||||
116 | $signParams = [ |
||||
117 | 1 | 'appid' => $params['appId'], |
|||
118 | 1 | 'url' => $this->getUrl(), |
|||
119 | 1 | 'timestamp' => $params['timeStamp'], |
|||
120 | 1 | 'noncestr' => $params['nonceStr'], |
|||
121 | 1 | 'accesstoken' => strval($accessToken), |
|||
122 | ]; |
||||
123 | |||||
124 | 1 | ksort($signParams); |
|||
125 | |||||
126 | 1 | $params['addrSign'] = sha1(urldecode(http_build_query($signParams))); |
|||
127 | |||||
128 | 1 | return $json ? json_encode($params) : $params; |
|||
129 | } |
||||
130 | |||||
131 | /** |
||||
132 | * Generate js config for contract of mini program. |
||||
133 | * |
||||
134 | * @param array $params |
||||
135 | * |
||||
136 | * @return array |
||||
137 | */ |
||||
138 | public function contractConfig(array $params): array |
||||
139 | { |
||||
140 | $params['appid'] = $this->app['config']->app_id; |
||||
141 | $params['timestamp'] = time(); |
||||
142 | |||||
143 | $params['sign'] = Support\generate_sign($params, $this->app['config']->key); |
||||
0 ignored issues
–
show
The function
generate_sign was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
144 | |||||
145 | return $params; |
||||
146 | } |
||||
147 | } |
||||
148 |