💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 实际运用 微信支付调起微信支付失败的测试 ``` alert(res.err_code+res.err_desc+res.err_msg); ``` 1. 发起获取code操作 **`$this->code();`** 2. 通过code获取用户openid **`$this->get_code();`** 3. 通过openid 获取 prepay_id `3.4步都在第四步` 4. 通过prepay_id 获取 JSSDK配置信息 **`$this->payment();`** 5. 通过JSSDK发起微信支付 ` 这个操作是前端操作,看最下面` 6. 支付成功后,微信返回异步通知 `微信操作` 7. 处理异步通知结果 **`$this->notify();`** ``` ~~~ namespace app\api\controller; use EasyWeChat\Factory; /** * //微信支付 * Class Pays * @package app\api\controller */ class Pays { protected $wechat_config = []; public function __construct() { $this->wechat_config = [ 'app_id' => 'wx33bbed8b7c826d52', 'mch_id' => '1490502702', 'key' => '6c9e463accf01208d0948dfc4e2bb45b', 'secret' => '9c0229d48cf57fdcecb347ae92f310bd', 'token' => 'ivears_2012', 'notify_url' => 'http://' . $_SERVER['SERVER_NAME'] . url('home/pays/notify_url'), 'log' => [ 'level' => 'debug', 'file' => '/tmp/easywechat.log', ], 'oauth' => [ 'scopes' => ['snsapi_base'], 'callback' => 'http://' . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"], ], 'aes_key' => 'iJakxyndRYJqKKSC7121F3gC3bN13Xv9ODkgKlZSIBZ', ]; } /** * 获取微信 JSSDK * @param $data * @return array|string */ public function payment($data) { $app = Factory::payment($this->wechat_config); //获取微信 prepay_id $payment = $app->order->unify([ 'body' => $data['body'], 'out_trade_no' => $data['out_trade_no'], 'total_fee' => $data['total_fee'], 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型 'openid' => 'oQEMT1ZM340eG2O_xpBKGGKdG9sY', ]); //生成JSSDK $jssdk = $app->jssdk; $config = $jssdk->bridgeConfig($payment['prepay_id']); return $config; } /** * 发起 获取微信CODE 操作 */ public function code() { $app = Factory::OfficialAccount($this->wechat_config); $oauth = $app->oauth; //记录当前URL $oauth->redirect()->send(); } /** * 微信授权登录回调地址, 获取code * @return mixed */ public function get_code() { $app = Factory::OfficialAccount($this->wechat_config); $user = $app->oauth->user(); // 获取 openid $OPENID = $user->getId(); return $OPENID; } /** * 微信异步回调方法 */ public function notify() { $app = Factory::payment($this->wechat_config); $response = $app->handlePaidNotify(function ($message, $fail) { $Pays = new \app\home\controller\Pays(); $result = $Pays->result_reduction($message); if ($result) { return true; } $fail('Order not exists.'); }); $response->send(); } } ~~~ ``` # `发起微信支付前端页面` ``` <script> $(document).ready(function(){ callpay();   }); function jsApiCall() { WeixinJSBridge.invoke( 'getBrandWCPayRequest', <?php echo $config; ?>, function(res){ var pay_type = "{$pay_type}"; if(res.err_msg == "get_brand_wcpay_request:ok"){ layer.msg('支付成功',{icon: 1}); setTimeout(function(){ //返回支付成功页面 window.location = "{:url('members/report/index')}"; },1000) }else{ //支付调试方法 // alert(res.err_code+res.err_desc+res.err_msg); // return false; layer.msg('充值失败',{icon: 2}); setTimeout(function(){ //返回支付失败页面 window.location = "{$site_url}"+"{:url('members/report/index')}"; },1000) } } ); } function callpay() { if (typeof WeixinJSBridge == "undefined"){ if( document.addEventListener ){ document.addEventListener('WeixinJSBridgeReady', jsApiCall, false); }else if (document.attachEvent){ document.attachEvent('WeixinJSBridgeReady', jsApiCall); document.attachEvent('onWeixinJSBridgeReady', jsApiCall); } }else{ jsApiCall(); } } </script> ```