befinal / node-tenpay Goto Github PK
View Code? Open in Web Editor NEW微信支付 for nodejs
License: MIT License
微信支付 for nodejs
License: MIT License
try{
const result = await api.refund(order);
console.log('result', result);
} catch (err) {
console.log(err);
}
大神,上周getPayParams 方法还可以返回支付参数,今天直接返回 INVALID_REQUEST 了,公众号配置也没动过,微信改啥了吗?
downloadbill 接口返回的数据 退款记录 refund_status = 'PROCESSING' , 一直不更新,这是微信返回的,比较坑,还需要再去查询一次,修改状态吗?
// JSSDK支付签名(原始方法)
Payment.prototype._getPayParams = function(params, callback) {
var that = this;
that.unifiedOrder(params, function(err, result) {
if (err) return callback(err);
var pkg = Object.assign({}, {
appId: that.appid,
timeStamp: '' + (Date.now() / 1000 |0),
nonceStr: util.generateNonceStr(),
package: 'prepay_id=' + result.prepay_id,
signType: 'MD5'
});
pkg.paySign = that.getSign(pkg);
pkg.timestamp = pkg.timeStamp;
callback(null, pkg);
})
};
谢谢分享,请问现在支持退款通知吗,和支付通知的配置一样吗。
提供部分退款通知消息解密的方法。
`// xml 解析
var parseXML = function (xml, callback) {
xml2js.parseString(xml, {
trim: true,
explicitArray: false
}, function (err, result) {
err ? callback(err) : callback(null, result ? result.root : {});
});
};
// 解密
var decrypt = function (text, trade_type) {
var result = '';
if (trade_type == 'APP') {
let key1 = CryptoJS.MD5(partner_key);
result = CryptoJS.AES.decrypt(text, CryptoJS.enc.Utf8.parse(key1), {
mode: CryptoJS.mode.ECB
});
}
return result.toString(CryptoJS.enc.Utf8);
};`
api.refund()调用的时候会报错“sign签名错误”,但是在微信退款查询里发现,刚才的退款已经成功了。
server error Error: ORDERPAID
at Payment._parse (E:\nodeproject\everprint\node_modules_tenpay@2.1.6@tenpay\lib\index.js:74:51)
at
at process._tickCallback (internal/process/next_tick.js:188:7) { request: ~~~~~
这个是已经支付过了的意思吗?是腾讯返回的吗?
res.reply('success');
res.reply('fail');
都不行,微信还是会不停的回调过来
你好,尝试使用了统一下单,还有查询订单,都没问题。郁闷:(
api.refund({
out_trade_no: order._id,
out_refund_no: refund._id,
total_fee: 1,
refund_fee: 1
})
//值都是正确的
报错
server error { AssertionError [ERR_ASSERTION]: invalid status code: -1
at Object.set status [as status] (D:\document\takeout-api\node_modules\koa\lib\response.js:86:5)
at Object.status (D:\document\takeout-api\node_modules\delegates\index.js:92:31)
at D:\document\takeout-api\middlewares\error-handling.js:9:18
at
at process._tickCallback (internal/process/next_tick.js:188:7)
generatedMessage: false,
name: 'AssertionError [ERR_ASSERTION]',
code: 'ERR_ASSERTION',
actual: undefined,
expected: true,
operator: '==' } { request:
{ method: 'POST',
url: '/order/cancel',
header:
{ host: 'localhost:4000',
connection: 'keep-alive',
'content-length': '33',
pragma: 'no-cache',
'cache-control': 'no-cache',
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1 wechatdevtools/1.02.1806120 MicroMessenger/6.5.7 Language/zh_CN webview/',
origin: 'http://127.0.0.1:26462',
authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoiU3N2ZnhpbmRHZWtITXM4cVVQZit2QT09IiwiaWF0IjoxNTI5MDI3NTk0LCJleHAiOjE1MjkyODY3OTR9.Zx7r9-2RQXC02IWsWYiFSh6V4uV243U5xfJvKcQa-zM',
'content-type': 'application/json; charset=UTF-8',
accept: '/',
referer: 'https://servicewechat.com/wx67518436f322a9c4/devtools/page-frame.html',
'accept-encoding': 'gzip, deflate, br' } },
response:
{ status: 404,
message: 'Not Found',
header:
{ 'x-dns-prefetch-control': 'off',
'x-frame-options': 'SAMEORIGIN',
'strict-transport-security': 'max-age=15552000; includeSubDomains',
'x-download-options': 'noopen',
'x-content-type-options': 'nosniff',
'x-xss-protection': '1; mode=block',
vary: 'Origin',
'access-control-allow-origin': 'http://127.0.0.1:26462' } },
app: { subdomainOffset: 2, proxy: false, env: 'development' },
originalUrl: '/order/cancel',
req: '',
res: '',
socket: '' }
报这个错误;
pc版微信支付的话这个支持吗,有没有思路的
能收到请求,但是没有数据;有使用中间件的例子么?
看这里看这里看这里,https://github.com/cezerin
micropay: 扫码支付 ? 看 lib/index.js 下的注释。。不像
// 扫码支付, 生成URL(模式一)
getNativeUrl(params) {
let pkg = {
...params,
appid: this.appid,
mch_id: this.mchid,
time_stamp: '' + (Date.now() / 1000 |0),
nonce_str: util.generate()
};
let url = 'weixin://wxpay/bizpayurl'
+ '?sign=' + this._getSign(pkg)
+ '&appid=' + pkg.appid
+ '&mch_id=' + pkg.mch_id
+ '&product_id=' + pkg.product_id
+ '&time_stamp=' + pkg.time_stamp
+ '&nonce_str=' + pkg.nonce_str;
return url;
}
// 刷卡支付
micropay(params) {
let pkg = {
...params,
appid: this.appid,
mch_id: this.mchid,
nonce_str: util.generate(),
sign_type: params.sign_type || 'MD5',
spbill_create_ip: params.spbill_create_ip || this.spbill_create_ip
};
return this._request(pkg, 'micropay');
}
您好,没看到授权,组织包,计算签名那步在哪呢
{"code":-1,"reason":"/data/release/node-weapp-demo/node_modules/tenpay/lib/index.js:52\n ...config,\n ^^^\n\nSyntaxError: Unexpected token ...\n at createScript (vm.js:74:10)\n at Object.runInThisContext (vm.js:"}
// 申请退款 await api.refund(refund_order).then(rs => { // console.log('>>>>>>退款结果 result is :', JSON.stringify(rs)) if (rs.return_code == 'SUCCESS' && rs.result_code == 'SUCCESS') { // 获取之前充值押金的的价格 const refund_data = { user_id: user_id, wallet_id: wallet_id, out_trade_no: out_trade_no, out_refund_no: out_refund_no, reason: reason, refund_price: order.finally_price } console.log('>>>>>>退款结果1 result is :', JSON.stringify(rs)) // 退款表 return WalletDepositRefundModel.create(refund_data) } }).catch(err => { // 日志 console.log('>>>>>>退款结果3 result is :', JSON.stringify(err)) })
谢谢楼主分享,现在有个小疑问是不是then里面不需要判断 return_code,result_code的状态,都是SUCCESS,所有的错误都在 catch 里面捕捉了。我看了util.js 里面的validate方法,好像需要return_code,result_code 都是SUCCESS才是正确的。我测试了一下也是这样,再来确认下。
请问支付通知中间件怎么回复消息?
如果我收通知并且成功是回复ctx.reply('') 吗,怎么微信服务器好像一直在通知?
如题,在 sails 中使用,非 express 和 koa, 是否能直接调用 api
let result = await api.getPayParams({
out_trade_no: Date.now(),
body: '商品简单描述',
total_fee: '1',
sub_openid: subOpenid,
sub_appid: subAppid,
sub_mch_id: subMchid
});
小程序中调用支付结果错误
{err_desc: "Unable to verify signature", err_code: "-1", errMsg: "requestPayment:fail"}
有没有事例代码,解密一直不正确
/**
* 生成直接支付url,支付url有效期为2小时,模式二
* @param productId 商品ID
* @return 模式二URL
*/
public string GetPayUrl(string productId)
{
Log.Info(this.GetType().ToString(), "Native pay mode 2 url is producing...");
WxPayData data = new WxPayData();
data.SetValue("body", "test");//商品描述
data.SetValue("attach", "test");//附加数据
data.SetValue("out_trade_no", WxPayApi.GenerateOutTradeNo());//随机字符串
data.SetValue("total_fee", 1);//总金额
data.SetValue("time_start", DateTime.Now.ToString("yyyyMMddHHmmss"));//交易起始时间
data.SetValue("time_expire", DateTime.Now.AddMinutes(10).ToString("yyyyMMddHHmmss"));//交易结束时间
data.SetValue("goods_tag", "jjj");//商品标记
data.SetValue("trade_type", "NATIVE");//交易类型
data.SetValue("product_id", productId);//商品ID
WxPayData result = WxPayApi.UnifiedOrder(data);//调用统一下单接口
string url = result.GetValue("code_url").ToString();//获得统一下单接口返回的二维码链接
Log.Info(this.GetType().ToString(), "Get native pay mode 2 url : " + url);
return url;
}
怎么调用接口都返回null呢,用的是function回调
npm WARN deprecated [email protected]: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0
能否提供对应d.ts文件支持Typescript使用?
router.post('/', function (req, res, next) {
let openid = req.body.openid
let products = req.body.products
let totalAmount = req.body.totalAmount
let address = req.body.address
Customer.findOne({openid:openid}, function(err, customer){
if(customer){
let order = new Order({
user: customer._id,
phone: customer.phone,
items: products,
totalAmount: totalAmount,
shippingAddress: address,
status: 'created',
createdAt: new Date().getTime(),
updatedAt: new Date().getTime()
})
order.save((err, order)=>{
if(order){
let result = unifiedOrder(order._id, '服装', totalAmount,customer.openId)
console.log(result)
return res.send(result)
}
})
}
})
})
async function unifiedOrder(orderId, name, totalAmount, openid){
try {
let result = await wxpay.unifiedOrder({
out_trade_no: orderId,
body: name,
total_fee: totalAmount,
openid: openid
});
console.log(result)
} catch (error) {
console.error(error);
}
}
Error Console
Promise { <pending> } Error: XML格式错误 at Payment._parse (/Users/haochen/wechat/kunkka/node_modules/tenpay/lib/index.js:43:47) at <anonymous> at process._tickCallback (internal/process/next_tick.js:160:7)
不太清楚那里出错了 才会导致返回xml格式错误?在local做测试时返回的
{ResponseError: socket hang up (req "error"), POST https://api.mch.weixin.qq.com/secapi/pay/refund -1 (connected: true, keepalive socket: false)
headers: {}
at createHangUpError (_http_client.js:331:15)
at TLSSocket.socketOnEnd (_http_client.js:423:23)
at emitNone (events.js:111:20)
at TLSSocket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
From previous event:
at Payment.request (C:\project\nextown\nodejs-nextown-api\node_modules\tenpay\lib\index.js:282:10)
at Payment.refund (C:\project\nextown\nodejs-nextown-api\node_modules\tenpay\lib\index.js:136:15)
at Finance.refundDispatchOrder (C:\project\nextown\nodejs-nextown-api\src\models\finance.js:40:41)
at <anonymous>
code: 'ECONNRESET',
name: 'ResponseError',
data: undefined,
path: '/secapi/pay/refund',
status: 500,
headers: {},
res:
{ status: -1,
statusCode: 500,
headers: {},
size: 0,
aborted: false,
rt: 216,
keepAliveSocket: false,
data: undefined,
requestUrls: [ 'https://api.mch.weixin.qq.com/secapi/pay/refund' ],
timing: null,
remoteAddress: '182.254.78.168',
remotePort: 443 } }
在调用退款接口时出现如下错误,请怎么回事啊
tenpay版本:1.1.4
node版本:8.11.1
npm run dev报以下错误:
Module parse failed: Unexpected token (196:6)
You may need an appropriate loader to handle this file type.
| micropay(params) {
| let pkg = {
| ...params,
| appid: this.appid,
| mch_id: this.mchid,
@ ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/page/reg_order_confirm.vue 33:19-36
@ ./src/page/reg_order_confirm.vue
@ ./src/router/index.js
@ ./src/main.js
@ multi (webpack)-dev-server/client?http://0.0.0.0:
企业付款,接口报错 err:mchid不匹配,但是实际是成功的,楼主看下呢。
为啥没有企业付款到银行卡接口
微信的文档说明:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_7&index=8
微信支付通知xml数据中有个result_code字段为FAIL时,表示用户支付未成功。这个未成功的消息,业务服务器应该有权知道。
但是tenpay的支付通知中间件,在这种情况下会返回给微信支付服务器return res.reply('XMLDataError'),暗示业务服务器未收到消息,而不是收到消息。
总感觉这在逻辑上是错的。
你好,
感谢你这个lib,非常棒 🙏
想要实现企业付款的功能,但是在用 api.transfers的时候遇到一个错误,不知道是什么问题:
config是这样初始化的:
const config = {
appid: process.env.WX_APP_ID,
mchid: process.env.WX_MCHID,
partnerKey: process.env.WX_PAY_SECRET, // 微信商户平台 API secret,非小程序 secret
notify_url: process.env.WX_NOTIFY_URL,
pfx: require('fs').readFileSync('./apiclient_cert.pem')
}
const wxpay = WXPay.init(config)
然后在我的routes里面这样使用:
app.route('/test-transfer')
.post(async ({ body: { openid, fullname } }, res, next) => {
const partner_trade_no = _generateOutTradeNo()
try {
let result = await wxpay.transfers({
partner_trade_no,
openid,
re_user_name: fullname,
amount: 1,
desc: '扫街费'
})
console.log(result)
res.json({ result })
} catch (error) {
console.log(error)
next(error)
}
})
返回的错误是这样的:
Error: wrong tag, POST https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers -1 (connected: false, keepalive socket: false) headers: {}
if (json.return_code !== 'SUCCESS') throw new Error(json.return_msg || 'XMLDataError');
if (type !== 'middleware_refund') {
if (json.result_code !== 'SUCCESS') throw new Error(json.err_code || 'XMLDataError');
}
代码里,交易过程中出错了之后,直接throw了Error,这样json里返回的微信返回却看不到了,是不是直接callback(null, json)返回就好呢,让大家自己来处理这些错误,因为有时候是想要记录这些错误并处理的
微信支付结果通知 • 中间件
// Express
var middleware = api.middlewareForExpress();
app.use('/xxx', middleware, function (req, res) {
var payInfo = req.weixin;
})
请问 /xxx 与 notify_url - 支付结果通知回调地址是一致的吗?
谢谢
能否增加给APP的生成签名sign的api呢,
自己回答:好像在order里指定trade_type: 'APP',删除openid后,直接用getPayParams就可以调用。
lib/index.js的182行,getAppParamsByPrepay方法中,微信支付文档的随机字符串字段为:noncestr,而方法中为nonceStr,会导致微信后台在检查签名的时候报错~
lib\index.js
switch (type) { case 'middleware_refund': if (json.appid !== this.appid) throw new Error('appid不匹配'); if (json.mch_id !== this.mchid) throw new Error('mch_id不匹配'); let key = util.md5(this.partnerKey).toLowerCase(); let info = util.decrypt(json.req_info, key); json.req_info = await util.parseXML(info); break; case 'transfers': if (json.mchid !== this.mchid) throw new Error('mchid不匹配'); break; case 'sendredpack': case 'sendgroupredpack': if (json.wxappid !== this.appid) throw new Error('wxappid不匹配'); if (json.mch_id !== this.mchid) throw new Error('mchid不匹配'); break; case 'gethbinfo': case 'gettransferinfo': if (json.mch_id !== this.mchid) throw new Error('mchid不匹配'); break; default: if (json.appid !== this.appid) throw new Error('appid不匹配'); if (json.mch_id !== this.mchid) throw new Error('mch_id不匹配'); if (json.sign !== this._getSign(json)) throw new Error('sign签名错误'); } return json;
微信官方都没有req_info 这个字段,带req_info 的这行代码报错,怎么回事?
https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
官方文档并没有写清楚,但是测试时发送大于2元时就已经需要 scene_id 了,毕竟最大红包金额也只是 200 元。或者代码不需要做判断处理,调用时有传入参数则传到接口。
Lines 339 to 354 in 8542215
另外错误判断只针对特定错误,导致其他出错情况下不会 throw error,比如后台没有配置
{
"return_msg": "参数错误:该场景未开通,请登陆商户平台:产品中心-现金红包-产品设置-使用场景 进行开通",
"result_code": "FAIL",
"err_code": "PARAM_ERROR",
"err_code_des": "参数错误:该场景未开通,请登陆商户平台:产品中心-现金红包-产品设置-使用场景 进行开通"
}
请问有 App 支付回调 处理的例子吗 只看到了 中间件相关的 如果 没有中间件 如何处理呢
在计算字符串md5时需要指定编码为utf8
Payment.prototype.getSign = function(pkg) {
pkg = Object.assign({}, pkg);
delete pkg.sign;
var str = ulits.toQueryString(pkg) + '&key=' + this.partnerKey;
return crypto.createHash('md5').update(str, 'utf8').digest('hex').toUpperCase();
};
getAppParamsByPrepay(params) {
let pkg = {
appid: this.appid,
partnerid: this.mchid,
prepayid: params.prepay_id,
package: 'Sign=WXPay',
nonceStr: util.generate(),
timestamp: '' + (Date.now() / 1000 |0)
};
pkg.sign = this._getSign(pkg);
return pkg;
}
这里的nonceStr应该是noncestr。希望改正
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.