nodejs Alipay Interface

  1. Alipay Configuration
    1. File Read
    Here Insert Picture Description
    You need to generate a key software:Alipay open platform development assistantThis

FIG key provided above
Then directly generate the key
First haste to shut the software, but also to copy the public key applications
Here Insert Picture Description
When set to apply public key into them, save!
Here Insert Picture Description
Other settings temporarily set up
this way on the line!
2. Code
installation of the SDK methods alipay

npm i alipay-sdk --save
  1. Profiles
// 支付宝配置文件
// 文件读取 应用私钥
let appPrivateKey = fs.readFileSync(path.join(__dirname,'../sendbox_pem/private_app2048.txt'),'ascii')
// 支付宝 公钥
let alipayPublicKey = fs.readFileSync(path.join(__dirname,'../sendbox_pem/public_alipay.txt'),'ascii')
const alipayConfig = {
  // APPId 应用id
  appId:123225525, 
  // 应用私钥,我这里直接读取它生成的.txt文件
  privateKey:appPrivateKey,
  // 支付宝公钥,一样是读取的.txt文件
  alipayPublicKey:alipayPublicKey,
  // 支付宝网关
  gateway:'https://openapi.alipaydev.com/gateway.do',
  // 编码字符集
  charset:'utf-8',
  // 版本默认 1.0
  version:'1.0',
  // 加密方式,与自己的加密方式对应
  signType:'RSA2'
}
// 将配置 导出
module.exports = {
	alipayConfig 
}
  1. Add package orders Alipay method
// 导入sdk环境 注意这个default
const AlipaySDK = require('alipay-sdk').default
// 引入配置文件 , 路径看自己
const config = require('../config/config')
// 实例化对象
let alipay = new AlipaySDK(config.alipayConfig)
// PC支付接口 alipay.trade.page.pay 返回的内容为 表单
let AlipayFormData = require('alipay-sdk/lib/form').default

/**
 * 创建订单支付宝封装
 * 异步方法
 * @param {Object} goods  商品信息
 */
const createOrder = async (goods) => {
  // 设置调用的接口
  let method = 'alipay.trade.page.pay'
  // 设置公共参数 没使用,后面会尝试使用
  // let params = {
  //   app_id: '2016101500692746', /*应用的id*/
  //   method: method,/*调用接口*/
  //   format: 'JSON', /*返回数据*/
  //   charset: 'utf-8',/*字符编码*/
  //   timestamp: '',/*请求时间戳*/
  //   version: '1.0'/*版本*/
  // }

  // 根据官方给的api文档提供一个参数合集
  let bizContent = {
    out_trade_no: goods.orderNumber, //订单号 时间戳
    product_code: 'FAST_INSTANT_TRADE_PAY', // 商品码
    total_amount: goods.allTotal, // 商品价格
    subject: goods.orderName, // 订单名称
    timeout_express: '5m', // 超时时间
    passback_params: JSON.stringify(goods.pack_params) // 返回一个参数,用于自定义商品信息最后做通知使用
  }
  // 创建formData 对象
  const formData = new AlipayFormData()
  // 调用 setMethod 并传入 get,会返回可以跳转到支付页面的 url
  formData.setMethod('get')
  // 客户支付成功之后会同步跳回的地址 看自己的
  formData.addField('returnUrl','http://192.168.97.240:3000/public/') 
  // 支付宝在用户支付成功之后会异步通知的回调地址,必须在公网ip 才能收到 根据自己的网址
  formData.addField('notifyUrl','http://192.168.97.240:3000/public/')  
  // 将必要的参数集合添加进 form 表单
  formData.addField('bizContent', bizContent); 
  // 异步向支付宝 发送生成订单请求,第二个参数为公共参数,可以为空
  const result = await alipay.exec(method, {}, {
    formData: formData
  })
  console.log(result)
  return result
}
// 导出方法
module.exports = {
  createOrder
}
  1. Method uses
// 我的方法都封装在 common文件中 肯定是要先导入方法的
let common = require('../common/common')
// 在调用的时候传 从前端获得的商品信息 处理后 goods
let resultOfAlipay = await common.createOrder(goods)
// 将结果(url)返回
res.json({
        status: 200,
        result: resultOfAlipay,
        message: '添加订单成功'
      })

Achievement
Here Insert Picture Description

Encountered pit

  1. When introducing modules often Ignore default
// 导入sdk环境 注意这个default
const AlipaySDK = require('alipay-sdk').default
// PC支付接口 alipay.trade.page.pay 返回的内容为 表单
let AlipayFormData = require('alipay-sdk/lib/form').default
  1. Package add Alipay order method when selecting the type of return according to their needs
// 调用 setMethod 并传入 get,会返回可以跳转到支付页面的 url
  formData.setMethod('get')

Without this,Returns the type of form to form

  1. Operating results error 错误代码 invalid-app-id 错误原因: 无效的AppID参数
    1. Check the configuration fileFieldThere is no wrong
    2. Check the configuration file appId Application of the private key Alipay public Gateway Encryption is it right or not

reference:

  1. NodeJS access the site using Alipay payment Demo Development
  2. Ants gold dress open platform node sdk
Published 50 original articles · won praise 23 · views 1221

Guess you like

Origin blog.csdn.net/qq_44698161/article/details/103536167