推拿小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.3 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  1. // #ifdef H5
  2. import jWeixin from './lib/jweixin-module.js'
  3. // #endif
  4. /**
  5. * 调用微信支付
  6. * @param {Object} res - 支付参数对象包含appIdtimeStampnonceStr等必要信息
  7. * @returns {Promise} - 返回Promise对象resolve表示支付成功reject表示支付失败
  8. */
  9. export function wxPay(res) {
  10. return new Promise((resolve, reject) => {
  11. // 配置微信JSSDK
  12. jWeixin.config({
  13. debug: false,
  14. appId: res.result.appId, //必填,公众号的唯一标识
  15. jsApiList: ['chooseWXPay'] //必填,需要使用的JS接口列表
  16. });
  17. // JSSDK配置成功后的回调
  18. jWeixin.ready(function() {
  19. // 调用微信支付接口
  20. jWeixin.chooseWXPay({
  21. appId: res.result.appId,
  22. timestamp: res.result.timeStamp, // 支付签名时间戳
  23. nonceStr: res.result.nonceStr, // 支付签名随机串
  24. package: res.result.packageValue, // 统一支付接口返回的prepay_id参数值
  25. signType: res.result.signType, // 签名类型,默认为MD5
  26. paySign: res.result.paySign, // 支付签名
  27. success: function(result) {
  28. resolve(result);
  29. },
  30. fail: function(error) {
  31. reject(error);
  32. },
  33. cancel: function(error) {
  34. reject({ type: 'cancel', ...error });
  35. }
  36. });
  37. });
  38. // JSSDK配置失败处理
  39. jWeixin.error(function(error) {
  40. reject({ type: 'config_error', ...error });
  41. });
  42. });
  43. }