1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! # 💳 payment_kit
//!
//! A powerful and extensible payment toolkit for Rust — featuring core data models, robust error handling,
//! flexible status flows, and seamless integration into any payment system.
//!
//! Designed to be embedded in services that communicate with third-party payment providers,
//! the `payment_kit` ensures consistency and testability across environments.
//!
//! ---
//!
//! ## ✨ Features
//!
//! - Pluggable `PaymentGateway` trait for easy integration with third-party providers
//! - Strongly-typed `PaymentRequest` and `PaymentResponse` structures
//! - Input validation via the `ValidatableRequest` trait as an option
//! - Rich, structured error types with `PaymentError`
//! - Built-in mock gateway for testing and development
//! - Support for various payment instruments (e.g., credit cards, e-wallets, bank transfers).
//!
//! ---
//!
//! ## 🚀 Quick Start
//!
//! ```rust
//! use payment_kit::models::{PaymentInstrument, PaymentRequest, PaymentResponse, RefundResponse};
//! use payment_kit::processor::PaymentProcessor;
//! use payment_kit::utils::validation::ValidatableRequest;
//! use payment_kit::error::PaymentError;
//! use payment_kit::gateway::PaymentGateway;
//! use payment_kit::status::PaymentStatus;
//!
//! pub struct MockPaymentGateway;
//!
//! impl PaymentGateway for MockPaymentGateway {
//!
//! fn create_payment(&self, req: PaymentRequest) -> Result<PaymentResponse, PaymentError> {
//! if req.amount == 0 || req.order_id.contains("fail") {
//! return Err(PaymentError::InvalidRequest("Simulated failure".into()));
//! }
//!
//! Ok(PaymentResponse {
//! transaction_id: format!("mock_txn_{}", req.order_id),
//! amount: req.amount,
//! payment_instrument: req.payment_instrument,
//! status: PaymentStatus::Pending,
//! redirect_url: Some("https://mock.payment/redirect".to_string()),
//! })
//! }
//! fn check_status(&self, transaction_id: &str) -> Result<PaymentStatus, PaymentError> {
//! if transaction_id.contains("fail") {
//! Err(PaymentError::ProcessingError("Transaction failed".into()))
//! } else {
//! Ok(PaymentStatus::Success)
//! }
//! }
//!
//! fn refund(&self, transaction_id: &str) -> Result<RefundResponse, PaymentError> {
//! if transaction_id.contains("notfound") {
//! Err(PaymentError::InvalidRequest("Transaction ID not found".into()))
//! } else {
//! Ok(RefundResponse {
//! refund_id: format!("refund_{}", transaction_id),
//! status: PaymentStatus::Refunded,
//! transaction_id: transaction_id.to_string(),
//! refunded: true,
//! })
//! }
//! }
//! }
//!
//! fn main() -> Result<(), PaymentError> {
//! let gateway = MockPaymentGateway;
//! let processor = PaymentProcessor::new(&gateway);
//!
//! let request = PaymentRequest {
//! order_id: "INV-001".to_string(),
//! amount: 100_000,
//! currency: "IDR".to_string(),
//! payment_instrument: PaymentInstrument::EWallet {
//! provider: "OVO".to_string(),
//! },
//! customer_id: Some("cust-123".to_string()),
//! description: Some("Order payment".to_string()),
//! metadata: None,
//! };
//!
//! if let Err(e) = request.validate() {
//! eprintln!("Validation failed: {}", e);
//! }
//!
//! let response = processor.create_payment(request);
//! match response {
//! Ok(res) => {
//! println!("Payment successful: {:#?}", res);
//! }
//! Err(err) => {
//! eprintln!("Payment failed: {}", err);
//! }
//! }
//!
//! Ok(())
//! }
//! ```
//!
//! ---
//!
//! ## 📄 License
//!
//! Licensed under the [Apache-2.0 license](http://www.apache.org/licenses/LICENSE-2.0.txt)
//!
//! ---
//!
//! ## 👨 Author
//!
//! Jerry Maheswara <jerrymaheswara@gmail.com>
//!
//! ---
//!
//! ## ❤️ Built with Love in Rust
//!
//! This project is built with ❤️ using **Rust** — a systems programming language that is safe, fast, and concurrent.
//! Rust is the perfect choice for building reliable and efficient applications.
//!
//! ---
//!
//! ## 🤝 Contributing
//!
//! Pull requests, issues, and feedback are welcome!
//! If you find this crate useful, give it a ⭐ and share it with others in the Rustacean community.
//!
//! ---
/// Data models used for representing payment requests, responses, refunds, and payment instruments.
/// The `PaymentGateway` trait defines the interface for integrating with external payment providers.
/// Error types representing various failure scenarios during payment processing.
/// Payment status definitions representing the lifecycle of a transaction.
/// Optional utility helpers.
/// The `PaymentProcessor` struct coordinates the interaction between your application and a payment gateway.