小程序微信支付

实现前布置

1:下载官方微信支付SDK(‘https://pay.weixin.qq.com/wiki/doc/api/index.html’)

2:引入开发框架(我写的是微信小程序支付)

话不多说直接上代码

3:控制器方法

public function getPayOrder(){
try{
$tools = new \JsApiPay();
// $openId = $tools->GetOpenid();
$openId = $open_id;
//②、统一下单
$input = new \WxPayUnifiedOrder();
$input->SetBody("");
$input->SetAttach("");
$input->SetOut_trade_no("");
$input->SetTotal_fee("");
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("");
$input->SetNotify_url("你的回调方法");
$input->SetTrade_type("JSAPI");
$input->SetOpenid("");
$config = new \WxPayConfig();
$order = \WxPayApi::unifiedOrder($config, $input);
$printf_info = $this->printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order);
return $jsApiParameters;
} catch(Exception $e) {
\Log::ERROR(json_encode($e));
}
}

不清除的参数可以查看SDJ的源代码

4:回调方法
 public function index(Request $request)
{
    //可以先查看日志
    // file_put_contents('notify.txt', "----------------------------------------\r\n", FILE_APPEND);
    // $data = file_get_contents('php://input');
    // file_put_contents('notify.txt', '收到异步通知:' . json_encode($data) . "\r\n", FILE_APPEND);

$testxml = file_get_contents("php://input");
$jsonxml = json_encode(simplexml_load_string($testxml, 'SimpleXMLElement', LIBXML_NOCDATA));
$result = json_decode($jsonxml, true);//转成数组,
//如果成功返回了
if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
//告诉微信服务器,我已经接收到成功的通知了,你不要再发送了
echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
//处理自己的业务逻辑
}
}
5:微信小程序代码
wx.request({
        url: '请求的地址',
        data:product,
        method:'POST',
        success:res=>{
          wx.hideLoading()
          var data = res.data;
          wx.requestPayment({
            nonceStr: data.nonceStr,
            package: data.package,
            paySign: data.paySign,
            timeStamp: data.timeStamp,
            signType:data.signType,
            success (res) {
              wx.hideLoading()
              wx.switchTab({
                url: '../personal/index/index',
              })
            },
            fail (err) {
              wx.showLoading({
                title: '已取消支付',
              })
              wx.hideLoading()
              wx.switchTab({
                url: '../personal/index/index',
              })
            }
          })
        },
        fail:res=>{
          wx.showToast({
            title: '网络异常',
            icon: 'loading',
            duration: 1000
          })
        }
      })



posted @ 2021-11-10 11:02  用代码砌墙的小白  阅读(348)  评论(0编辑  收藏  举报