123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- // Alipay.cpp : 定义 DLL 的初始化例程。
- //
- #include "stdafx.h"
- #include "Alipay.h"
- #include "PayApi.h"
- #include "EncodingConversion.h"
- // 全局静态变量;
- static PayApi gs_PayApi;
- // 全局静态变量;
- static string gs_strFailMsg;
- static string gs_strReturnMsg;
- // 生成guid函数;
- const char* newguid()
- {
- static char buf[64] = { 0 };
- GUID guid;
- if (S_OK == ::CoCreateGuid(&guid))
- {
- _stprintf_s(buf, sizeof(buf)
- , "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X"
- , guid.Data1
- , guid.Data2
- , guid.Data3
- , guid.Data4[0], guid.Data4[1]
- , guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
- , guid.Data4[6], guid.Data4[7]
- );
- }
- return (const char*)buf;
- }
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
- //
- //TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的,
- // 则从此 DLL 导出的任何调入
- // MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到
- // 该函数的最前面。
- //
- // 例如:
- //
- // extern "C" BOOL PASCAL EXPORT ExportedFunction()
- // {
- // AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // // 此处为普通函数体
- // }
- //
- // 此宏先于任何 MFC 调用
- // 出现在每个函数中十分重要。这意味着
- // 它必须作为函数中的第一个语句
- // 出现,甚至先于所有对象变量声明,
- // 这是因为它们的构造函数可能生成 MFC
- // DLL 调用。
- //
- // 有关其他详细信息,
- // 请参阅 MFC 技术说明 33 和 58。
- //
- // CAlipayApp
- BEGIN_MESSAGE_MAP(CAlipayApp, CWinApp)
- END_MESSAGE_MAP()
- // CAlipayApp 构造
- CAlipayApp::CAlipayApp()
- {
- // TODO: 在此处添加构造代码,
- // 将所有重要的初始化放置在 InitInstance 中
- }
- // 唯一的一个 CAlipayApp 对象
- CAlipayApp theApp;
- // CAlipayApp 初始化
- BOOL CAlipayApp::InitInstance()
- {
- CWinApp::InitInstance();
- return TRUE;
- }
- /************************************************************************/
- /* 函数:[3/31/2017 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:不支付多线程;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- extern "C" LPCTSTR PASCAL EXPORT GetFailMsg()
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 返回失败的错误码;
- return gs_strFailMsg.c_str();
- }
- extern "C" LPCTSTR PASCAL EXPORT GetReturnMsg()
- {
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- // 返回成功的结果;
- return gs_strReturnMsg.c_str();
- }
- /************************************************************************/
- /* 函数:[3/30/2017 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- extern "C" BOOL PASCAL EXPORT Alipay_SetCommonParam(
- LPCTSTR lpAppid,
- LPCTSTR lpPrivateKey,
- LPCTSTR lpSignType,
- LPCTSTR lpCharset,
- LPCTSTR lpVersion,
- LPCTSTR lpUrl,
- LPCTSTR lpPublicKey
- )
- {
- gs_strFailMsg.clear();
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
-
- if (lpAppid == NULL || lpAppid[0] == '\0' ||
- lpPrivateKey == NULL || lpPrivateKey[0] == '\0' ||
- lpSignType == NULL || lpSignType[0] == '\0' ||
- lpCharset == NULL || lpCharset[0] == '\0' ||
- lpVersion == NULL || lpVersion[0] == '\0' ||
- lpUrl == NULL || lpUrl[0] == '\0' ||
- lpPublicKey == NULL || lpPublicKey[0] == '\0'
- )
- {
- // 参数无效;
- gs_strFailMsg = "输出的参数无效";
- return FALSE;
- }
- gs_PayApi.alipay_appid = lpAppid;
- gs_PayApi.alipay_privatekey = lpPrivateKey;
- gs_PayApi.alipay_signtype = lpSignType;
- gs_PayApi.alipay_charset = lpCharset;
- gs_PayApi.alipay_version = lpVersion;
- gs_PayApi.alipay_url = lpUrl;
- gs_PayApi.alipay_PublicKey = lpPublicKey;
- return TRUE;
- }
- /************************************************************************/
- /* 函数:[3/30/2017 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- #ifdef _CUSTOMER_MSG_
- extern "C" LPCTSTR PASCAL EXPORT Alipay_PayContent(LPCTSTR lpOutTradeNo,DOUBLE lfTotalAmount, LPCTSTR lpSubject)
- #else
- extern "C" BOOL PASCAL EXPORT Alipay_PayContent(LPCTSTR lpOutTradeNo, DOUBLE lfTotalAmount, LPCTSTR lpSubject)
- #endif
- {
- gs_strFailMsg.clear();
- gs_strReturnMsg.clear();
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
-
- if (lpOutTradeNo == NULL || lpSubject == NULL)
- {
- gs_strFailMsg = "输出的支付参数无效";
- #ifdef _CUSTOMER_MSG_
- return "";
- #else
- return FALSE;
- #endif
- }
- JsonMap contentMap;
- contentMap.insert(JsonMap::value_type(JsonType("out_trade_no"), JsonType(lpOutTradeNo)));
- contentMap.insert(JsonMap::value_type(JsonType("total_amount"), JsonType(lfTotalAmount)));
- contentMap.insert(JsonMap::value_type(JsonType("subject"), JsonType(lpSubject)));
- string method = "alipay.trade.precate";
- gs_strReturnMsg = gs_PayApi.alipay_invoke(method, contentMap);
- #ifdef _CUSTOMER_MSG_
- return gs_strReturnMsg.c_str();
- #else
- JsonMap respMap = gs_PayApi.alipay_Str2JsonMap(gs_strReturnMsg);
- // 分析结果;
- return TRUE;
- #endif
- }
- /************************************************************************/
- /* 函数:[3/30/2017 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- #ifdef _CUSTOMER_MSG_
- extern "C" LPCTSTR PASCAL EXPORT Alipay_PrecreateContent(LPCTSTR lpOutTradeNo, LPCTSTR lpScence, LPCTSTR lpAuthCode, DOUBLE lfTotalAmount, LPCTSTR lpSubject)
- #else
- extern "C" BOOL PASCAL EXPORT Alipay_PrecreateContent(LPCTSTR lpOutTradeNo, LPCTSTR lpScence, LPCTSTR lpAuthCode, DOUBLE lfTotalAmount, LPCTSTR lpSubject)
- #endif
- {
- gs_strFailMsg.clear();
- gs_strReturnMsg.clear();
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- if (lpOutTradeNo == NULL || lpOutTradeNo[0] == '\0' ||
- lpScence == NULL || lpScence[0] == '\0' ||
- lpAuthCode == NULL || lpAuthCode[0] == '\0' ||
- lpSubject == NULL || lpSubject[0] == '\0' )
- {
- // 参数无效;
- gs_strFailMsg = "输出的支付参数无效";
- #ifdef _CUSTOMER_MSG_
- return "";
- #else
- return FALSE;
- #endif
- }
- JsonMap contentMap;
- // 商户订单号;
- contentMap.insert(JsonMap::value_type(JsonType("out_trade_no"), JsonType(lpOutTradeNo)));
- // 支付场景;
- contentMap.insert(JsonMap::value_type(JsonType("scene"), JsonType(lpScence)));
- // 支付授权码-条码;
- contentMap.insert(JsonMap::value_type(JsonType("auth_code"), JsonType(lpAuthCode)));
- // 描述;
- contentMap.insert(JsonMap::value_type(JsonType("subject"), JsonType(lpSubject)));
- // 金额;
- contentMap.insert(JsonMap::value_type(JsonType("total_amount"), JsonType(lfTotalAmount)));
- string method = "alipay.trade.pay";
- gs_strReturnMsg = gs_PayApi.alipay_invoke(method, contentMap);
- #ifdef _CUSTOMER_MSG_
- return gs_strReturnMsg.c_str();
- #else
- JsonMap respMap = gs_PayApi.alipay_Str2JsonMap(gs_strReturnMsg);
- // 分析结果;
- JsonMap::const_iterator it_code = respMap.find("code");
- if (it_code == respMap.end())
- {
- gs_strFailMsg = "无code信息返回";
- return FALSE;
- }
- if ( _tcscmp(it_code->second.toString().c_str(), "10000") != 0 )
- {
- gs_strFailMsg.append("失败详情:");
- JsonMap::const_iterator it_msg = respMap.find("msg");
- JsonMap::const_iterator it_sub_msg = respMap.find("sub_msg");
- if ( it_msg != respMap.end())
- gs_strFailMsg.append(it_msg->second.toString());
-
- gs_strFailMsg.append(" ");
- if ( it_msg != respMap.end())
- gs_strFailMsg.append(it_sub_msg->second.toString());
- return FALSE;
- }
- return TRUE;
- #endif
- }
- /************************************************************************/
- /* 函数:[3/30/2017 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- extern "C" BOOL PASCAL EXPORT wxpay_SetCommParam(LPCTSTR lpAppId, LPCTSTR lpMchId, LPCTSTR lpSubMchId, LPCTSTR lpPrivateKey)
- {
- gs_strFailMsg.clear();
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
-
- if (lpAppId == NULL || lpAppId[0] == '\0' ||
- lpMchId == NULL || lpMchId[0] == '\0' ||
- lpPrivateKey == NULL || lpPrivateKey[0] == '\0')
- {
- // 参数无效;
- gs_strFailMsg = "输出的参数无效";
- return FALSE;
- }
- gs_PayApi.wxpay_appid = lpAppId;
- gs_PayApi.wxpay_mchid = lpMchId;
- gs_PayApi.wxapi_submchid = lpSubMchId ? lpSubMchId : "";
- gs_PayApi.wxpay_privatekey = lpPrivateKey;
- return TRUE;
- }
- /************************************************************************/
- /* 函数:[3/30/2017 Jeff];
- /* 描述:;
- /* 参数:;
- /* [IN] :;
- /* [OUT] :;
- /* [IN/OUT] :;
- /* 返回:void;
- /* 注意:;
- /* 示例:;
- /*
- /* 修改:;
- /* 日期:;
- /* 内容:;
- /************************************************************************/
- #ifdef _CUSTOMER_MSG_
- extern "C" LPCTSTR PASCAL EXPORT wxpay_PayContent(LPCTSTR lpOutTradeNo, LPCTSTR lpBody, LPCTSTR lpAuthCode, LPCTSTR lpTotalAmount, LPCTSTR lpBillIP)
- #else
- extern "C" BOOL PASCAL EXPORT wxpay_PayContent(LPCTSTR lpOutTradeNo, LPCTSTR lpBody, LPCTSTR lpAuthCode, LPCTSTR lpTotalAmount, LPCTSTR lpBillIP)
- #endif
- {
- gs_strFailMsg.clear();
- gs_strReturnMsg.clear();
- AFX_MANAGE_STATE(AfxGetStaticModuleState());
- if (lpOutTradeNo == NULL || lpOutTradeNo[0] == '\0' ||
- lpBody == NULL || lpBody[0] == '\0' ||
- lpAuthCode == NULL || lpAuthCode[0] == '\0' ||
- lpTotalAmount == NULL || lpTotalAmount[0] == '\0' ||
- lpBillIP == NULL || lpBillIP[0] == '\0' )
- {
- // 参数无效;
- gs_strFailMsg = "输出的支付参数无效";
- #ifdef _CUSTOMER_MSG_
- return "";
- #else
- return FALSE;
- #endif
- }
- StringMap strMap;
- strMap.insert(StringMap::value_type("appid", gs_PayApi.wxpay_appid));
- strMap.insert(StringMap::value_type("mch_id", gs_PayApi.wxpay_mchid));
- if ( !gs_PayApi.wxapi_submchid.empty() )
- strMap.insert(StringMap::value_type("sub_mch_id", gs_PayApi.wxapi_submchid));
- strMap.insert(StringMap::value_type("nonce_str", newguid()));
- strMap.insert(StringMap::value_type("body", lpBody));
- strMap.insert(StringMap::value_type("out_trade_no", lpOutTradeNo));
- strMap.insert(StringMap::value_type("total_fee", lpTotalAmount));
- strMap.insert(StringMap::value_type("spbill_create_ip", lpBillIP));
- strMap.insert(StringMap::value_type("auth_code", lpAuthCode));
- gs_strReturnMsg = gs_PayApi.wxpay_invoke("https://api.mch.weixin.qq.com/pay/micropay", strMap, gs_PayApi.wxpay_privatekey);
- #ifdef _CUSTOMER_MSG_
- return gs_strReturnMsg.c_str();
- #else
- if ( gs_strReturnMsg.empty() )
- {
- gs_strFailMsg = "支付失败,无任何返回";
- return FALSE;
- }
- // 解析结果;
- JsonMap resultMap;
- tinyxml2::XMLDocument doc;
- doc.Parse(gs_strReturnMsg.c_str(), gs_strReturnMsg.length());
- tinyxml2::XMLElement *pRoot = doc.RootElement();
- if ( pRoot )
- {
- tinyxml2::XMLElement *pResultCode = pRoot->FirstChildElement();
- while( pResultCode )
- {
- // 将所有结果放到Map中;
- resultMap.insert(JsonMap::value_type(JsonType(pResultCode->Name()), JsonType(pResultCode->GetText())));
- #ifdef _DEBUG
- WriteTextLog("%s,%s",pResultCode->Name(), pResultCode->GetText());
- #endif
- pResultCode = pResultCode->NextSiblingElement();
- }
- }
- JsonMap::const_iterator it_reurnCode = resultMap.find("return_code");
- if (it_reurnCode == resultMap.end())
- {
- gs_strFailMsg = "支付失败,无返回码";
- return FALSE;
- }
- if ( _tcscmp(it_reurnCode->second.toString().c_str(), "SUCCESS") == 0 )
- {
- JsonMap::const_iterator it_resultCode = resultMap.find("result_code");
- if ( it_resultCode == resultMap.end() )
- {
- gs_strFailMsg = "支付失败,无结果码";
- return FALSE;
- }
- else
- {
- if ( _tcscmp( it_resultCode->second.toString().c_str(), "FAIL") == 0 )
- {
- gs_strFailMsg = "失败详情:";
- JsonMap::const_iterator it_erroCodeDes = resultMap.find("err_code_des");
- if ( it_erroCodeDes != resultMap.end() )
- gs_strFailMsg.append(it_erroCodeDes->second.toString());
- return FALSE;
- }
- }
- }
- else
- {
- JsonMap::const_iterator it_reurnMsg = resultMap.find("return_msg");
- gs_strFailMsg = "失败详情:";
- if (it_reurnMsg == resultMap.end())
- gs_strFailMsg.append(it_reurnMsg->second.toString());
- return FALSE;
- }
- return TRUE;
- #endif
- }
|