use chrono::{DateTime, NaiveDateTime};
use crate::wechat::Wechat;
use json::{object, JsonValue};
use crate::alipay::AliPay;
pub mod alipay;
pub mod wechat;
#[derive(Clone)]
pub enum Pay {
Wechat(Wechat),
Alipay(AliPay),
None,
}
impl Pay {
pub fn new(data: JsonValue) -> Self {
match data["mode"].as_str().unwrap_or("") {
"wechat" => Pay::Wechat(Wechat {
appid: data["appid"].to_string(),
sp_mchid: data["sp_mchid"].to_string(),
serial_no: data["serial_no"].to_string(),
app_private: data["app_private"].to_string(),
secret: data["secret"].to_string(),
apikey: data["apikey"].to_string(),
notify_url: data["notify_url"].to_string(),
}),
"alipay" => Pay::Alipay(AliPay {
appid: data["appid"].to_string(),
sp_mchid: data["sp_mchid"].to_string(),
app_private: data["app_private"].to_string(),
app_auth_token: data["app_auth_token"].as_str().unwrap_or("").to_string(),
content_encryp: data["content_encryp"].to_string(),
alipay_public_key: data["alipay_public_key"].to_string(),
notify_url: data["notify_url"].to_string(),
}),
_ => Pay::None
}
}
}
impl PayMode for Pay {
fn login(&mut self, code: &str) -> Result<JsonValue, String> {
match self {
Self::Wechat(e) => e.login(code),
Self::Alipay(e) => e.login(code),
Pay::None => Err("No login data".to_owned())
}
}
fn pay_query(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
match self {
Self::Wechat(e) => e.pay_query(
out_trade_no,
sub_mchid,
),
Self::Alipay(e) => e.pay_query(
out_trade_no,
sub_mchid,
),
Pay::None => Err("No login data".to_owned())
}
}
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> {
match self {
Self::Wechat(e) => e.refund(
sub_mchid,
out_trade_no,
transaction_id,
out_refund_no,
amount,
total,
currency,
),
Self::Alipay(e) => e.refund(
sub_mchid,
out_trade_no,
transaction_id,
out_refund_no,
amount,
total,
currency,
),
Pay::None => Err("No login data".to_owned())
}
}
fn pay_notify(&mut self, nonce: &str, ciphertext: &str, associated_data: &str) -> Result<JsonValue, String> {
match self {
Self::Wechat(e) => e.pay_notify(
nonce,
ciphertext,
associated_data,
),
Self::Alipay(e) => e.pay_notify(
nonce,
ciphertext,
associated_data,
),
Pay::None => Err("No login data".to_owned())
}
}
fn refund_notify(&mut self, nonce: &str, ciphertext: &str, associated_data: &str) -> Result<JsonValue, String> {
match self {
Self::Wechat(e) => e.refund_notify(
nonce,
ciphertext,
associated_data,
),
Self::Alipay(e) => e.refund_notify(
nonce,
ciphertext,
associated_data,
),
Pay::None => Err("No login data".to_owned())
}
}
fn refund_query(&mut self, trade_no: &str, out_refund_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
match self {
Self::Wechat(e) => e.refund_query(
trade_no,
out_refund_no,
sub_mchid,
),
Self::Alipay(e) => e.refund_query(
trade_no,
out_refund_no,
sub_mchid,
),
Pay::None => Err("No login data".to_owned())
}
}
fn auth(&mut self, code: &str) -> Result<JsonValue, String> {
match self {
Self::Wechat(e) => e.auth(
code
),
Self::Alipay(e) => e.auth(
code
),
Pay::None => Err("No login data".to_owned())
}
}
fn close(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String> {
match self {
Self::Wechat(e) => e.close(
out_trade_no, sub_mchid,
),
Self::Alipay(e) => e.close(
out_trade_no, sub_mchid,
),
Pay::None => Err("No login data".to_owned())
}
}
fn config(&mut self) -> JsonValue {
match self {
Self::Wechat(e) => object! {
sp_mchid:e.sp_mchid.clone(),
appid:e.appid.clone(),
},
Self::Alipay(e) => object! {
appid:e.appid.clone(),
sp_mchid:e.sp_mchid.clone()
},
Pay::None => object! {
appid:"",
sp_mchid:""
}
}
}
fn pay(&mut self, types: Types, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String> {
match self {
Self::Wechat(e) => e.pay(
types,
sub_mchid,
out_trade_no,
description,
total_fee,
sp_openid,
),
Self::Alipay(e) => e.pay(
types,
sub_mchid,
out_trade_no,
description,
total_fee,
sp_openid,
),
Pay::None => Err("No login data".to_owned())
}
}
}
pub trait PayMode {
fn config(&mut self) -> JsonValue;
fn login(&mut self, code: &str) -> Result<JsonValue, String>;
fn auth(&mut self, code: &str) -> Result<JsonValue, String>;
fn pay(&mut self, types: Types, sub_mchid: &str, out_trade_no: &str, description: &str, total_fee: f64, sp_openid: &str) -> Result<JsonValue, String>;
fn close(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String>;
fn pay_query(&mut self, out_trade_no: &str, sub_mchid: &str) -> Result<JsonValue, String>;
fn pay_notify(&mut self, nonce: &str, ciphertext: &str, associated_data: &str) -> Result<JsonValue, String>;
#[allow(clippy::too_many_arguments)]
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>;
fn refund_notify(&mut self, nonce: &str, ciphertext: &str, associated_data: &str) -> Result<JsonValue, String>;
fn refund_query(&mut self, trade_no: &str, out_refund_no: &str, sub_mchid: &str) -> Result<JsonValue, String>;
}
#[derive(Debug)]
pub struct PayNotify {
trade_type: TradeType,
out_trade_no: String,
sp_mchid: String,
sub_mchid: String,
sp_appid: String,
transaction_id: String,
success_time: i64,
sp_openid: String,
sub_openid: String,
total: usize,
payer_total: usize,
currency: String,
payer_currency: String,
trade_state: TradeState,
}
impl PayNotify {
pub fn json(&self) -> JsonValue {
object! {
"trade_type" => self.trade_type.to_string(),
"out_trade_no" => self.out_trade_no.to_string(),
"sp_mchid" => self.sp_mchid.to_string(),
"sub_mchid" => self.sub_mchid.to_string(),
"sp_appid" => self.sp_appid.to_string(),
"transaction_id" => self.transaction_id.to_string(),
"success_time" => self.success_time,
"sp_openid" => self.sp_openid.to_string(),
"sub_openid" => self.sub_openid.to_string(),
"total" => self.total,
"payer_total" => self.payer_total,
"currency" => self.currency.clone(),
"payer_currency" => self.payer_currency.clone(),
"trade_state" => self.trade_state.to_string(),
}
}
pub fn success_time(datetime: &str) -> i64 {
if datetime.is_empty() {
return 0;
}
let datetime = DateTime::parse_from_rfc3339(datetime).unwrap();
datetime.timestamp()
}
pub fn alipay_time(datetime: &str) -> i64 {
if datetime.is_empty() {
return 0;
}
let t = NaiveDateTime::parse_from_str(datetime, "%Y-%m-%d %H:%M:%S").unwrap();
t.and_utc().timestamp()
}
}
#[derive(Debug)]
pub enum TradeType {
JSAPI,
None,
}
impl TradeType {
fn from(code: &str) -> TradeType {
match code {
"JSAPI" => TradeType::JSAPI,
_ => TradeType::None
}
}
fn to_string(&self) -> &'static str {
match self {
TradeType::JSAPI => "JSAPI",
TradeType::None => ""
}
}
}
#[derive(Debug)]
pub enum TradeState {
SUCCESS,
REFUND,
NOTPAY,
CLOSED,
REVOKED,
USERPAYING,
PAYERROR,
None,
}
impl TradeState {
fn from(code: &str) -> TradeState {
match code {
"SUCCESS" | "TRADE_SUCCESS" => TradeState::SUCCESS,
"REFUND" => TradeState::REFUND,
"NOTPAY" | "WAIT_BUYER_PAY" => TradeState::NOTPAY,
"CLOSED" | "TRADE_CLOSED" => TradeState::CLOSED,
"REVOKED" => TradeState::REVOKED,
"USERPAYING" => TradeState::USERPAYING,
"PAYERROR" | "TRADE_FINISHED" => TradeState::PAYERROR,
_ => TradeState::None
}
}
fn to_string(&self) -> &'static str {
match self {
TradeState::SUCCESS => "已支付",
TradeState::REFUND => "已支付",
TradeState::NOTPAY => "待支付",
TradeState::CLOSED => "已关闭",
TradeState::REVOKED => "已关闭",
TradeState::USERPAYING => "待支付",
TradeState::PAYERROR => "支付失败",
TradeState::None => "待支付"
}
}
}
#[derive(Debug)]
pub enum RefundStatus {
SUCCESS,
CLOSED,
PROCESSING,
ABNORMAL,
None,
}
impl RefundStatus {
fn from(code: &str) -> RefundStatus {
match code {
"SUCCESS" | "Y" | "REFUND_SUCCESS" => RefundStatus::SUCCESS,
"CLOSED" => RefundStatus::CLOSED,
"PROCESSING" | "N" => RefundStatus::PROCESSING,
"ABNORMAL" => RefundStatus::ABNORMAL,
_ => RefundStatus::None
}
}
fn to_string(&self) -> &'static str {
match self {
RefundStatus::SUCCESS => "已退款",
RefundStatus::None => "退款中",
RefundStatus::CLOSED => "已关闭",
RefundStatus::PROCESSING => "退款中",
RefundStatus::ABNORMAL => "退款异常"
}
}
}
#[derive(Debug)]
pub struct RefundNotify {
out_trade_no: String,
refund_no: String,
sp_mchid: String,
sub_mchid: String,
transaction_id: String,
refund_id: String,
success_time: i64,
total: f64,
refund: f64,
payer_total: f64,
payer_refund: f64,
status: RefundStatus,
}
impl RefundNotify {
pub fn json(&self) -> JsonValue {
object! {
"out_trade_no" => self.out_trade_no.clone(),
"sp_mchid" => self.sp_mchid.clone(),
"sub_mchid" => self.sub_mchid.clone(),
"transaction_id" => self.transaction_id.clone(),
"success_time" => self.success_time,
"total" => self.total,
"refund" => self.refund,
"payer_total" => self.payer_total,
"payer_refund" => self.payer_refund,
"status" => self.status.to_string(),
"refund_no"=>self.refund_no.clone(),
"refund_id"=>self.refund_id.clone(),
}
}
}
pub enum Types {
Jsapi,
Native,
H5,
MiniJsapi,
App,
Micropay,
}
impl Types {
pub fn str(self) -> &'static str {
match self {
Types::Jsapi => "jsapi",
Types::Native => "native",
Types::H5 => "h5",
Types::MiniJsapi => "minijsapi",
Types::App => "app",
Types::Micropay => "micropay"
}
}
pub fn from(name: &str) -> Self {
match name {
"jsapi" => Types::Jsapi,
"native" => Types::Native,
"h5" => Types::H5,
"minijsapi" => Types::MiniJsapi,
"app" => Types::App,
"micropay" => Types::Micropay,
_ => Types::Jsapi
}
}
}