
import path = require('path');
import fs = require('fs');
const Alipay = require('alipay-mobile').default
/*
包名：com.bantu.sgzzyz
APPID：2021002142639438
*/

//notify_url: 异步通知url
//app_id: 开放平台 appid
//appPrivKeyFile: 你的应用私钥
//alipayPubKeyFile: 蚂蚁金服公钥
const config = {
    app_id: '2021002142639438',
    appPrivKeyFile: fs.readFileSync(path.resolve(__dirname, '../../../../../key/alipay/rsa_private_key.pem')),
    alipayPubKeyFile: fs.readFileSync(path.resolve(__dirname, '../../../../../key/alipay/rsa_public_key.pem'))
}


const service = new Alipay(config)

/**
   * 支付宝下单
   *
   * @param {number} price 订单总金额，单位为元，精确到小数点后两位
   * @param {string} message 商品信息
   * @param {string} localOrderID 本地订单号
   *
   */
export async function applyOrderALI(localOrderID: string, price: number, message: string) {
    const data = {
        subject: message,
        out_trade_no: localOrderID,
        total_amount: price.toFixed(2).toString()
    }
    console.log('applyOrderALI :', data)
    let result = await service.createOrder(data)

    // { code: 0,
    //     message: '请求成功',
    //     data:
    //      'app_id=2021002142639438&biz_content=%7B%22subject%22%3A%22%E8%B5%B5%E4%BA%91%E4%BC%A0%E8%B4%AD%E4%B9%B0%E5%85%83%E5%AE%9D60%22%2C%22out_trade_no%22%3A%22bwnNLGfdCylflxKf54bEpcwyEbq7xygp%22%2C%22total_amount%22%3A%226.00%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=utf-8&method=alipay.trade.app.pay&sign_type=RSA2&timestamp=2021-05-12%2018%3A40%3A25&version=1.0&sign=S4U6JzSUiCSMvAbXeiJ2m2AQ%2BfCSr4VE4VNN9QEZtplgWIO51HIGhL24BpR4aj7mMfWO%2BqaWQnU0Fe8Uki960smBdj0%2FDpuztA7%2BIBWHK1Qj69%2FLhDNLoHZgO41jQJjIOVI0gWt1KaDlG2%2B52ghswqcZN%2FkFLwfMZCMEph3eg3aofq1mmwRbqAh8Sepoy9nCd24CmMUIbNe%2FmcxVg3xvYcWXnneWWHu%2FFbnaeZtynoHVhXqqhjFZSrxdk8lo5qyTXkE1ZoUrhJz918E2rArwzZeUL09ncPshHHvX6AygXp5BtV4clwTY0OY5yzOEXboluR5YmSaVXn0VkTWJZCTWTQ%3D%3D' 
    // }
    console.log('applyOrderALI result: ', result)
    if (result.code == 0) {
        return { code: 0, data: result.data };
    }
    return { code: -1, data: JSON.stringify(result) };
}



/**
   * 支付宝查询支付结果
   *
   * @param {number} serverId 区Id
   * @param {string} roleId 角色Id
   * @param {string} localOrderID 本地订单号
   *
   */
export async function checkOrderALI(localOrderID: string) {
    let data = {
        out_trade_no: localOrderID,
    }
    console.log('checkOrderALI :', data)

    let result = await service.queryOrder(data)
    console.log('checkOrderALI result: ', result)
    // {
    //     0|app  |   code: '0',
    //     0|app  |   data: {
    //     0|app  |     code: '10000',
    //     0|app  |     msg: 'Success',
    //     0|app  |     buyer_logon_id: '525***@qq.com',
    //     0|app  |     buyer_pay_amount: '0.00',
    //     0|app  |     buyer_user_id: '2088302819007765',
    //     0|app  |     invoice_amount: '0.00',
    //     0|app  |     out_trade_no: 'JbyWyR1SIVIw9ArxvHAgte6tmnIzSRea',
    //     0|app  |     point_amount: '0.00',
    //     0|app  |     receipt_amount: '0.00',
    //     0|app  |     send_pay_date: '2019-09-29 18:32:13',
    //     0|app  |     total_amount: '1.00',
    //     0|app  |     trade_no: '2019092922001407760553648380',
    //     0|app  |     trade_status: 'TRADE_SUCCESS'
    //     0|app  |   },
    //     0|app  |   message: 'success'
    //     0|app  | }
    if ((result.code == '0') && (result.message == "success")) {
        return { code: 1, data: result.data };
    }
    return { code: 0, data: JSON.stringify(result) };
}