br-pay 0.0.17

This is an pay
Documentation
use std::collections::{HashMap};
use base64::Engine;
use base64::engine::general_purpose;
use chrono::{Local};
use crate::{PayMode, PayNotify, RefundNotify, RefundStatus, TradeState, TradeType, Types};
use json::{object, JsonValue};
use openssl::hash::MessageDigest;
use openssl::pkey::{PKey};
use openssl::rsa::Rsa;
use openssl::sign::Signer;
use urlencoding::encode;
use log::error;

#[derive(Clone)]
pub struct AliPay {
    /// 应用appid
    pub appid: String,
    /// 服务商商家号
    pub sp_mchid: String,
    /// 授权token
    pub app_auth_token: String,
    /// 应用私钥证书路径
    pub app_private: String,
    /// 接口内容加密密钥
    pub content_encryp: String,
    /// 支付宝公钥
    pub alipay_public_key: String,
    pub notify_url: String,
}
impl AliPay {
    pub fn http(&mut self, method: &str, biz_content: JsonValue) -> Result<JsonValue, String> {
        let mut http = br_http::Http::new();
        let sign = "";

        let now = Local::now();
        let timestamp = now.format("%Y-%m-%d %H:%M:%S").to_string();


        let mut data = object! {
            "charset":"UTF-8",
            "method":method,
            "app_id":self.appid.clone(),
            "version":"1.0",
            "sign_type":"RSA2",
            "timestamp":timestamp,
            "app_auth_token":self.app_auth_token.clone(),
            "sign":sign,
            "biz_content": biz_content,
        };

        let mut map = HashMap::new();
        for (key, value) in data.entries() {
            if key == "sign" {
                continue;
            }
            if value.is_empty() {
                continue;
            }
            map.insert(key, value);
        }

        let mut keys: Vec<_> = map.keys().cloned().collect();
        keys.sort();
        let mut txt = vec![];
        for key in keys {
            txt.push(format!("{}={}", key, map.get(&key).unwrap()));
        }
        let txt = txt.join("&");
        let t = self.app_private.as_bytes().chunks(64).map(|chunk| std::str::from_utf8(chunk).unwrap_or("")).collect::<Vec<&str>>().join("\n");
        let cart = format!("-----BEGIN PRIVATE KEY-----\n{}\n-----END PRIVATE KEY-----\n", t);

        // 1. 加载私钥
        let rsa = match Rsa::private_key_from_pem(cart.as_bytes()) {
            Ok(e) => e,
            Err(e) => return Err(e.to_string())
        };

        let pkey = match PKey::from_rsa(rsa) {
            Ok(e) => e,
            Err(e) => return Err(e.to_string())
        };

        // 2. 创建签名器,使用 SHA256
        let mut signer = match Signer::new(MessageDigest::sha256(), &pkey) {
            Ok(e) => e,
            Err(e) => return Err(e.to_string())
        };
        match signer.update(txt.as_bytes()) {
            Ok(()) => {}
            Err(e) => return Err(e.to_string())
        };
        // 3. 生成签名
        let signature = match signer.sign_to_vec() {
            Ok(e) => e,
            Err(e) => return Err(e.to_string())
        };
        // 4. Base64 编码输出
        data["sign"] = general_purpose::STANDARD.encode(signature).into();

        let mut new_data = object! {};
        for (key, value) in data.entries() {
            let t = encode(value.to_string().as_str()).to_string();
            new_data[key] = t.into();
        }
        data = new_data;
        let url = "https://openapi.alipay.com/gateway.do".to_string();
        let res = match method {
            "alipay.trade.wap.pay" => {
                let tt = http.get(url.as_str()).query(data);
                return Ok(tt.url.clone().into());
                //match http.get(url.as_str()).query(data).html() {
                //    Ok(e) => {
                //        return Ok(e.into());
                //    }
                //    Err(e) => {
                //        println!(">>>>>>{}", e);
                //        return Err(e);
                //    }
                //}
            }
            _ => {
                match http.post(url.as_str()).query(data).json() {
                    Ok(e) => e,
                    Err(e) => {
                        println!(">>>>>>{}", e);
                        return Err(e);
                    }
                }
            }
        };
        if res.has_key("error_response") {
            return Err(res["error_response"]["sub_msg"].to_string());
        }
        let key = method.replace(".", "_");
        let key = format!("{}_response", key);
        let data = res[key].clone();
        if data.has_key("code") {
            if data["code"] != "10000" {
                Err(data["sub_msg"].to_string())
            } else {
                Ok(data)
            }
        } else {
            Err(data.to_string())
        }
    }
}
impl PayMode for AliPay {
    fn config(&mut self) -> JsonValue {
        todo!()
    }

    fn login(&mut self, code: &str) -> Result<JsonValue, String> {
        let res = self.http("alipay.system.oauth.token", object! {
            //grant_type:"authorization_code",
            code:code
        })?;
        Ok(res)
    }

    fn auth(&mut self, code: &str) -> Result<JsonValue, String> {
        let res = match self.http("alipay.open.auth.token.app", object! {
            grant_type:"authorization_code",
            code:code
        }) {
            Ok(e) => e,
            Err(e) => {
                error!("Err: {:#}", e);
                return Err(e);
            }
        };
        //let user_id = res["user_id"].clone();
        //let auth_app_id = res["auth_app_id"].clone();
        //let re_expires_in = res["re_expires_in"].clone();
        //let expires_in = res["expires_in"].clone();
        //let app_auth_token = res["app_auth_token"].clone();
        Ok(res)
    }

    fn pay(&mut self, types: Types, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String> {
        let mut api = "";
        let mut order = object! {
            out_trade_no:out_trade_no,
            total_amount:total_fee,
            subject:description,
            product_code:"",
            op_app_id:sub_mchid,
            buyer_open_id:sp_openid
        };

        match types {
            Types::Jsapi => {
                api = "alipay.trade.create";
                order["product_code"] = "JSAPI_PAY".into();
            }
            Types::H5 => {
                api = "alipay.trade.wap.pay";
                order["product_code"] = "QUICK_WAP_WAY".into();
            }
            Types::Native => {
                api = "alipay.trade.wap.pay";
                order["product_code"] = "QUICK_WAP_WAY".into();
            }
            _ => {
                order["product_code"] = "JSAPI_PAY".into();
            }
        };

        match self.http(api, order) {
            Ok(e) => {
                match types {
                    Types::Jsapi => {}
                    Types::Native => {}
                    Types::H5 => {
                        return Ok(object! {url:e});
                    }
                    Types::MiniJsapi => {}
                    Types::App => {}
                    Types::Micropay => {}
                }
                Ok(e)
            }
            Err(e) => {
                println!("Err: {:#}", e);
                Err(e)
            }
        }
    }


    fn close(&mut self, _out_trade_no: &str, _sub_mchid: &str) -> Result<JsonValue, String> {
        todo!()
    }

    fn pay_query(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
        let order = object! {
            out_trade_no:out_trade_no
        };
        match self.http("alipay.trade.query", order) {
            Ok(e) => {
                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
                    return Err(e["msg"].to_string());
                }
                let res = PayNotify {
                    trade_type: TradeType::None,
                    out_trade_no: e["out_trade_no"].to_string(),
                    sp_mchid: "".to_string(),
                    sub_mchid: sub_mchid.to_string(),
                    sp_appid: "".to_string(),
                    transaction_id: e["trade_no"].to_string(),
                    success_time: PayNotify::alipay_time(e["send_pay_date"].as_str().unwrap()),
                    sp_openid: e["buyer_user_id"].to_string(),
                    sub_openid: e["buyer_user_id"].to_string(),
                    total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0) * 100.0) as usize,
                    payer_total: (e["total_amount"].to_string().parse::<f64>().unwrap_or(0.0) * 100.0) as usize,
                    currency: "CNY".to_string(),
                    payer_currency: "CNY".to_string(),
                    trade_state: TradeState::from(e["trade_status"].as_str().unwrap()),
                };
                Ok(res.json())
            }
            Err(e) => {
                println!("Err: {:#}", e);
                Err(e)
            }
        }
    }

    fn pay_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
        todo!()
    }

    fn refund(&mut self, sub_mchid: &str, out_trade_no: &str, transaction_id: &str, out_refund_no: &str, amount: f64, total: f64, _currency: &str) -> Result<JsonValue, String> {
        let body = object! {
            "trade_no"=>transaction_id,
            "out_trade_no"=>out_trade_no,
            "out_request_no"=>out_refund_no,
            "refund_amount"=>format!("{:.2}",amount),
        };
        match self.http("alipay.trade.refund", body.clone()) {
            Ok(e) => {
                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
                    return Err(e["msg"].to_string());
                }
                let res = RefundNotify {
                    out_trade_no: e["out_trade_no"].to_string(),
                    refund_no: out_refund_no.to_string(),
                    sp_mchid: "".to_string(),
                    sub_mchid: sub_mchid.to_string(),
                    transaction_id: e["trade_no"].to_string(),
                    refund_id: out_refund_no.to_string(),
                    success_time: PayNotify::alipay_time(e["gmt_refund_pay"].as_str().unwrap()),
                    total,
                    refund: e["refund_fee"].to_string().parse::<f64>().unwrap(),
                    payer_total: e["refund_fee"].to_string().parse::<f64>().unwrap(),
                    payer_refund: e["send_back_fee"].to_string().parse::<f64>().unwrap(),
                    status: RefundStatus::from(e["fund_change"].as_str().unwrap()),
                };
                Ok(res.json())
            }
            Err(e) => {
                println!("ERR: {:#} {:#}", e, body);
                Err(e)
            }
        }
    }

    fn refund_notify(&mut self, _nonce: &str, _ciphertext: &str, _associated_data: &str) -> Result<JsonValue, String> {
        todo!()
    }

    fn refund_query(&mut self, trade_no: &str, out_refund_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
        let body = object! {
            "out_request_no"=>out_refund_no,
            "trade_no"=>trade_no,
        };
        match self.http("alipay.trade.fastpay.refund.query", body.clone()) {
            Ok(e) => {
                if e.has_key("code") && e["code"].as_str().unwrap() != "10000" {
                    return Err(e["msg"].to_string());
                }
                let res = RefundNotify {
                    out_trade_no: e["out_trade_no"].to_string(),
                    refund_no: e["out_request_no"].to_string(),
                    sp_mchid: "".to_string(),
                    sub_mchid: sub_mchid.to_string(),
                    transaction_id: e["trade_no"].to_string(),
                    refund_id: e["out_request_no"].to_string(),
                    success_time: Local::now().timestamp(),
                    total: e["total_amount"].to_string().parse::<f64>().unwrap(),
                    payer_total: e["total_amount"].to_string().parse::<f64>().unwrap(),
                    refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
                    payer_refund: e["refund_amount"].to_string().parse::<f64>().unwrap(),
                    status: RefundStatus::from(e["refund_status"].as_str().unwrap()),
                };
                Ok(res.json())
            }
            Err(e) => Err(e)
        }
    }
}