10 个版本

0.1.9 2022年4月29日
0.1.8 2022年4月29日

#2128 in 网页编程

每月27次下载

MIT 许可证

84KB
1.5K SLoC

非官方 Payu 客户端

自 0.1.9 版起

正在稳定,可能会发生破坏性更改,但不是很常见

此客户端支持与 REST API 2.1 协议的集成。它展示了通过不同的 PayU 服务实现在线支付的多种方法,主要针对希望实现 PayU 支付服务的开发者。

安装

cargo add pay_u

使用方法

async fn usage() {
    use pay_u::*;
    
    let client_id = ClientId::new(std::env::var("PAYU_CLIENT_ID").unwrap());
    let client_secret = ClientSecret::new(std::env::var("PAYU_CLIENT_SECRET").unwrap());
    let merchant_id = std::env::var("PAYU_CLIENT_MERCHANT_ID").unwrap().parse::<i32>().map(MerchantPosId::from).unwrap();
    let mut client = Client::new(client_id, client_secret, merchant_id);
    client.authorize().await.expect("Invalid credentials");

    let _res = client.create_order(
        req::OrderCreate::build(
            Buyer::new("[email protected]", "654111654", "John", "Doe", "pl"),
            "127.0.0.1",
            "PLN",
            "Some description"
        )
            .expect("All required fields must be valid")
            // Endpoint which will be requested by PayU with payment status update
            .with_notify_url("https://your.eshop.com/notify")
            // payment description (MANDATORY)
            .with_description("RTV market")
            // add list of products
            .with_products(
                [
                    Product::new("Wireless Mouse for Laptop", 15000, 1),
                    Product::new("HDMI cable", 6000, 1),
                ]
                    .into_iter(),
            )
            // add additional product
            .with_product(Product::new("HDMI cable", 6000, 1)),
    )
        .await;

    // partial refund
    let _res = client
        .refund(
            OrderId::new("H9LL64F37H160126GUEST000P01"),
            RefundRequest::new("Refund", Some(1000)),
        )
        .await;
    // Full refund
    let _res = client
        .refund(
            OrderId::new("H9LL64F37H160126GUEST000P01"),
            RefundRequest::new("Refund", None),
        )
        .await;
    
    // Order details
    let _res = client.order_details(OrderId::new("H9LL64F37H160126GUEST000P01")).await;
    
    // Transactions
    let _res = client.order_transactions(OrderId::new("H9LL64F37H160126GUEST000P01")).await;
}

Actix 集成

use actix_web::{*, web::*};

#[post("/checkout")]
async fn checkout(session: Data<Session>, db: Data<Database>, payu: Data<Arc<Mutex<pay_u::Client>>>) -> HttpResponse {
    let user_id = session.user_required()?;
    let payu = payu.into_inner();
    let shopping_cart = db.send(LoadShoppingCart { user_id }).await??;
    let shopping_cart_id = shopping_cart.id;
    let create_order_req: pay_u::req::OrderCreate = shopping_cart.into();
    let pay_u::res::CreateOrder { redirect_uri, order_id, .. } = payu.create_order(create_order_req).await?;
    db.send(database::CreateOrder { shopping_cart_id, order_id }).await??;
    HttpResponse::SeeOther().append_header((actix_web::http::header::LOCATION, redirect_uri)).body("")
}

#[post("/pay_u/{own_order_id}/notify")]
async fn handle_notification(
    path: Path<i32>,
    Json(notify): Json<pay_u::notify::StatusUpdate>,
    payment: Data<Addr<PaymentManager>>
) -> HttpResponse {
    let status = notify.status();
    // Create additional field which will always be unique like UUID
    // Do not use record primary key!
    let ext_order_id = String::from(notify.ext_order_id());
    
    let order_id = path.into_inner();
    payment.do_send(payment_manager::Update {
        status,
        ext_order_id
    });
    HttpResponse::Ok().body("")
}

版本

0.1.9 - 稳定,可能会发生破坏性更改,但不是很常见 0.1.8 - 增加文档,将请求移动到模块 0.1.7 - 添加了信用和更多创建订单请求选项,如附加描述、可见描述。

错误

请在此处报告错误:https://todo.sr.ht/~tsumanu/payu-rs

依赖项

~5–16MB
~242K SLoC