|
| 1 | +use derive_builder::Builder; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +use crate::client::Mpesa; |
| 5 | +use crate::constants::TransactionType; |
| 6 | +use crate::environment::ApiEnvironment; |
| 7 | +use crate::errors::{MpesaError, MpesaResult}; |
| 8 | + |
| 9 | +const DYNAMIC_QR_URL: &str = "/mpesa/qrcode/v1/generate"; |
| 10 | + |
| 11 | +#[derive(Debug, Serialize)] |
| 12 | +#[serde(rename_all = "PascalCase")] |
| 13 | +pub struct DynamicQRRequest<'mpesa> { |
| 14 | + /// Name of the Company/M-Pesa Merchant Name |
| 15 | + pub merchant_name: &'mpesa str, |
| 16 | + /// Transaction Reference Number |
| 17 | + pub ref_no: &'mpesa str, |
| 18 | + /// The total amount of the transaction |
| 19 | + pub amount: f64, |
| 20 | + #[serde(rename = "TrxCode")] |
| 21 | + /// Transaction Type |
| 22 | + /// |
| 23 | + /// This can be a `TransactionType` or a `&str` |
| 24 | + /// The `&str` must be one of the following: |
| 25 | + /// - `BG` for Buy Goods |
| 26 | + /// - `PB` for Pay Bill |
| 27 | + /// - `WA` Withdraw Cash |
| 28 | + /// - `SM` Send Money (Mobile Number) |
| 29 | + /// - `SB` Sent to Business. Business number CPI in MSISDN format. |
| 30 | + pub transaction_type: TransactionType, |
| 31 | + ///Credit Party Identifier. |
| 32 | + /// |
| 33 | + /// Can be a Mobile Number, Business Number, Agent |
| 34 | + /// Till, Paybill or Business number, or Merchant Buy Goods. |
| 35 | + #[serde(rename = "CPI")] |
| 36 | + pub credit_party_identifier: &'mpesa str, |
| 37 | + /// Size of the QR code image in pixels. |
| 38 | + /// |
| 39 | + /// QR code image will always be a square image. |
| 40 | + pub size: &'mpesa str, |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug, Clone, Deserialize)] |
| 44 | +#[serde(rename_all = "PascalCase")] |
| 45 | +pub struct DynamicQRResponse { |
| 46 | + #[serde(rename(deserialize = "QRCode"))] |
| 47 | + pub qr_code: String, |
| 48 | + pub response_code: String, |
| 49 | + pub response_description: String, |
| 50 | +} |
| 51 | + |
| 52 | +/// Dynamic QR builder struct |
| 53 | +#[derive(Builder, Debug, Clone)] |
| 54 | +#[builder(build_fn(error = "MpesaError"))] |
| 55 | +pub struct DynamicQR<'mpesa, Env: ApiEnvironment> { |
| 56 | + #[builder(pattern = "immutable")] |
| 57 | + client: &'mpesa Mpesa<Env>, |
| 58 | + /// Name of the Company/M-Pesa Merchant Name |
| 59 | + #[builder(setter(into))] |
| 60 | + merchant_name: &'mpesa str, |
| 61 | + /// Transaction Reference Number |
| 62 | + #[builder(setter(into))] |
| 63 | + amount: f64, |
| 64 | + /// The total amount of the transaction |
| 65 | + ref_no: &'mpesa str, |
| 66 | + /// Transaction Type |
| 67 | + /// |
| 68 | + /// This can be a `TransactionType` or a `&str` |
| 69 | + /// The `&str` must be one of the following: |
| 70 | + /// - `BG` for Buy Goods |
| 71 | + /// - `PB` for Pay Bill |
| 72 | + /// - `WA` Withdraw Cash |
| 73 | + /// - `SM` Send Money (Mobile Number) |
| 74 | + /// - `SB` Sent to Business. Business number CPI in MSISDN format. |
| 75 | + #[builder(try_setter, setter(into))] |
| 76 | + transaction_type: TransactionType, |
| 77 | + /// Credit Party Identifier. |
| 78 | + /// Can be a Mobile Number, Business Number, Agent |
| 79 | + /// Till, Paybill or Business number, or Merchant Buy Goods. |
| 80 | + #[builder(setter(into))] |
| 81 | + credit_party_identifier: &'mpesa str, |
| 82 | + /// Size of the QR code image in pixels. |
| 83 | + /// |
| 84 | + /// QR code image will always be a square image. |
| 85 | + #[builder(setter(into))] |
| 86 | + size: &'mpesa str, |
| 87 | +} |
| 88 | + |
| 89 | +impl<'mpesa, Env: ApiEnvironment> From<DynamicQR<'mpesa, Env>> for DynamicQRRequest<'mpesa> { |
| 90 | + fn from(express: DynamicQR<'mpesa, Env>) -> DynamicQRRequest<'mpesa> { |
| 91 | + DynamicQRRequest { |
| 92 | + merchant_name: express.merchant_name, |
| 93 | + ref_no: express.ref_no, |
| 94 | + amount: express.amount, |
| 95 | + transaction_type: express.transaction_type, |
| 96 | + credit_party_identifier: express.credit_party_identifier, |
| 97 | + size: express.size, |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl<'mpesa, Env: ApiEnvironment> DynamicQR<'mpesa, Env> { |
| 103 | + pub(crate) fn builder(client: &'mpesa Mpesa<Env>) -> DynamicQRBuilder<'mpesa, Env> { |
| 104 | + DynamicQRBuilder::default().client(client) |
| 105 | + } |
| 106 | + |
| 107 | + /// # Build Dynamic QR |
| 108 | + /// |
| 109 | + /// Returns a `DynamicQR` which can be used to send a request |
| 110 | + pub fn from_request( |
| 111 | + client: &'mpesa Mpesa<Env>, |
| 112 | + request: DynamicQRRequest<'mpesa>, |
| 113 | + ) -> DynamicQR<'mpesa, Env> { |
| 114 | + DynamicQR { |
| 115 | + client, |
| 116 | + merchant_name: request.merchant_name, |
| 117 | + ref_no: request.ref_no, |
| 118 | + amount: request.amount, |
| 119 | + transaction_type: request.transaction_type, |
| 120 | + credit_party_identifier: request.credit_party_identifier, |
| 121 | + size: request.size, |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /// # Generate a Dynamic QR |
| 126 | + /// |
| 127 | + /// This enables Safaricom M-PESA customers who |
| 128 | + /// have My Safaricom App or M-PESA app, to scan a QR (Quick Response) |
| 129 | + /// code, to capture till number and amount then authorize to pay for goods |
| 130 | + /// and services at select LIPA NA M-PESA (LNM) merchant outlets. |
| 131 | + /// |
| 132 | + /// # Response |
| 133 | + /// A successful request returns a `DynamicQRResponse` type |
| 134 | + /// which contains the QR code |
| 135 | + /// |
| 136 | + /// # Errors |
| 137 | + /// Returns a `MpesaError` on failure |
| 138 | + pub async fn send(self) -> MpesaResult<DynamicQRResponse> { |
| 139 | + let url = format!("{}{}", self.client.environment.base_url(), DYNAMIC_QR_URL); |
| 140 | + |
| 141 | + let response = self |
| 142 | + .client |
| 143 | + .http_client |
| 144 | + .post(&url) |
| 145 | + .bearer_auth(self.client.auth().await?) |
| 146 | + .json::<DynamicQRRequest>(&self.into()) |
| 147 | + .send() |
| 148 | + .await?; |
| 149 | + |
| 150 | + if response.status().is_success() { |
| 151 | + let value = response.json::<_>().await?; |
| 152 | + return Ok(value); |
| 153 | + } |
| 154 | + |
| 155 | + let value = response.json().await?; |
| 156 | + Err(MpesaError::MpesaDynamicQrError(value)) |
| 157 | + } |
| 158 | +} |
0 commit comments