$wx_config['appid'], 'secret' => $wx_config['secret'], // 'mch_id' => $pay_config['mchid'], // 'key' => $pay_config['key'], // 'notify_url' => $pay_config['notify_url'], ]; } /** * 公众号实例 * @return \EasyWeChat\OfficialAccount\Application */ public static function getApp(): \EasyWeChat\Payment\Application { return Factory::payment(self::getConfig()); } /** * 小程序实例 * @return \EasyWeChat\MiniProgram\Application */ public static function getMiniApp(): \EasyWeChat\MiniProgram\Application { return Factory::miniProgram(self::getConfig()); } /** * 支付实例 * @return \EasyWeChat\Payment\Application */ public static function getPayment(): \EasyWeChat\Payment\Application { $config = Factory::payment(self::getConfig()); // $config['cert_path'] = APP_PATH . '/common/certs/apiclient_cert.pem'; // $config['key_path'] = APP_PATH . '/common/certs/apiclient_key.pem'; return $config; } /** * H5发起登录 * @param array $params 回调参数 * @param int $type 授权方式 * @return \Symfony\Component\HttpFoundation\RedirectResponse|void */ 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(); } /** * 小程序登录 * @param string $code 前端js code * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException */ public function miniLogin(string $code) { $app = self::getMiniApp(); return $app->auth->session($code); } /** * 统一支付 * @param string $openid 用户open id * @param string $out_trade_no 订单编号 * @param int $fee 订单金额 * @param string $body 订单说明 * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException * @throws \GuzzleHttp\Exception\GuzzleException */ 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); } /** * 微信小程序消息解密 * 比如获取电话等功能,信息是加密的,需要解密 * @param $session * @param $iv * @param $encryptedData * @return mixed */ public function decryptedData($session, $iv, $encryptedData) { $app = self::getMiniApp(); return $app->encryptor->decryptData($session, $iv, $encryptedData); } }