• Stars
    star
    914
  • Rank 49,973 (Top 1.0 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

微信支付 Nodejs SDK

weixin-pay

微信支付 for node.js

npm version

Installation

npm install weixin-pay

Usage

创建统一支付订单

var WXPay = require('weixin-pay');

var wxpay = WXPay({
	appid: 'xxxxxxxx',
	mch_id: '1234567890',
	partner_key: 'xxxxxxxxxxxxxxxxx', //微信商户平台API密钥
	pfx: fs.readFileSync('./wxpay_cert.p12'), //微信商户平台证书
});

wxpay.createUnifiedOrder({
	body: '扫码支付测试',
	out_trade_no: '20140703'+Math.random().toString().substr(2, 10),
	total_fee: 1,
	spbill_create_ip: '192.168.2.210',
	notify_url: 'http://wxpay_notify_url',
	trade_type: 'NATIVE',
	product_id: '1234567890'
}, function(err, result){
	console.log(result);
});

查询订单

// 通过微信订单号查
wxpay.queryOrder({ transaction_id:"xxxxxx" }, function(err, order){
	console.log(order);
});

// 通过商户订单号查
wxpay.queryOrder({ out_trade_no:"xxxxxx" }, function(err, order){
	console.log(order);
});

关闭订单

wxpay.closeOrder({ out_trade_no:"xxxxxx"}, function(err, result){
	console.log(result);
});

退款接口

var params = {
	appid: 'xxxxxxxx',
	mch_id: '1234567890',
    op_user_id: '商户号即可',
    out_refund_no: '20140703'+Math.random().toString().substr(2, 10),
    total_fee: '1', //原支付金额
    refund_fee: '1', //退款金额
    transaction_id: '微信订单号'
};

wxpay.refund(params, function(err, result){
    console.log('refund', arguments);
});

原生支付 (NATIVE)

模式一

提供一个生成支付二维码链接的函数,把url生成二维码给用户扫。

var url = wxpay.createMerchantPrepayUrl({ product_id: '123456' });

商户后台收到微信的回调之后,调用 createUnifiedOrder() 生成预支付交易单,将结果的XML数据返回给微信。

什么是模式一?

模式二

直接调用 createUnifiedOrder() 函数生成预支付交易单,将结果中的 code_url 生成二维码给用户扫。

什么是模式二?

公众号支付 (JS API)

生成JS API支付参数,发给页面

wxpay.getBrandWCPayRequestParams({
	openid: '微信用户 openid',
	body: '公众号支付测试',
    detail: '公众号支付测试',
	out_trade_no: '20150331'+Math.random().toString().substr(2, 10),
	total_fee: 1,
	spbill_create_ip: '192.168.2.210',
	notify_url: 'http://wxpay_notify_url'
}, function(err, result){
	// in express
    res.render('wxpay/jsapi', { payargs:result })
});

网页调用参数(以ejs为例)

WeixinJSBridge.invoke(
	"getBrandWCPayRequest", <%-JSON.stringify(payargs)%>, function(res){
		if(res.err_msg == "get_brand_wcpay_request:ok" ) {
    		// success
    	}
});

中间件

商户服务端处理微信的回调(express为例)

// 原生支付回调
router.use('/wxpay/native/callback', wxpay.useWXCallback(function(msg, req, res, next){
	// msg: 微信回调发送的数据
}));

// 支付结果异步通知
router.use('/wxpay/notify', wxpay.useWXCallback(function(msg, req, res, next){
	// 处理商户业务逻辑

    // res.success() 向微信返回处理成功信息,res.fail()返回失败信息。
    res.success();
}));

More Repositories

1

weixin-redpack

微信红包 (wechat redpack, weixin redpack)
JavaScript
39
star
2

egg-react-mobx-ts-antd

Start node with eggjs, react, mobx-react, typescript and ant design.
JavaScript
35
star
3

atom-icons

The awesome icons for Atom
CSS
15
star
4

mobx-react-connect

Connect react component, mobx store and css modules.
JavaScript
12
star
5

promise

Promise +
JavaScript
11
star
6

inbox

10
star
7

oss-action

Upload files to OSS
JavaScript
7
star
8

lego-react-mobx-antd

一颗脚手架
JavaScript
7
star
9

webpack-compile-loop

High efficient compiling experience
JavaScript
3
star
10

hq

流处理中间件
JavaScript
3
star
11

blackbox

Chrome Extension
JavaScript
2
star
12

collect

数据采集
JavaScript
2
star
13

cowtransfer-action

Upload by CowTransfer
Shell
2
star
14

docker-builder

A github action to build and push docker image
Shell
2
star
15

umi-plugin-electron

Umi plugin to build electron app
TypeScript
1
star
16

github-stats

Python
1
star
17

dataware

数据套件
JavaScript
1
star
18

docker-library

docker library
Dockerfile
1
star
19

file-upload-app

JavaScript
1
star
20

easy-cmd

Easy to build CLI
JavaScript
1
star
21

create

Project boilerplates
JavaScript
1
star
22

egg-function

Function framework based on eggjs
JavaScript
1
star
23

fsp

First Screen Paint
TypeScript
1
star
24

stage

任务调度
JavaScript
1
star
25

commitlint-config

JavaScript
1
star
26

alfredtweet

tweet via alfred
1
star
27

egg-ws

Websocket plugin for egg
1
star
28

fx

工具集
JavaScript
1
star
29

fengzheng

1
star
30

docker-node-launch

Node running envirement
Shell
1
star
31

docker-mongo-sharded-cluster

Mongodb sharded cluster based on docker
1
star
32

hyper-app

Easy to build app
JavaScript
1
star
33

egg-cloud

Cloud service plugin for egg.
1
star
34

antd-table

Table Component
JavaScript
1
star
35

homebridge

连接 Apple Homekit 和小米智能家居
Dockerfile
1
star
36

injection

An IoC toolkit
TypeScript
1
star
37

bee

变态的小蜜蜂
JavaScript
1
star
38

docker-shadowsocks

Zero configuration shadow
Dockerfile
1
star