Crate midtransclient

Source
Expand description

§Midtransclient for Rust

Unofficial Rust API client/library for Midtrans Payment API

§Usage

Midtrans have 2 different products (Snap & Core API) of payment that you can use:

  • Snap - Customizable payment popup will appear on your web/app (no redirection). doc ref
  • Snap Redirect - Customer need to be redirected to payment url hosted by midtrans. doc ref
  • Core API (VT-Direct) - Basic backend implementation, you can customize the frontend embedded on your web/app as you like (no redirection). doc ref

Choose one that you think best for your unique needs.

§Examples

§Core API Simple Charge

use std::env;
use midtransclient::{MidtransError, CoreApi};
use serde_json::json;

fn main() -> Result<(), MidtransError> {
    // Get MIDTRANS_SERVER_KEY and MIDTRANS_CLIENT_KEY from environment variables
    // or you just can type it directly
    // You can find it in Merchant Portal -> Settings -> Access keys
    let server_key = env::var("MIDTRANS_SERVER_KEY").expect("SERVER_KEY NOT FOUND");
    let client_key = env::var("MIDTRANS_CLIENT_KEY").expect("CLIENT_KEY NOT FOUND");

    // Create Core API instance
    let core = CoreApi::new(false, server_key)
        .client_key(client_key)
        .build()?;

    // Prepare CORE API parameter
    // ( refer to: https://docs.midtrans.com/en/core-api/bank-transfer?id=sample-request-and-request-body )
    // charge bank_transfer parameter example
    let parameters = json!({
        "payment_type": "bank_transfer",
        "transaction_details": {
            "gross_amount": 24145,
            "order_id": "test-transaction-321"
        },
        "bank_transfer": {
            "bank": "bni"
        }
    }).to_string();

    // charge transaction
    let charge_response = core.charge(&parameters)?;
    println!("Charge Response: {:#?}", charge_response);

    // Charge Response: {
    //     "transaction_status": String("pending"),
    //     "va_numbers": Array [Object {
    //         "bank": String("bca"),
    //         "va_number": String("28276362079")
    //     }],
    //     "transaction_time": String("2022-11-18 21:01:24"),
    //     "status_message": String("Success, Bank Transfer transaction is created"),
    //     "fraud_status": String("accept"),
    //     "merchant_id": String("G738628276"),
    //     "transaction_id": String("73ebe57c-ffb0-42a6-93fa-c5022a7f316e"),
    //     "status_code": String("201"),
    //     "order_id": String("test-transaction-321"),
    //     "currency": String("IDR"),
    //     "payment_type": String("bank_transfer"),
    //     "gross_amount": String("24145.00")
    // }

    Ok(())
}

§Snap Simple

use std::env;
use midtransclient::{MidtransError, Snap};
use serde_json::json;

fn main() -> Result<(), MidtransError> {
    // Get MIDTRANS_SERVER_KEY and MIDTRANS_CLIENT_KEY from environment variables
    // or you just can type it directly
    // You can find it in Merchant Portal -> Settings -> Access keys
    let server_key = env::var("MIDTRANS_SERVER_KEY").expect("SERVER_KEY NOT FOUND");
    let client_key = env::var("MIDTRANS_CLIENT_KEY").expect("CLIENT_KEY NOT FOUND");

    // Create Core API instance
    let snap = Snap::new(false, server_key)
        .client_key(client_key)
        .build()?;

    // Prepare SNAP API parameter ( refer to: https://snap-docs.midtrans.com )
    // this is full parameter including optionals parameter.
    let parameters = json!({
        "transaction_details": {
            "order_id": "test-transaction-123",
            "gross_amount": 200000
        }, "credit_card":{
            "secure" : true
        }
    }).to_string();

    let transaction = snap.create_transaction(&parameters)?;
    println!("Create Transaction Response: {:#?}", transaction);

    // Create Transaction Response: {
    //     "redirect_url": String("https://app.sandbox.midtrans.com/snap/v3/redirection/1115ee78-9e17-4089-9992-a6f39e355fa7"),
    //     "token": String("1115ee78-9e17-4089-9992-a6f39e355fa7"),
    //     "status_code": String("201"),
    // }

    Ok(())
}

Modules§

config
Api Config
core_api
CoreApi
error_midtrans
Midtransclient Custom Error
http_client
Http Client
snap
Snap
transactions
Transactions

Structs§

ApiConfig
Config Object that used to store is_production, server_key, client_key. And also API base urls.
CoreApi
CoreApi struct used to do request to Midtrans Core API
Snap
Snap struct used to do request to Midtrans Snap API

Enums§

MidtransError
Midtransclient Custom Errors

Traits§

MidtransClient
Transactions