use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum PayType {
Alipay,
Wxpay,
Qqpay,
#[serde(untagged)]
Other(String),
}
impl PayType {
pub const ALIPAY: &'static str = "alipay";
pub const WXPAY: &'static str = "wxpay";
pub const QQPAY: &'static str = "qqpay";
pub fn as_str(&self) -> &str {
match self {
Self::Alipay => Self::ALIPAY,
Self::Wxpay => Self::WXPAY,
Self::Qqpay => Self::QQPAY,
Self::Other(s) => s,
}
}
pub fn display_name(&self) -> &str {
match self {
Self::Alipay => "支付宝",
Self::Wxpay => "微信支付",
Self::Qqpay => "QQ钱包",
Self::Other(s) => s,
}
}
}
impl fmt::Display for PayType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<&str> for PayType {
fn from(s: &str) -> Self {
match s {
Self::ALIPAY => Self::Alipay,
Self::WXPAY => Self::Wxpay,
Self::QQPAY => Self::Qqpay,
other => Self::Other(other.to_owned()),
}
}
}
impl From<String> for PayType {
fn from(s: String) -> Self {
PayType::from(s.as_str())
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[non_exhaustive]
pub enum Device {
Pc,
Mobile,
Wechat,
Alipay,
#[serde(untagged)]
Other(String),
}
impl Device {
pub fn as_str(&self) -> &str {
match self {
Self::Pc => "pc",
Self::Mobile => "mobile",
Self::Wechat => "wechat",
Self::Alipay => "alipay",
Self::Other(s) => s,
}
}
}
impl fmt::Display for Device {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<&str> for Device {
fn from(s: &str) -> Self {
match s {
"pc" => Self::Pc,
"mobile" => Self::Mobile,
"wechat" => Self::Wechat,
"alipay" => Self::Alipay,
other => Self::Other(other.to_owned()),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TradeStatus(pub String);
impl TradeStatus {
pub const SUCCESS: &'static str = "TRADE_SUCCESS";
pub fn is_success(&self) -> bool {
self.0 == Self::SUCCESS
}
}
impl fmt::Display for TradeStatus {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for TradeStatus {
fn from(s: &str) -> Self {
Self(s.to_owned())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum OrderStatus {
Unpaid,
Paid,
Other(i32),
}
impl OrderStatus {
pub fn from_i32(v: i32) -> Self {
match v {
0 => Self::Unpaid,
1 => Self::Paid,
other => Self::Other(other),
}
}
pub fn is_paid(self) -> bool {
matches!(self, Self::Paid)
}
pub fn as_i32(self) -> i32 {
match self {
Self::Unpaid => 0,
Self::Paid => 1,
Self::Other(v) => v,
}
}
}
pub mod path {
pub const MAPI: &str = "/mapi.php";
pub const SUBMIT: &str = "/submit.php";
pub const API: &str = "/api.php";
}
pub const NOTIFY_SUCCESS: &str = "success";
pub const NOTIFY_FAIL: &str = "fail";