欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP微信支付实例解析_php实例

程序员文章站 2022-03-28 10:39:23
...
本文为大家分享了PHP微信支付实例,包括PHP微信支付源码,PHP微信退款源码,php微信支付接口,供大家参考,具体内容如下

1.JSapi支付demo(在微信客户端中点击)

createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
 Header("Location: $url"); 
 }else
 {
 //获取code码,以获取openid
  $code = $_GET['code'];
 $jsApi->setCode($code);
 $openid = $jsApi->getOpenId();
 }
 
 //=========步骤2:使用统一支付接口,获取prepay_id============
 //使用统一支付接口
 $unifiedOrder = new UnifiedOrder_pub();
 
 //设置统一支付接口参数
 //设置必填参数
 //appid已填,商户无需重复填写
 //mch_id已填,商户无需重复填写
 //noncestr已填,商户无需重复填写
 //spbill_create_ip已填,商户无需重复填写
 //sign已填,商户无需重复填写
 $unifiedOrder->setParameter("openid","$openid");//商品描述
 $unifiedOrder->setParameter("body","贡献一分钱");//商品描述
 //自定义订单号,此处仅作举例
 $timeStamp = time();
 $out_trade_no = WxPayConf_pub::APPID."$timeStamp";
 $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 
 $unifiedOrder->setParameter("total_fee","1");//总金额
 $unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
 $unifiedOrder->setParameter("trade_type","JSAPI");//交易类型
 //非必填参数,商户可根据实际情况选填
 //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号 
 //$unifiedOrder->setParameter("device_info","XXXX");//设备号 
 //$unifiedOrder->setParameter("attach","XXXX");//附加数据 
 //$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
 //$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间 
 //$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记 
 //$unifiedOrder->setParameter("openid","XXXX");//用户标识
 //$unifiedOrder->setParameter("product_id","XXXX");//商品ID

 $prepay_id = $unifiedOrder->getPrepayId();
 //=========步骤3:使用jsapi调起支付============
 $jsApi->setPrepayId($prepay_id);

 $jsApiParameters = $jsApi->getParameters();
 //echo $jsApiParameters;
?>

2.native支付模式一demo(用微信扫的静态链接二维码)

setParameter("product_id","$product_id");//商品id
 //获取链接
 $product_url = $nativeLink->getUrl();

 //使用短链接转换接口
 $shortUrl = new ShortUrl_pub();
 //设置必填参数
 //appid已填,商户无需重复填写
 //mch_id已填,商户无需重复填写
 //noncestr已填,商户无需重复填写
 //sign已填,商户无需重复填写
 $shortUrl->setParameter("long_url","$product_url");//URL链接
 //获取短链接
 $codeUrl = $shortUrl->getShortUrl();
 
?>

3.native支付模式二demo(用微信扫的动态链接二维码)

 setParameter("body","贡献一分钱");//商品描述
 //自定义订单号,此处仅作举例
 $timeStamp = time();
 $out_trade_no = WxPayConf_pub::APPID."$timeStamp";
 $unifiedOrder->setParameter("out_trade_no","$out_trade_no");//商户订单号 
 $unifiedOrder->setParameter("total_fee","1");//总金额
 $unifiedOrder->setParameter("notify_url",WxPayConf_pub::NOTIFY_URL);//通知地址 
 $unifiedOrder->setParameter("trade_type","NATIVE");//交易类型
 //非必填参数,商户可根据实际情况选填
 //$unifiedOrder->setParameter("sub_mch_id","XXXX");//子商户号 
 //$unifiedOrder->setParameter("device_info","XXXX");//设备号 
 //$unifiedOrder->setParameter("attach","XXXX");//附加数据 
 //$unifiedOrder->setParameter("time_start","XXXX");//交易起始时间
 //$unifiedOrder->setParameter("time_expire","XXXX");//交易结束时间 
 //$unifiedOrder->setParameter("goods_tag","XXXX");//商品标记 
 //$unifiedOrder->setParameter("openid","XXXX");//用户标识
 //$unifiedOrder->setParameter("product_id","XXXX");//商品ID
 
 //获取统一支付接口结果
 $unifiedOrderResult = $unifiedOrder->getResult();
 
 //商户根据实际情况设置相应的处理流程
 if ($unifiedOrderResult["return_code"] == "FAIL") 
 {
 //商户自行增加处理流程
 echo "通信出错:".$unifiedOrderResult['return_msg']."
"; } elseif($unifiedOrderResult["result_code"] == "FAIL") { //商户自行增加处理流程 echo "错误代码:".$unifiedOrderResult['err_code']."
"; echo "错误代码描述:".$unifiedOrderResult['err_code_des']."
"; } elseif($unifiedOrderResult["code_url"] != NULL) { //从统一支付接口获取到code_url $code_url = $unifiedOrderResult["code_url"]; //商户自行增加处理流程 //...... } ?>

4.支付查询接口demo

setParameter("out_trade_no","$out_trade_no");//商户订单号 
 //非必填参数,商户可根据实际情况选填
 //$orderQuery->setParameter("sub_mch_id","XXXX");//子商户号 
 //$orderQuery->setParameter("transaction_id","XXXX");//微信订单号
 
 //获取订单查询结果
 $orderQueryResult = $orderQuery->getResult();
 
 //商户根据实际情况设置相应的处理流程,此处仅作举例
 if ($orderQueryResult["return_code"] == "FAIL") {
 echo "通信出错:".$orderQueryResult['return_msg']."
"; } elseif($orderQueryResult["result_code"] == "FAIL"){ echo "错误代码:".$orderQueryResult['err_code']."
"; echo "错误代码描述:".$orderQueryResult['err_code_des']."
"; } else{ echo "交易状态:".$orderQueryResult['trade_state']."
"; echo "设备号:".$orderQueryResult['device_info']."
"; echo "用户标识:".$orderQueryResult['openid']."
"; echo "是否关注公众账号:".$orderQueryResult['is_subscribe']."
"; echo "交易类型:".$orderQueryResult['trade_type']."
"; echo "付款银行:".$orderQueryResult['bank_type']."
"; echo "总金额:".$orderQueryResult['total_fee']."
"; echo "现金券金额:".$orderQueryResult['coupon_fee']."
"; echo "货币种类:".$orderQueryResult['fee_type']."
"; echo "微信支付订单号:".$orderQueryResult['transaction_id']."
"; echo "商户订单号:".$orderQueryResult['out_trade_no']."
"; echo "商家数据包:".$orderQueryResult['attach']."
"; echo "支付完成时间:".$orderQueryResult['time_end']."
"; } } //商户自行增加处理流程 //...... ?>

5.对账单接口demo

setParameter("bill_date","$bill_date");//对账单日期 
 $downloadBill->setParameter("bill_type","ALL");//账单类型 
 //非必填参数,商户可根据实际情况选填
 //$downloadBill->setParameter("device_info","XXXX");//设备号 
 
 //对账单接口结果
 $downloadBillResult = $downloadBill->getResult();
 echo $downloadBillResult['return_code'];
 
 if ($downloadBillResult['return_code'] == "FAIL") {
 echo "通信出错:".$downloadBillResult['return_msg'];
 }else{
 print_r('
');
 echo "【对账单详情】"."";
 print_r($downloadBill->response);
 print_r('
'); } } ?>

6.退款接口demo

setParameter("out_trade_no","$out_trade_no");//商户订单号
 $refund->setParameter("out_refund_no","$out_refund_no");//商户退款单号
 $refund->setParameter("total_fee","$total_fee");//总金额
 $refund->setParameter("refund_fee","$refund_fee");//退款金额
 $refund->setParameter("op_user_id",WxPayConf_pub::MCHID);//操作员
 //非必填参数,商户可根据实际情况选填
 //$refund->setParameter("sub_mch_id","XXXX");//子商户号 
 //$refund->setParameter("device_info","XXXX");//设备号 
 //$refund->setParameter("transaction_id","XXXX");//微信订单号
 
 //调用结果
 $refundResult = $refund->getResult();
 
 //商户根据实际情况设置相应的处理流程,此处仅作举例
 if ($refundResult["return_code"] == "FAIL") {
 echo "通信出错:".$refundResult['return_msg']."
"; } else{ echo "业务结果:".$refundResult['result_code']."
"; echo "错误代码:".$refundResult['err_code']."
"; echo "错误代码描述:".$refundResult['err_code_des']."
"; echo "公众账号ID:".$refundResult['appid']."
"; echo "商户号:".$refundResult['mch_id']."
"; echo "子商户号:".$refundResult['sub_mch_id']."
"; echo "设备号:".$refundResult['device_info']."
"; echo "签名:".$refundResult['sign']."
"; echo "微信订单号:".$refundResult['transaction_id']."
"; echo "商户订单号:".$refundResult['out_trade_no']."
"; echo "商户退款单号:".$refundResult['out_refund_no']."
"; echo "微信退款单号:".$refundResult['refund_idrefund_id']."
"; echo "退款渠道:".$refundResult['refund_channel']."
"; echo "退款金额:".$refundResult['refund_fee']."
"; echo "现金券退款金额:".$refundResult['coupon_refund_fee']."
"; } } ?>

7.退款查询接口demo

setParameter("out_trade_no","$out_trade_no");//商户订单号
 // $refundQuery->setParameter("out_refund_no","XXXX");//商户退款单号
 // $refundQuery->setParameter("refund_id","XXXX");//微信退款单号
 // $refundQuery->setParameter("transaction_id","XXXX");//微信退款单号
 //非必填参数,商户可根据实际情况选填
 //$refundQuery->setParameter("sub_mch_id","XXXX");//子商户号 
 //$refundQuery->setParameter("device_info","XXXX");//设备号 
 
 //退款查询接口结果
 $refundQueryResult = $refundQuery->getResult();
 
 //商户根据实际情况设置相应的处理流程,此处仅作举例
 if ($refundQueryResult["return_code"] == "FAIL") {
 echo "通信出错:".$refundQueryResult['return_msg']."
"; } else{ echo "业务结果:".$refundQueryResult['result_code']."
"; echo "错误代码:".$refundQueryResult['err_code']."
"; echo "错误代码描述:".$refundQueryResult['err_code_des']."
"; echo "公众账号ID:".$refundQueryResult['appid']."
"; echo "商户号:".$refundQueryResult['mch_id']."
"; echo "子商户号:".$refundQueryResult['sub_mch_id']."
"; echo "设备号:".$refundQueryResult['device_info']."
"; echo "签名:".$refundQueryResult['sign']."
"; echo "微信订单号:".$refundQueryResult['transaction_id']."
"; echo "商户订单号:".$refundQueryResult['out_trade_no']."
"; echo "退款笔数:".$refundQueryResult['refund_count']."
"; echo "商户退款单号:".$refundQueryResult['out_refund_no']."
"; echo "微信退款单号:".$refundQueryResult['refund_idrefund_id']."
"; echo "退款渠道:".$refundQueryResult['refund_channel']."
"; echo "退款金额:".$refundQueryResult['refund_fee']."
"; echo "现金券退款金额:".$refundQueryResult['coupon_refund_fee']."
"; echo "退款状态:".$refundQueryResult['refund_status']."
"; } } ?>

微信支付源码下载

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。