Alipay.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. // Alipay.cpp : 定义 DLL 的初始化例程。
  2. //
  3. #include "stdafx.h"
  4. #include "Alipay.h"
  5. #include "PayApi.h"
  6. #include "EncodingConversion.h"
  7. // 全局静态变量;
  8. static PayApi gs_PayApi;
  9. // 全局静态变量;
  10. static string gs_strFailMsg;
  11. static string gs_strReturnMsg;
  12. // 生成guid函数;
  13. const char* newguid()
  14. {
  15. static char buf[64] = { 0 };
  16. GUID guid;
  17. if (S_OK == ::CoCreateGuid(&guid))
  18. {
  19. _stprintf_s(buf, sizeof(buf)
  20. , "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X"
  21. , guid.Data1
  22. , guid.Data2
  23. , guid.Data3
  24. , guid.Data4[0], guid.Data4[1]
  25. , guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
  26. , guid.Data4[6], guid.Data4[7]
  27. );
  28. }
  29. return (const char*)buf;
  30. }
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #endif
  34. //
  35. //TODO: 如果此 DLL 相对于 MFC DLL 是动态链接的,
  36. // 则从此 DLL 导出的任何调入
  37. // MFC 的函数必须将 AFX_MANAGE_STATE 宏添加到
  38. // 该函数的最前面。
  39. //
  40. // 例如:
  41. //
  42. // extern "C" BOOL PASCAL EXPORT ExportedFunction()
  43. // {
  44. // AFX_MANAGE_STATE(AfxGetStaticModuleState());
  45. // // 此处为普通函数体
  46. // }
  47. //
  48. // 此宏先于任何 MFC 调用
  49. // 出现在每个函数中十分重要。这意味着
  50. // 它必须作为函数中的第一个语句
  51. // 出现,甚至先于所有对象变量声明,
  52. // 这是因为它们的构造函数可能生成 MFC
  53. // DLL 调用。
  54. //
  55. // 有关其他详细信息,
  56. // 请参阅 MFC 技术说明 33 和 58。
  57. //
  58. // CAlipayApp
  59. BEGIN_MESSAGE_MAP(CAlipayApp, CWinApp)
  60. END_MESSAGE_MAP()
  61. // CAlipayApp 构造
  62. CAlipayApp::CAlipayApp()
  63. {
  64. // TODO: 在此处添加构造代码,
  65. // 将所有重要的初始化放置在 InitInstance 中
  66. }
  67. // 唯一的一个 CAlipayApp 对象
  68. CAlipayApp theApp;
  69. // CAlipayApp 初始化
  70. BOOL CAlipayApp::InitInstance()
  71. {
  72. CWinApp::InitInstance();
  73. return TRUE;
  74. }
  75. /************************************************************************/
  76. /* 函数:[3/31/2017 Jeff];
  77. /* 描述:;
  78. /* 参数:;
  79. /* [IN] :;
  80. /* [OUT] :;
  81. /* [IN/OUT] :;
  82. /* 返回:void;
  83. /* 注意:不支付多线程;
  84. /* 示例:;
  85. /*
  86. /* 修改:;
  87. /* 日期:;
  88. /* 内容:;
  89. /************************************************************************/
  90. extern "C" LPCTSTR PASCAL EXPORT GetFailMsg()
  91. {
  92. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  93. // 返回失败的错误码;
  94. return gs_strFailMsg.c_str();
  95. }
  96. extern "C" LPCTSTR PASCAL EXPORT GetReturnMsg()
  97. {
  98. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  99. // 返回成功的结果;
  100. return gs_strReturnMsg.c_str();
  101. }
  102. /************************************************************************/
  103. /* 函数:[3/30/2017 Jeff];
  104. /* 描述:;
  105. /* 参数:;
  106. /* [IN] :;
  107. /* [OUT] :;
  108. /* [IN/OUT] :;
  109. /* 返回:void;
  110. /* 注意:;
  111. /* 示例:;
  112. /*
  113. /* 修改:;
  114. /* 日期:;
  115. /* 内容:;
  116. /************************************************************************/
  117. extern "C" BOOL PASCAL EXPORT Alipay_SetCommonParam(
  118. LPCTSTR lpAppid,
  119. LPCTSTR lpPrivateKey,
  120. LPCTSTR lpSignType,
  121. LPCTSTR lpCharset,
  122. LPCTSTR lpVersion,
  123. LPCTSTR lpUrl,
  124. LPCTSTR lpPublicKey
  125. )
  126. {
  127. gs_strFailMsg.clear();
  128. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  129. if (lpAppid == NULL || lpAppid[0] == '\0' ||
  130. lpPrivateKey == NULL || lpPrivateKey[0] == '\0' ||
  131. lpSignType == NULL || lpSignType[0] == '\0' ||
  132. lpCharset == NULL || lpCharset[0] == '\0' ||
  133. lpVersion == NULL || lpVersion[0] == '\0' ||
  134. lpUrl == NULL || lpUrl[0] == '\0' ||
  135. lpPublicKey == NULL || lpPublicKey[0] == '\0'
  136. )
  137. {
  138. // 参数无效;
  139. gs_strFailMsg = "输出的参数无效";
  140. return FALSE;
  141. }
  142. gs_PayApi.alipay_appid = lpAppid;
  143. gs_PayApi.alipay_privatekey = lpPrivateKey;
  144. gs_PayApi.alipay_signtype = lpSignType;
  145. gs_PayApi.alipay_charset = lpCharset;
  146. gs_PayApi.alipay_version = lpVersion;
  147. gs_PayApi.alipay_url = lpUrl;
  148. gs_PayApi.alipay_PublicKey = lpPublicKey;
  149. return TRUE;
  150. }
  151. /************************************************************************/
  152. /* 函数:[3/30/2017 Jeff];
  153. /* 描述:;
  154. /* 参数:;
  155. /* [IN] :;
  156. /* [OUT] :;
  157. /* [IN/OUT] :;
  158. /* 返回:void;
  159. /* 注意:;
  160. /* 示例:;
  161. /*
  162. /* 修改:;
  163. /* 日期:;
  164. /* 内容:;
  165. /************************************************************************/
  166. #ifdef _CUSTOMER_MSG_
  167. extern "C" LPCTSTR PASCAL EXPORT Alipay_PayContent(LPCTSTR lpOutTradeNo,DOUBLE lfTotalAmount, LPCTSTR lpSubject)
  168. #else
  169. extern "C" BOOL PASCAL EXPORT Alipay_PayContent(LPCTSTR lpOutTradeNo, DOUBLE lfTotalAmount, LPCTSTR lpSubject)
  170. #endif
  171. {
  172. gs_strFailMsg.clear();
  173. gs_strReturnMsg.clear();
  174. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  175. if (lpOutTradeNo == NULL || lpSubject == NULL)
  176. {
  177. gs_strFailMsg = "输出的支付参数无效";
  178. #ifdef _CUSTOMER_MSG_
  179. return "";
  180. #else
  181. return FALSE;
  182. #endif
  183. }
  184. JsonMap contentMap;
  185. contentMap.insert(JsonMap::value_type(JsonType("out_trade_no"), JsonType(lpOutTradeNo)));
  186. contentMap.insert(JsonMap::value_type(JsonType("total_amount"), JsonType(lfTotalAmount)));
  187. contentMap.insert(JsonMap::value_type(JsonType("subject"), JsonType(lpSubject)));
  188. string method = "alipay.trade.precate";
  189. gs_strReturnMsg = gs_PayApi.alipay_invoke(method, contentMap);
  190. #ifdef _CUSTOMER_MSG_
  191. return gs_strReturnMsg.c_str();
  192. #else
  193. JsonMap respMap = gs_PayApi.alipay_Str2JsonMap(gs_strReturnMsg);
  194. // 分析结果;
  195. return TRUE;
  196. #endif
  197. }
  198. /************************************************************************/
  199. /* 函数:[3/30/2017 Jeff];
  200. /* 描述:;
  201. /* 参数:;
  202. /* [IN] :;
  203. /* [OUT] :;
  204. /* [IN/OUT] :;
  205. /* 返回:void;
  206. /* 注意:;
  207. /* 示例:;
  208. /*
  209. /* 修改:;
  210. /* 日期:;
  211. /* 内容:;
  212. /************************************************************************/
  213. #ifdef _CUSTOMER_MSG_
  214. extern "C" LPCTSTR PASCAL EXPORT Alipay_PrecreateContent(LPCTSTR lpOutTradeNo, LPCTSTR lpScence, LPCTSTR lpAuthCode, DOUBLE lfTotalAmount, LPCTSTR lpSubject)
  215. #else
  216. extern "C" BOOL PASCAL EXPORT Alipay_PrecreateContent(LPCTSTR lpOutTradeNo, LPCTSTR lpScence, LPCTSTR lpAuthCode, DOUBLE lfTotalAmount, LPCTSTR lpSubject)
  217. #endif
  218. {
  219. gs_strFailMsg.clear();
  220. gs_strReturnMsg.clear();
  221. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  222. if (lpOutTradeNo == NULL || lpOutTradeNo[0] == '\0' ||
  223. lpScence == NULL || lpScence[0] == '\0' ||
  224. lpAuthCode == NULL || lpAuthCode[0] == '\0' ||
  225. lpSubject == NULL || lpSubject[0] == '\0' )
  226. {
  227. // 参数无效;
  228. gs_strFailMsg = "输出的支付参数无效";
  229. #ifdef _CUSTOMER_MSG_
  230. return "";
  231. #else
  232. return FALSE;
  233. #endif
  234. }
  235. JsonMap contentMap;
  236. // 商户订单号;
  237. contentMap.insert(JsonMap::value_type(JsonType("out_trade_no"), JsonType(lpOutTradeNo)));
  238. // 支付场景;
  239. contentMap.insert(JsonMap::value_type(JsonType("scene"), JsonType(lpScence)));
  240. // 支付授权码-条码;
  241. contentMap.insert(JsonMap::value_type(JsonType("auth_code"), JsonType(lpAuthCode)));
  242. // 描述;
  243. contentMap.insert(JsonMap::value_type(JsonType("subject"), JsonType(lpSubject)));
  244. // 金额;
  245. contentMap.insert(JsonMap::value_type(JsonType("total_amount"), JsonType(lfTotalAmount)));
  246. string method = "alipay.trade.pay";
  247. gs_strReturnMsg = gs_PayApi.alipay_invoke(method, contentMap);
  248. #ifdef _CUSTOMER_MSG_
  249. return gs_strReturnMsg.c_str();
  250. #else
  251. JsonMap respMap = gs_PayApi.alipay_Str2JsonMap(gs_strReturnMsg);
  252. // 分析结果;
  253. JsonMap::const_iterator it_code = respMap.find("code");
  254. if (it_code == respMap.end())
  255. {
  256. gs_strFailMsg = "无code信息返回";
  257. return FALSE;
  258. }
  259. if ( _tcscmp(it_code->second.toString().c_str(), "10000") != 0 )
  260. {
  261. gs_strFailMsg.append("失败详情:");
  262. JsonMap::const_iterator it_msg = respMap.find("msg");
  263. JsonMap::const_iterator it_sub_msg = respMap.find("sub_msg");
  264. if ( it_msg != respMap.end())
  265. gs_strFailMsg.append(it_msg->second.toString());
  266. gs_strFailMsg.append(" ");
  267. if ( it_msg != respMap.end())
  268. gs_strFailMsg.append(it_sub_msg->second.toString());
  269. return FALSE;
  270. }
  271. return TRUE;
  272. #endif
  273. }
  274. /************************************************************************/
  275. /* 函数:[3/30/2017 Jeff];
  276. /* 描述:;
  277. /* 参数:;
  278. /* [IN] :;
  279. /* [OUT] :;
  280. /* [IN/OUT] :;
  281. /* 返回:void;
  282. /* 注意:;
  283. /* 示例:;
  284. /*
  285. /* 修改:;
  286. /* 日期:;
  287. /* 内容:;
  288. /************************************************************************/
  289. extern "C" BOOL PASCAL EXPORT wxpay_SetCommParam(LPCTSTR lpAppId, LPCTSTR lpMchId, LPCTSTR lpSubMchId, LPCTSTR lpPrivateKey)
  290. {
  291. gs_strFailMsg.clear();
  292. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  293. if (lpAppId == NULL || lpAppId[0] == '\0' ||
  294. lpMchId == NULL || lpMchId[0] == '\0' ||
  295. lpPrivateKey == NULL || lpPrivateKey[0] == '\0')
  296. {
  297. // 参数无效;
  298. gs_strFailMsg = "输出的参数无效";
  299. return FALSE;
  300. }
  301. gs_PayApi.wxpay_appid = lpAppId;
  302. gs_PayApi.wxpay_mchid = lpMchId;
  303. gs_PayApi.wxapi_submchid = lpSubMchId ? lpSubMchId : "";
  304. gs_PayApi.wxpay_privatekey = lpPrivateKey;
  305. return TRUE;
  306. }
  307. /************************************************************************/
  308. /* 函数:[3/30/2017 Jeff];
  309. /* 描述:;
  310. /* 参数:;
  311. /* [IN] :;
  312. /* [OUT] :;
  313. /* [IN/OUT] :;
  314. /* 返回:void;
  315. /* 注意:;
  316. /* 示例:;
  317. /*
  318. /* 修改:;
  319. /* 日期:;
  320. /* 内容:;
  321. /************************************************************************/
  322. #ifdef _CUSTOMER_MSG_
  323. extern "C" LPCTSTR PASCAL EXPORT wxpay_PayContent(LPCTSTR lpOutTradeNo, LPCTSTR lpBody, LPCTSTR lpAuthCode, LPCTSTR lpTotalAmount, LPCTSTR lpBillIP)
  324. #else
  325. extern "C" BOOL PASCAL EXPORT wxpay_PayContent(LPCTSTR lpOutTradeNo, LPCTSTR lpBody, LPCTSTR lpAuthCode, LPCTSTR lpTotalAmount, LPCTSTR lpBillIP)
  326. #endif
  327. {
  328. gs_strFailMsg.clear();
  329. gs_strReturnMsg.clear();
  330. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  331. if (lpOutTradeNo == NULL || lpOutTradeNo[0] == '\0' ||
  332. lpBody == NULL || lpBody[0] == '\0' ||
  333. lpAuthCode == NULL || lpAuthCode[0] == '\0' ||
  334. lpTotalAmount == NULL || lpTotalAmount[0] == '\0' ||
  335. lpBillIP == NULL || lpBillIP[0] == '\0' )
  336. {
  337. // 参数无效;
  338. gs_strFailMsg = "输出的支付参数无效";
  339. #ifdef _CUSTOMER_MSG_
  340. return "";
  341. #else
  342. return FALSE;
  343. #endif
  344. }
  345. StringMap strMap;
  346. strMap.insert(StringMap::value_type("appid", gs_PayApi.wxpay_appid));
  347. strMap.insert(StringMap::value_type("mch_id", gs_PayApi.wxpay_mchid));
  348. if ( !gs_PayApi.wxapi_submchid.empty() )
  349. strMap.insert(StringMap::value_type("sub_mch_id", gs_PayApi.wxapi_submchid));
  350. strMap.insert(StringMap::value_type("nonce_str", newguid()));
  351. strMap.insert(StringMap::value_type("body", lpBody));
  352. strMap.insert(StringMap::value_type("out_trade_no", lpOutTradeNo));
  353. strMap.insert(StringMap::value_type("total_fee", lpTotalAmount));
  354. strMap.insert(StringMap::value_type("spbill_create_ip", lpBillIP));
  355. strMap.insert(StringMap::value_type("auth_code", lpAuthCode));
  356. gs_strReturnMsg = gs_PayApi.wxpay_invoke("https://api.mch.weixin.qq.com/pay/micropay", strMap, gs_PayApi.wxpay_privatekey);
  357. #ifdef _CUSTOMER_MSG_
  358. return gs_strReturnMsg.c_str();
  359. #else
  360. if ( gs_strReturnMsg.empty() )
  361. {
  362. gs_strFailMsg = "支付失败,无任何返回";
  363. return FALSE;
  364. }
  365. // 解析结果;
  366. JsonMap resultMap;
  367. tinyxml2::XMLDocument doc;
  368. doc.Parse(gs_strReturnMsg.c_str(), gs_strReturnMsg.length());
  369. tinyxml2::XMLElement *pRoot = doc.RootElement();
  370. if ( pRoot )
  371. {
  372. tinyxml2::XMLElement *pResultCode = pRoot->FirstChildElement();
  373. while( pResultCode )
  374. {
  375. // 将所有结果放到Map中;
  376. resultMap.insert(JsonMap::value_type(JsonType(pResultCode->Name()), JsonType(pResultCode->GetText())));
  377. #ifdef _DEBUG
  378. WriteTextLog("%s,%s",pResultCode->Name(), pResultCode->GetText());
  379. #endif
  380. pResultCode = pResultCode->NextSiblingElement();
  381. }
  382. }
  383. JsonMap::const_iterator it_reurnCode = resultMap.find("return_code");
  384. if (it_reurnCode == resultMap.end())
  385. {
  386. gs_strFailMsg = "支付失败,无返回码";
  387. return FALSE;
  388. }
  389. if ( _tcscmp(it_reurnCode->second.toString().c_str(), "SUCCESS") == 0 )
  390. {
  391. JsonMap::const_iterator it_resultCode = resultMap.find("result_code");
  392. if ( it_resultCode == resultMap.end() )
  393. {
  394. gs_strFailMsg = "支付失败,无结果码";
  395. return FALSE;
  396. }
  397. else
  398. {
  399. if ( _tcscmp( it_resultCode->second.toString().c_str(), "FAIL") == 0 )
  400. {
  401. gs_strFailMsg = "失败详情:";
  402. JsonMap::const_iterator it_erroCodeDes = resultMap.find("err_code_des");
  403. if ( it_erroCodeDes != resultMap.end() )
  404. gs_strFailMsg.append(it_erroCodeDes->second.toString());
  405. return FALSE;
  406. }
  407. }
  408. }
  409. else
  410. {
  411. JsonMap::const_iterator it_reurnMsg = resultMap.find("return_msg");
  412. gs_strFailMsg = "失败详情:";
  413. if (it_reurnMsg == resultMap.end())
  414. gs_strFailMsg.append(it_reurnMsg->second.toString());
  415. return FALSE;
  416. }
  417. return TRUE;
  418. #endif
  419. }