123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace addons\exam\library;
- use EasyWeChat\Factory;
- class WechatService
- {
- public static function getConfig(): array
- {
- $wx_config = getConfig('wx_config');
- switch (true) {
- case is_empty_in_array($wx_config, 'appid'):
- fail('缺少app_id配置');
- case is_empty_in_array($wx_config, 'secret'):
- fail('缺少secret配置');
-
-
-
-
-
-
- }
- return [
- 'app_id' => $wx_config['appid'],
- 'secret' => $wx_config['secret'],
-
-
-
- ];
- }
-
- public static function getApp(): \EasyWeChat\Payment\Application
- {
- return Factory::payment(self::getConfig());
- }
-
- public static function getMiniApp(): \EasyWeChat\MiniProgram\Application
- {
- return Factory::miniProgram(self::getConfig());
- }
-
- public static function getPayment(): \EasyWeChat\Payment\Application
- {
- $config = Factory::payment(self::getConfig());
-
-
- return $config;
- }
-
- public function login(array $params = [], int $type = 0)
- {
- $scopes = $type ? 'snsapi_userinfo' : 'snsapi_base';
- $callback_url = self::CALLBACK_URL;
- $callback_url = $params ? $callback_url . '?' . http_build_query($params) : $callback_url;
- $this->getApp()->oauth->withRedirectUrl($callback_url)->scopes([$scopes])->redirect()->send();
- }
-
- public function miniLogin(string $code)
- {
- $app = self::getMiniApp();
- return $app->auth->session($code);
- }
-
- public function unifyPay(string $openid, string $out_trade_no, int $fee, string $body)
- {
- $payment = self::getPayment();
- $result = $payment->order->unify([
- 'body' => $body,
- 'out_trade_no' => $out_trade_no,
- 'total_fee' => $fee,
- 'trade_type' => 'JSAPI',
- 'openid' => $openid,
- ]);
- if ($result['return_code'] == 'FAIL') {
- fail('发起支付失败:' . $result['return_msg']);
- }
- return $payment->jssdk->bridgeConfig($result['prepay_id'], false);
- }
-
- public function decryptedData($session, $iv, $encryptedData)
- {
- $app = self::getMiniApp();
- return $app->encryptor->decryptData($session, $iv, $encryptedData);
- }
-
- }
|