附laravel +EasyWechat代码,其实thinkphp也类似,仅做参考:
1、公众号支付等资格申请2、公众号对应的支付商户主体申请 开通H5支付3、配置easywechat,并填写配置文件
4、方法中如下:
public function userOrderWechatMobile(Request $request) { $data = $request->all(); //金钱特殊处理,通过后台查询取得 $res_order = \DB::table('dxjk_order') ->where('order_id',$data['order_id']) ->first(); $order_sid = $res_order->order_sid; $money = $res_order->order_money; $attributes = [ 'trade_type' => 'MWEB', // JSAPI,NATIVE,APP...微信H5就是MWEB 'body' => '测试', 'detail' => '测试', 'out_trade_no' => $order_sid, 'total_fee' => $money * 100, // 单位:分 'notify_url' => 'https://www.df81.com/dxjk/notifyUrlJk', // 'openid' => $request->get('openid'),//小程序支付等需要 'scene_info' => "{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \"https://www.df81.com/m_web\",\"wap_name\": \"丹溪预约\"}}",//这个是H5支付场景所用,项目中使用的jso ]; $orde = new Order($attributes); $dre_url = '指定路径'; // 统一下单 $result = $this->wechat->payment->prepare($orde); $prepayId = null; if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') { $prepayId = $result->prepay_id; $mweb_url = $result->mweb_url . "&redirect_url=" . $dre_url;//返回前台跳转支付的中间也,网址默认跳到发起支付的页面,加参数redirect_url后,跳到指定页面 // $config = $this->wechat->payment->configForPayment($prepayId,false);// 返回 json 字符串,如果想返回数组,传第二个参数 false $config_jsp = $this->wechat->payment->configForJSSDKPayment($prepayId);//两种不同的返回 return ['code' => 1000, 'data' => $config_jsp,'mweb_url' => $mweb_url]; } else { return ['code' => 1001, 'data' => ['message' => '订单支付失败']]; } } //EasyWechat laravel中多个公众号,和商户的支付处理 示例 $options = [ /* * Debug 模式,bool 值:true/false * * 当值为 false 时,所有的日志都不会记录 */ 'debug' => true, /* * 使用 Laravel 的缓存系统 */ 'use_laravel_cache' => true, /* * 账号基本信息,请从微信公众平台/开放平台获取 */ 'app_id' => env('WECHAT_APPID', 'wx5b80a44ad4'), // AppID 'secret' => env('WECHAT_SECRET', 'cdea2e4e50137'), // AppSecret 'token' => env('WECHAT_TOKEN', 'danxigu'), // Token 'aes_key' => env('WECHAT_AES_KEY', 'pG9BO18oH5u'), // EncodingAESKey /** * 开放平台第三方平台配置信息 */ //'open_platform' => [ /** * 事件推送URL */ //'serve_url' => env('WECHAT_OPEN_PLATFORM_SERVE_URL', 'serve'), //], /* * 日志配置 * * level: 日志级别,可选为: * debug/info/notice/warning/error/critical/alert/emergency * file:日志文件位置(绝对路径!!!),要求可写权限 */ 'log' => [ 'level' => env('WECHAT_LOG_LEVEL', 'debug'), 'file' => env('WECHAT_LOG_FILE', storage_path('logs/wechat.log')), ], /* * OAuth 配置 * * only_wechat_browser: 只在微信浏览器跳转 * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login * callback:OAuth授权完成后的回调页地址(如果使用中间件,则随便填写。。。) */ 'oauth' => [ 'only_wechat_browser' => false, 'scopes' => array_map('trim', explode(',', env('WECHAT_OAUTH_SCOPES', 'snsapi_base'))), 'callback' => env('WECHAT_OAUTH_CALLBACK', '/callbackAuthorized'), ], /* * 微信支付 */ 'payment' => [ 'merchant_id' => env('WECHAT_PAYMENT_MERCHANT_ID', '129695'), 'key' => env('WECHAT_PAYMENT_KEY', 'c90bac7e771c266f8'), // md5('') 'cert_path' => env('WECHAT_PAYMENT_CERT_PATH', public_path('jio/cert/apiclient_cert.pem')), // XXX: 绝对路径!!!! 'key_path' => env('WECHAT_PAYMENT_KEY_PATH', public_path('jiao/cert/apiclient_key.pem')), // XXX: 绝对路径!!!! // 'device_info' => env('WECHAT_PAYMENT_DEVICE_INFO', ''), // 'sub_app_id' => env('WECHAT_PAYMENT_SUB_APP_ID', ''), // 'sub_merchant_id' => env('WECHAT_PAYMENT_SUB_MERCHANT_ID', ''), // ... ], ]; $app = new Application($options); $payment = $app->payment; $money = $goods->goods_money; $order_sid = $morder['morder_sid']; $openid = $data['openid']; $goods_name = $goods->goods_name; $attributes = [ 'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP... 'body' => $goods_name, 'detail' => $goods_name, 'out_trade_no' => $order_sid, 'total_fee' => $money * 100, // 单位:分 'notify_url' => 'https://www.df81.com/dxjk/notifyJbb', 'openid' => $openid, ]; $order = new Order($attributes); // 统一下单 $result = $payment->prepare($order); $prepayId = null; if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') { $prepayId = $result->prepay_id; $config = $payment->configForJSSDKPayment($prepayId); return ['code' => 1000, 'data' => $config,'order_sid' => $order_sid]; } else { return ['code' => 1015, 'data' => ['message' => '生成订单错误!']]; }
上一篇: 《建站管家》二次开发文档
下一篇: easywechat报错:the directory "c:\Windows" is not writable的解决方法