egg-alipay-crypto

1.0.0 • Public • Published

egg-alipay-crypto

扩展支持支付宝加解密 nodejs 版本的 egg 插件

NPM version Codacy Badge Known Vulnerabilities npm download License

Sonar

Install

# use pnpm
$ pnpm install egg-alipay-crypto

# use yarn
$ yarn add egg-alipay-crypto

Usage

// {app_root}/config/plugin.js
exports.alipaycrypto = {
  enable: true,
  package: 'egg-alipay-crypto'
}

Configurations

egg-alipay-crypto support all alipay-crypto's configurations, check alipay-crypto documents to get more information.

// {app_root}/config/config.default.js
exports.alipaycrypto = {
  privateKey: ''
}

Use in service

// {app_root}/app/service/home.js
const { Service } = require('egg')

class HomeService extends Service {
  async index() {
    const { ctx, app } = this
    const data = {
      app_id: '20135234674',
      method: 'alipay.system.oauth.token',
      sign_type: 'RSA2',
      version: '1.0',
      charset: 'utf-8',
      timestamp: '2023-07-29 14:50:22',
      grant_type: 'authorization_code',
      biz_content: ''
    }

    const initial = ctx.serializedParams(data) // 'app_id=20135234674&charset=utf-8&grant_type=authorization_code&method=alipay.system.oauth.token&sign_type=RSA2&timestamp=2023-07-29 14:50:22&version=1.0'
    const sign = ctx.alipaySign(initial) // or => const sign = ctx.alipaySign(data)
    const md5_result = ctx.md5(data)
    // or
    const initial = app.alipaycrypto.serializedParams(data)
    const sign = app.alipaycrypto.encrypt(initial)
    const md5_result = app.alipaycrypto.md5(data)
  }
}

module.exports = HomeService

Change logs

Change logs

Questions & Suggestions

Please open an issue here.

License

MIT

Package Sidebar

Install

npm i egg-alipay-crypto

Weekly Downloads

13

Version

1.0.0

License

MIT

Unpacked Size

7.67 kB

Total Files

6

Last publish

Collaborators

  • saqqdy