1 module hubtel.merchant; 2 3 import hubtel.config: Config; 4 5 struct MobileMoney 6 { 7 enum TransactionType {send, receive} 8 private Config config; 9 10 auto send(string paymentInformation) 11 { 12 return this.makeTransaction(paymentInformation, TransactionType.send); 13 } 14 15 auto receive(string paymentInformation) 16 { 17 return this.makeTransaction(paymentInformation, TransactionType.receive); 18 } 19 20 private auto makeTransaction(string paymentInformation, TransactionType transactionType) 21 { 22 import std.base64: Base64URL; 23 import std.json: JSONValue; 24 import std.conv: to; 25 import requests: Request, Response; 26 string transaction = (transactionType == TransactionType.send) ? "send" : "receive"; 27 string paymentURL = config.apiURLBase ~ "merchants/" ~ config.merchantAccountNumber ~ "/" ~ transaction ~ "/mobilemoney"; 28 string mix = config.clientID ~ ":" ~ config.clientSecret; 29 string auth = "Basic " ~ Base64URL.encode(cast(ubyte[]) mix).to!string; 30 31 struct Result 32 { 33 bool error = false; 34 string response; 35 int code; 36 } 37 38 Request req = Request(); 39 Response res; 40 req.addHeaders([ 41 "Authorization" : auth, 42 "content-type": "application/json" 43 ]); 44 45 try 46 { 47 res = req.post(paymentURL, JSONValue(paymentInformation).toString(), "x-urlformdata-encoded"); 48 return Result(false, res.responseBody.to!string, res.code); 49 } 50 catch (Exception e) 51 { 52 return Result(true, e.msg); 53 } 54 } 55 }