GithubHelp home page GithubHelp logo

wechatpay's Introduction

Build Status Coverage Status MIT license

安装

npm i -S @ycnt/wechatpay

yarn add @ycnt/wechatpay

使用方法

配置

import { Wechatpay } from '@ycnt/wechatpay';

const wechatpay = new Wechatpay({
  appid: 'xxxx',
  mch_id: 'xxxx',
  apiKey: 'xxxx', // 微信商户平台API密钥
  pfx: readFileSync(resolve('xxxx/apiclient_cert.p12')),
});

创建订单

const orderParams: IOrderParams = {
  body: '支付测试', // 商品或支付单简要描述
  out_trade_no: 'xxx', // 商户系统内部的订单号,32个字符内、可包含字母
  total_fee: 100, // 价格单位为分
  spbill_create_ip: '127.0.0.1', // 客户IP地址
  notify_url: 'http://wxpayment_notify_url', // 回调通知接口URL
  trade_type: 'APP', // APP, JSAPI, NATIVE etc.
};

const order = await wechatpay.createUnifiedOrder(orderParams);

创建支付对象

支付对象是客户端用于发起支付的凭据

const payment = wechatpay.configForPayment(order);

查寻订单

const queryOrder = await wechatpay.queryOrder({
  out_trade_no: 'xxx',
});

关闭订单

const closeOrder = await wechatpay.closeOrder({
  out_trade_no: 'xxx',
});

退款

const refundOrder = await wechatpay.refund({
  out_trade_no: 'xxx',
  out_refund_no: 'xxx', // 退款流水号,同一流水号多次退款只会处理一次
  total_fee: 100, // 订单总额
  refund_fee: 100, // 退款金额
});

退款查寻

const queryRefund = await wechatpay.queryRefund({
  out_trade_no: 'xxx',
});

反馈信息验证

支付成功后微信给后台反馈的信息为XML格式。 为了安全,我们需要对信息进行验证。

const xml = `微信�通知的内容`
const obj = `转xml为Object`
const isSignOk = wechatpay.signVerify(obj);

给微信返回结果

微信反馈信息后,我们需要�回复成功或失败,格式为XML。 不然微信会持续发送反馈信息。 下面两个方法会�创建XML格式的回复信息。

const success: string = wechatpay.success(); // 成功XML信息
const fail: string = wechatpay.fail(); // 失败XML信息

wechatpay's People

Contributors

kuyoonjo avatar hcy123123 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.