#apple #mobile #google #购买 #验证

iap

使用 hyper 进行异步 google/apple 收款验证

6 个版本

0.3.1 2022年2月25日
0.3.0 2022年2月21日
0.2.1 2021年7月1日
0.2.0 2021年2月11日
0.1.1 2021年2月10日

52#mobile

Download history 7/week @ 2024-03-11 9/week @ 2024-04-01

每月下载量 155

MIT 许可证

56KB
1K SLoC

Build crates Docs

iap

iap 是一个 Rust 库,用于验证通过 Google Play 商店或 Apple App 商店进行的购买收据信息。

当前功能

  • 验证从 Unity 的 IAP 插件 收到的收据数据,以验证订阅是否有效且未过期
  • 辅助函数,用于从 Google/Apple 接收响应数据,以便进行更细粒度的错误处理或验证

支持的交易类型

  • 订阅
  • 消耗品

即将推出的功能

  • 非订阅购买类型
  • 手动输入用于验证的数据,这些数据未通过 Unity IAP 接收

用法

用于简单验证 Unity IAP 收据

您可以通过创建一个 UnityPurchaseValidator 来接收一个 PurchaseResponse,该响应将简单地告诉您购买是否有效(如果是订阅,则不会过期)。

use iap::*;

const APPLE_SECRET: &str = "<APPLE SECRET>";
const GOOGLE_KEY: &str = "<GOOGLE KEY JSON>";

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let validator = UnityPurchaseValidator::default()
        .set_apple_secret(APPLE_SECRET.to_string())
        .set_google_service_account_key(GOOGLE_KEY.to_string())?;

    // RECEIPT_INPUT would be the Json string containing the store, transaction id, and payload
    // from Unity IAP. ie:
    // "{ \"Store\": \"GooglePlay\", \"TransactionID\": \"<Txn ID>\", \"Payload\": \"<Payload>\" }"
    let unity_receipt = UnityPurchaseReceipt::from(&std::env::var("RECEIPT_INPUT")?)?;

    let response = validator.validate(&unity_receipt).await?;

    println!("PurchaseResponse is valid: {}", response.valid);

    Ok(())
}

如果您想要更细粒度的控制并访问商店端点的响应,我们提供了相应的辅助函数。

针对 Play 商店

pub async fn validate(receipt: &UnityPurchaseReceipt) -> error::Result<PurchaseResponse> {
    let response = fetch_google_receipt_data(receipt, "<GOOGLE_KEY>").await?;

    // debug or validate on your own with the data in the response
    println!("Expiry data: {}", response.expiry_time);

    // or just simply validate the response
    validate_google_subscription(&response)
}

针对 App 商店

pub async fn validate(receipt: &UnityPurchaseReceipt) -> error::Result<PurchaseResponse> {
    let response = fetch_apple_receipt_data(receipt, "<APPLE_SECRET>").await?;

    // was this purchase made in the production or sandbox environment
    println!("Environment: {}", response.environment.clone().unwrap());

    Ok(validate_apple_subscription(&response))
}

依赖项

~18–32MB
~611K SLoC