br-pay 0.0.14

This is an pay
Documentation
use std::collections::{HashMap};
use base64::Engine;
use base64::engine::general_purpose;
use chrono::Local;
use crate::{PayMode, 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;
use select::document::Document;
use select::predicate::Name;

#[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" => {
                match http.post(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();
            }
            _ => {
                order["product_code"] = "JSAPI_PAY".into();
            }
        };

        match self.http(api, order) {
            Ok(e) => {
                match types {
                    Types::Jsapi => {}
                    Types::Native => {}
                    Types::H5 => {
                        let document = Document::from(e.to_string().as_str());
                        for meta_tag in document.find(Name("meta")) {
                            if let Some(content) = meta_tag.attr("content") {
                                if let Some(name) = meta_tag.attr("name") {
                                    if name == "unio_origin_url" {
                                        return Ok(object! {url:content});
                                    }
                                }
                            }
                        }
                        return Ok("".into());
                    }
                    Types::MiniJsapi => {}
                    Types::App => {}
                    Types::Micropay => {}
                }
                Ok(e)
            }
            Err(e) => {
                println!("Err: {:#}", e);
                Err(e)
            }
        }
    }

    fn jsapi(
        &mut self,
        _sub_mchid: &str,
        out_trade_no: &str,
        description: &str,
        total_fee: f64,
        _notify_url: &str,
        sp_openid: &str,
    ) -> Result<JsonValue, String> {
        self.app_auth_token = "202503BB5aba0791592f4cd9966ba090d36dfX11".to_string();
        match self.http("alipay.trade.create", object! {
            out_trade_no:out_trade_no,
            total_amount:total_fee,
            subject:description,
            product_code:"JSAPI_PAY",
            op_app_id:"2088541900270114",
            buyer_open_id:sp_openid
        }) {
            Ok(e) => {
                println!("{:#}", e);
            }
            Err(e) => {
                println!("Err: {:#}", e);
                return Err(e);
            }
        };


        Ok(object! {})
    }

    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> {
        todo!()
    }

    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> {
        todo!()
    }

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

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