Skip to content

Latest commit

 

History

History
64 lines (55 loc) · 1.8 KB

bulk_invoice.md

File metadata and controls

64 lines (55 loc) · 1.8 KB

Bulk Invoice

Creates a BulkInvoiceBuilder which allows you to send invoices to your customers in bulk.

Safaricom API docs reference

Example

use mpesa::{Mpesa, Environment, Invoice, InvoiceItem};
use chrono::prelude::Utc;

#[tokio::main]
async fn main() {
    dotenvy::dotenv().ok();

    let client = Mpesa::new(
        dotenvy::var("CONSUMER_KEY").unwrap(),
        dotenvy::var("CONSUMER_SECRET").unwrap(),
        Environment::Sandbox,
    );

    let response = client
        .bulk_invoice()
        // Add multiple invoices at once
        .invoices(vec![
            Invoice {
                amount: 1000.0,
                account_reference: "John Doe",
                billed_full_name: "John Doe",
                billed_period: "August 2021",
                billed_phone_number: "0712345678",
                due_date: Utc::now(),
                external_reference: "INV2345",
                invoice_items: Some(
                    vec![InvoiceItem {amount: 1000.0, item_name: "An item"}]
                ),
                invoice_name: "Invoice 001"
            }
        ])
        // Add a single invoice
        .invoice(
            Invoice {
                amount: 1000.0,
                account_reference: "John Doe",
                billed_full_name: "John Doe",
                billed_period: "August 2021",
                billed_phone_number: "0712345678",
                due_date: Utc::now(),
                external_reference: "INV2345",
                invoice_items: Some(vec![InvoiceItem {
                    amount: 1000.0,
                    item_name: "An item",
                }]),
                invoice_name: "Invoice 001",
            }
        )
        .send()
        .await;


    assert!(response.is_ok());
}