const cloud = require('wx-server-sdk')
const axios = require('axios')
const config = require('../config')
const Guid = require('guid')
const fs = require('fs')
const rsa = require('../rsa')
const PRIMARY_KEY = fs.readFileSync('../apiclient_key.pem')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
exports.main = async (event, context) => {
function makeSignStr(...args) {
return args.join('\n') + '\n'
}
function makeSignMes(obj) {
return Object.keys(obj).map(key => `${key}="${obj[key]}"`).join()
}
let href = 'https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no'
let url = new URL(`${href}/${event.outTradeNo}?mchid=${config.mchid}`)
let timestamp = Math.floor(+new Date / 1000).toString()
let nonceStr = Guid.raw().replace(/-/g, '')
let signStr = makeSignStr('GET', url.pathname + url.search, timestamp, nonceStr,"")
let signature = rsa.sign(signStr, PRIMARY_KEY)
let signMes = makeSignMes({
mchid: config.mchid,
"serial_no": config.serial_no,
"nonce_str": nonceStr,
timestamp,
signature
})
const { data } = await axios.get(url.href, {
headers: {
"Accept": 'application/json',
"Authorization": 'WECHATPAY2-SHA256-RSA2048 '+ signMes
}
})
console.log(data)
return data
}
404是订单不存在,检查一下给的订单号对不对