#include "WxPayApi.h"
namespace WxAPI{
HttpService * WxPayApi::service = new HttpService;
WxPayApi::WxPayApi(QObject *parent):PayApi(parent)
{
}
void WxPayApi::Micropay( WxPayData &inputObj)
{
qDebug("Micropay is processing...");
QString url = "https://api.mch.weixin.qq.com/pay/micropay";
QString out_trade_no = "";
inputObj.GenerateOutTradeNo(out_trade_no);
this->out_trade_no = out_trade_no;
inputObj.SetValue("out_trade_no",out_trade_no);//产生随机的商户订单号
//检测必填参数
if (!inputObj.IsSet("body"))
{
qDebug("提交被扫支付API接口中,缺少必填参数body!");
return;
}
else if (!inputObj.IsSet("out_trade_no"))
{
qDebug("提交被扫支付API接口中,缺少必填参数out_trade_no!");
return;
}
else if (!inputObj.IsSet("total_fee"))
{
qDebug("提交被扫支付API接口中,缺少必填参数total_fee!");
return;
}
else if (!inputObj.IsSet("auth_code"))
{
qDebug("提交被扫支付API接口中,缺少必填参数auth_code!");
return;
}
//inputObj.SetValue("limit_pay", "no_credit");//不能使用信用卡
inputObj.SetValue("spbill_create_ip", WxPayConfig::IP);//终端ip
inputObj.SetValue("appid", WxPayConfig::APPID);//公众账号ID
inputObj.SetValue("mch_id", WxPayConfig::MCHID);//商户号
QString randString;
inputObj.CreateRandString(randString);
inputObj.SetValue("nonce_str",randString);//随机字符串
QString sign;
inputObj.MakeSign(sign);
inputObj.SetValue("sign",sign);//签名
QString xml;
inputObj.ToXml(xml);
service->Post(xml,url,[&](WxPayData *data){
data->print();
if (data->GetValue("return_code").toString() == "-1") {
emit emitPayResult(-1,"网络异常,请稍后再试!");
delete data;
return;
}
//如果提交被扫支付接口调用失败,则抛异常
if (!data->IsSet("return_code") || data->GetValue("return_code").toString() == "FAIL")
{
QString returnMsg = data->IsSet("return_msg") ? data->GetValue("return_msg").toString() : "";
//Toast::showMessage(returnMsg);
qDebug("Micropay API interface call failur"+returnMsg.toUtf8() );
emit emitPayResult(-1,returnMsg);
delete data;
return;
}
//签名验证
data->CheckSign();
//刷卡支付直接成功
if(data->GetValue("return_code").toString() == "SUCCESS" &&
data->GetValue("result_code").toString() == "SUCCESS")
{
emit emitPayResult(0,"支付成功");
qDebug("支付成功");
delete data;
return;
}
/******************************************************************
* 剩下的都是接口调用成功,业务失败的情况
* ****************************************************************/
//1)业务结果明确失败
if(data->GetValue("err_code").toString() != "USERPAYING" &&
data->GetValue("err_code").toString() != "SYSTEMERROR")
{
QString returnMsg = data->IsSet("err_code_des") ? data->GetValue("err_code_des").toString() : "";
qDebug("Micropay API interface call success, business failure,"+returnMsg.toUtf8() );
emit emitPayResult(-1,returnMsg);
delete data;
return;
}
delete data;
//2)不能确定是否失败,需查单
//用商户订单号去查单
times = 10;
if (!timer) {
timer = new QTimer;
timer->setInterval(2000);
connect(timer,&QTimer::timeout,this,&WxPayApi::loolQueryPay);
timer->start(3000);
}
});
}
void WxPayApi::loolQueryPay()
{
if (times-->0) {
OrderQuery(false,this->out_trade_no);
}else{
if (timer&&timer->isActive()) {
timer->stop();
delete timer;
timer= 0;
}
qDebug("Micropay failure, Reverse order is processing...");
depth=1;
Reverse(false,this->out_trade_no);
emit emitPayResult(-1,"支付失败!");
}
}
void WxPayApi::OrderQuery(bool isTransactionId ,const QString & id){
qDebug("MicroPay OrderQuery 第%d次查询",10-times);
QString url = "https://api.mch.weixin.qq.com/pay/orderquery";
WxPayData inputObj;
if (isTransactionId) {
inputObj.SetValue("transaction_id",id);
}else{
inputObj.SetValue("out_trade_no",id);
}
//检测必填参数
if (!inputObj.IsSet("out_trade_no") && !inputObj.IsSet("transaction_id"))
{
qDebug("订单查询接口中,out_trade_no、transaction_id至少填一个!");
return;
}
inputObj.SetValue("appid", WxPayConfig::APPID);//公众账号ID
inputObj.SetValue("mch_id", WxPayConfig::MCHID);//商户号
QString randString;
inputObj.CreateRandString(randString);
inputObj.SetValue("nonce_str",randString);//随机字符串
QString sign;
inputObj.MakeSign(sign);
inputObj.SetValue("sign",sign);//签名
QString xml;
inputObj.ToXml(xml);
service->Post(xml,url,[&](WxPayData *result){
result->print();
if (result->GetValue("return_code").toString() == "-1") {
if (timer&&timer->isActive()) {
timer->stop();
delete timer;
timer= 0;
}
emit emitPayResult(-1,"网络异常,请稍后再试!");
delete result;
return;
}
if(result->GetValue("return_code").toString() == "SUCCESS"
&& result->GetValue("result_code").toString() == "SUCCESS")
{
//支付成功
if(result->GetValue("trade_state").toString() == "SUCCESS")
{
if (timer&&timer->isActive()) {
timer->stop();
delete timer;
timer= 0;
}
qDebug("查询订单,支付成功");
emit emitPayResult(0,"支付成功");
delete result;
return;
}
//用户支付中,需要继续查询
else if(result->GetValue("trade_state").toString() == "USERPAYING")
{
qDebug("正在支付,继续查询订单");
QString returnMsg = result->IsSet("trade_state_desc") ? result->GetValue("trade_state_desc").toString() : "";
emit emitPayResult(1,returnMsg);
delete result;
return;
}else{
delete result;
}
}
//如果返回错误码为“此交易订单号不存在”则直接认定失败
if(result->GetValue("err_code").toString() == "ORDERNOTEXIST")
{
if (timer&&timer->isActive()) {
timer->stop();
delete timer;
timer= 0;
}
emit emitPayResult(-1,"支付失败");
qDebug("查询订单,支付失败");
delete result;
return;
}
else
{
//如果是系统错误,则后续继续
qDebug("系统错误,继续查询订单");
delete result;
return;
}
delete result;
});
}
//撤销订单API接口
void WxPayApi::Reverse(bool isTransactionId ,const QString & id){
qDebug("MicroPay Reverse 第%d撤销订单",depth);
if(depth > 10)
{
return;
}
QString url = "https://api.mch.weixin.qq.c