|
| 1 | +use chrono::{DateTime, Local}; |
| 2 | +use reqwest::blocking::Response; |
| 3 | +use serde::Serialize; |
| 4 | +use serde_json::Value as JSON; |
| 5 | + |
| 6 | +use crate::utils::{make_get_request, make_request, REQUEST}; |
| 7 | + |
| 8 | +#[derive(Debug, Default)] |
| 9 | +pub struct Charge { |
| 10 | + pub(crate) bearer_auth: String, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Debug, Serialize)] |
| 14 | +pub struct CreateChargeBody<'a> { |
| 15 | + /// Customer's email address |
| 16 | + pub email: &'a str, |
| 17 | + /// Amount should be in kobo if currency is `NGN`, pesewas, if currency is `GHS`, and cents, if currency is `ZAR` |
| 18 | + pub amount: &'a str, |
| 19 | + /// Bank account to charge (don't send if charging an authorization code) |
| 20 | + pub bank: Option<JSON>, |
| 21 | + /// An authorization code to charge (don't send if charging a bank account) |
| 22 | + pub authorization_code: Option<&'a str>, |
| 23 | + /// 4-digit PIN (send with a non-reusable authorization code) |
| 24 | + pub pin: Option<&'a str>, |
| 25 | + /// A JSON object |
| 26 | + pub metadata: Option<JSON>, |
| 27 | + /// Unique transaction reference. Only -, .`, = and alphanumeric characters allowed. |
| 28 | + pub reference: Option<&'a str>, |
| 29 | + /// USSD type to charge (don't send if charging an authorization code, bank or card) |
| 30 | + pub ussd: Option<JSON>, |
| 31 | + /// Mobile details (don't send if charging an authorization code, bank or card) |
| 32 | + pub mobile_money: Option<JSON>, |
| 33 | + /// This is the unique identifier of the device a user uses in making payment. |
| 34 | + /// Only -, .`, = and alphanumeric characters allowed. |
| 35 | + pub device_id: Option<&'a str>, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Debug, Serialize)] |
| 39 | +pub struct SubmitPinBody<'a> { |
| 40 | + /// PIN submitted by user |
| 41 | + pub pin: &'a str, |
| 42 | + /// Reference for transaction that requested pin |
| 43 | + pub reference: &'a str, |
| 44 | +} |
| 45 | + |
| 46 | +#[derive(Debug, Serialize)] |
| 47 | +pub struct SubmitOTPBody<'a> { |
| 48 | + /// OTP submitted by user |
| 49 | + pub otp: &'a str, |
| 50 | + /// Reference for ongoing transaction |
| 51 | + pub reference: &'a str, |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Debug, Serialize)] |
| 55 | +pub struct SubmitPhoneBody<'a> { |
| 56 | + /// Phone submitted by user |
| 57 | + pub phone: &'a str, |
| 58 | + /// Reference for ongoing transaction |
| 59 | + pub reference: &'a str, |
| 60 | +} |
| 61 | + |
| 62 | +#[derive(Debug, Serialize)] |
| 63 | +pub struct SubmitBirthdayBody<'a> { |
| 64 | + /// Birthday submitted by user e.g. 2016-09-21 |
| 65 | + pub birthday: DateTime<Local>, |
| 66 | + /// Reference for ongoing transaction |
| 67 | + pub reference: &'a str, |
| 68 | +} |
| 69 | + |
| 70 | +#[derive(Debug, Serialize)] |
| 71 | +pub struct SubmitAddressBody<'a> { |
| 72 | + /// Address submitted by user |
| 73 | + pub address: &'a str, |
| 74 | + /// Reference for ongoing transaction |
| 75 | + pub reference: &'a str, |
| 76 | + /// City submitted by user |
| 77 | + pub city: &'a str, |
| 78 | + /// State submitted by user |
| 79 | + pub state: &'a str, |
| 80 | + /// Zipcode submitted by user |
| 81 | + pub zipcode: &'a str, |
| 82 | +} |
| 83 | + |
| 84 | +const CHARGE_URL: &str = "https://api.paystack.co/charge"; |
| 85 | +impl Charge { |
| 86 | + // TODO: link payment channel here |
| 87 | + /// Initiate a payment by integrating the [][payment channel] of your choice. |
| 88 | + pub fn create_charge(&self, body: CreateChargeBody) -> Result<Response, String> { |
| 89 | + let res = make_request(&self.bearer_auth, CHARGE_URL, Some(body), REQUEST::POST); |
| 90 | + return res; |
| 91 | + } |
| 92 | + |
| 93 | + /// Submit PIN to continue a charge |
| 94 | + pub fn submit_pin(&self, body: SubmitPinBody) -> Result<Response, String> { |
| 95 | + let url = format!("{}/submit_pin", CHARGE_URL); |
| 96 | + let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST); |
| 97 | + return res; |
| 98 | + } |
| 99 | + |
| 100 | + /// Submit OTP to complete a charge |
| 101 | + pub fn submit_otp(&self, body: SubmitOTPBody) -> Result<Response, String> { |
| 102 | + let url = format!("{}/submit_otp", CHARGE_URL); |
| 103 | + let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST); |
| 104 | + return res; |
| 105 | + } |
| 106 | + |
| 107 | + /// Submit phone when requested |
| 108 | + pub fn submit_phone(&self, body: SubmitPhoneBody) -> Result<Response, String> { |
| 109 | + let url = format!("{}/submit_phone", CHARGE_URL); |
| 110 | + let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST); |
| 111 | + return res; |
| 112 | + } |
| 113 | + |
| 114 | + /// Submit birthday when requested |
| 115 | + pub fn submit_birthday(&self, body: SubmitBirthdayBody) -> Result<Response, String> { |
| 116 | + let url = format!("{}/submit_birthday", CHARGE_URL); |
| 117 | + let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST); |
| 118 | + return res; |
| 119 | + } |
| 120 | + |
| 121 | + /// Submit address to continue a charge |
| 122 | + pub fn submit_address(&self, body: SubmitAddressBody) -> Result<Response, String> { |
| 123 | + let url = format!("{}/submit_address", CHARGE_URL); |
| 124 | + let res = make_request(&self.bearer_auth, &url, Some(body), REQUEST::POST); |
| 125 | + return res; |
| 126 | + } |
| 127 | + |
| 128 | + /// When you get "pending" as a charge status or if there was an exception when calling any of the /charge endpoints, |
| 129 | + /// wait 10 seconds or more, then make a check to see if its status has changed. |
| 130 | + /// Don't call too early as you may get a lot more pending than you should. |
| 131 | + pub fn check_pending_charge(&self, reference: &str) -> Result<Response, String> { |
| 132 | + let url = format!("{}/{}", CHARGE_URL, reference); |
| 133 | + let res = make_get_request(&self.bearer_auth, &url, None::<String>); |
| 134 | + return res; |
| 135 | + } |
| 136 | +} |
0 commit comments