wxpay.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * 微信支付相关函数
  3. */
  4. var wxpay = {
  5. async getAccessToken(data) {
  6. let {
  7. appId,
  8. secret,
  9. force_refresh,
  10. } = data;
  11. let res = await uniCloud.httpclient.request("https://api.weixin.qq.com/cgi-bin/stable_token", {
  12. method: 'POST',
  13. data: {
  14. appid: appId,
  15. secret: secret,
  16. grant_type: "client_credential",
  17. force_refresh
  18. },
  19. contentType: 'json',
  20. dataType: 'json' ,
  21. });
  22. let result = res.data || {};
  23. if (!result.access_token) {
  24. return {
  25. errCode: -1,
  26. errMsg: result.errmsg
  27. }
  28. }
  29. return {
  30. errCode: 0,
  31. errMsg: 'ok',
  32. accessToken: result.access_token,
  33. expiresIn: result.expires_in
  34. }
  35. },
  36. async getOpenid(data) {
  37. let {
  38. provider_pay_type
  39. } = data;
  40. if (provider_pay_type === "jsapi") {
  41. return this._getJsOpenid(data);
  42. } else {
  43. return this._getMpOpenid(data);
  44. }
  45. },
  46. async _getMpOpenid(data) {
  47. let {
  48. config = {},
  49. code,
  50. provider_pay_type
  51. } = data;
  52. if (!config.appId) throw new Error("uni-pay配置wxpay.mp节点下的appId不能为空");
  53. if (!config.secret) throw new Error("uni-pay配置wxpay.mp节点下的secret不能为空");
  54. let res = await uniCloud.httpclient.request("https://api.weixin.qq.com/sns/jscode2session", {
  55. method: 'GET',
  56. data: {
  57. appid: config.appId,
  58. secret: config.secret,
  59. js_code: code,
  60. grant_type: "authorization_code"
  61. },
  62. contentType: 'json', // 指定以application/json发送data内的数据
  63. dataType: 'json' // 指定返回值为json格式,自动进行parse
  64. });
  65. if (res.data && !res.data.errcode && res.data.openid) {
  66. return {
  67. errCode: 0,
  68. errMsg: 'ok',
  69. openid: res.data.openid,
  70. unionid: res.data.unionid,
  71. session_key: res.data.session_key, // 此参数不可以暴露给前端
  72. }
  73. } else {
  74. return {
  75. errCode: -1,
  76. errMsg: res.data.errmsg
  77. }
  78. }
  79. },
  80. async _getJsOpenid(data) {
  81. let {
  82. config = {},
  83. code,
  84. provider_pay_type
  85. } = data;
  86. if (!config.appId) throw new Error("uni-pay配置wxpay.jsapi节点下的appId不能为空");
  87. if (!config.secret) throw new Error("uni-pay配置wxpay.jsapi节点下的secret不能为空");
  88. let res = await uniCloud.httpclient.request("https://api.weixin.qq.com/sns/oauth2/access_token", {
  89. method: 'GET',
  90. data: {
  91. appid: config.appId,
  92. secret: config.secret,
  93. code: code,
  94. grant_type: "authorization_code"
  95. },
  96. contentType: 'json', // 指定以application/json发送data内的数据
  97. dataType: 'json' // 指定返回值为json格式,自动进行parse
  98. });
  99. if (res.data && !res.data.errcode && res.data.openid) {
  100. return {
  101. errCode: 0,
  102. errMsg: 'ok',
  103. openid: res.data.openid,
  104. unionid: res.data.unionid,
  105. }
  106. } else {
  107. return {
  108. errCode: -1,
  109. errMsg: res.data.errmsg
  110. }
  111. }
  112. }
  113. };
  114. module.exports = wxpay;