wx_pay

Crates.iowx_pay
lib.rswx_pay
version0.2.3
sourcesrc
created_at2022-08-19 03:50:57.739926
updated_at2024-10-06 10:31:09.151047
description微信支付 api
homepage
repository
max_upload_size
id648516
size49,557
(zomem)

documentation

README

注:当前为测试版

微信支付,rust api

目前版本 apiv3 jsapi

let wx_pay = WxPay {
    appid: WECHAT_MINI_APP_ID,
    mchid: WECHAT_PAY_MCH_ID,
    private_key: WECHAT_PRIVATE_KEY,
    serial_no: WECHAT_PAY_SERIAL,
    apiv3_private_key: WECHAT_PAY_APIV3,
    notify_url: WECHAT_PAY_NOTIFY_URL,
};

jsapi 支付,返回客户端的支付参数信息

    wx_pay.jsapi

微信支付订单号查询订单

    wx_pay.get_transactions_by_id

商户订单号查询订单

    wx_pay.get_transactions_by_out_trade_no

关闭订单

    wx_pay.close

退款申请

    wx_pay.refund

查寻单笔退款

    wx_pay.get_refund

后台接口,以actix-web为例

use wx_pay::{decode_wx_pay, Amount, Jsapi, Payer, WxPayData, WxPay, WxPayNotify};

#[post("/pay/wx/v3/test")]
pub async fn pay_wx_v3_test() -> Result<impl Responder> {
    let wxpay = WxPay {
        appid: WECHAT_MINI_APP_ID,
        mchid: WECHAT_PAY_MCH_ID,
        private_key: WECHAT_PRIVATE_KEY,
        serial_no: WECHAT_PAY_SERIAL,
        apiv3_private_key: WECHAT_PAY_APIV3,
        notify_url: WECHAT_PAY_NOTIFY_URL,
    };
    let data: WxPayData = wxpay
        .jsapi(&Jsapi {
            description: "测试122".to_string(),
            out_trade_no: rand_string(16),
            amount: Amount {
                total: 1,
                ..Default::default()
            },
            payer: Payer { openid },
            ..Default::default()
        })
        .await
        .unwrap();
    return Ok(web::Json(data));
}

/// 微信支付 回调
#[post("/pay/notify_url/action")]
pub async fn pay_notify_url_action(params: web::Json<WxPayNotify>) -> Result<impl Responder> {
    let params = params.0;
    let data = decode_wx_pay(WECHAT_PAY_APIV3, params).unwrap();
    Ok(web::Json(()))
}

公众号/小程序端:

let res = await post("/pay/wx/v3/test");
wx.requestPayment({
  timeStamp: res.data.time_stamp,
  nonceStr: res.data.nonce_str,
  package: res.data.package,
  signType: res.data.sign_type,
  paySign: res.data.pay_sign,
  success(res2) {
    console.log("支付返回", res2);
  },
});
Commit count: 0

cargo fmt