Skip to content

Commit

Permalink
Chore: Added data models
Browse files Browse the repository at this point in the history
  • Loading branch information
DerrickMm committed Jun 24, 2020
1 parent bd16c66 commit dccc1f3
Showing 7 changed files with 155 additions and 1 deletion.
17 changes: 16 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
TODO: Add your license here.
Copyright (c) 2020 Derrick Mungai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
18 changes: 18 additions & 0 deletions lib/AccessToken.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class AccessToken {
String accessToken;
String expiresIn;

AccessToken({this.accessToken, this.expiresIn});

AccessToken.fromJson(Map<String, dynamic> json) {
accessToken = json['access_token'];
expiresIn = json['expires_in'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['access_token'] = this.accessToken;
data['expires_in'] = this.expiresIn;
return data;
}
}
56 changes: 56 additions & 0 deletions lib/Request.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class Request {
String businessShortCode;
String password;
String timestamp;
String transactionType;
String amount;
String partyA;
String partyB;
String phoneNumber;
String callBackURL;
String accountReference;
String transactionDesc;

Request(
{this.businessShortCode,
this.password,
this.timestamp,
this.transactionType,
this.amount,
this.partyA,
this.partyB,
this.phoneNumber,
this.callBackURL,
this.accountReference,
this.transactionDesc});

Request.fromJson(Map<String, dynamic> json) {
businessShortCode = json['BusinessShortCode'];
password = json['Password'];
timestamp = json['Timestamp'];
transactionType = json['TransactionType'];
amount = json['Amount'];
partyA = json['PartyA'];
partyB = json['PartyB'];
phoneNumber = json['PhoneNumber'];
callBackURL = json['CallBackURL'];
accountReference = json['AccountReference'];
transactionDesc = json['TransactionDesc'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['BusinessShortCode'] = this.businessShortCode;
data['Password'] = this.password;
data['Timestamp'] = this.timestamp;
data['TransactionType'] = this.transactionType;
data['Amount'] = this.amount;
data['PartyA'] = this.partyA;
data['PartyB'] = this.partyB;
data['PhoneNumber'] = this.phoneNumber;
data['CallBackURL'] = this.callBackURL;
data['AccountReference'] = this.accountReference;
data['TransactionDesc'] = this.transactionDesc;
return data;
}
}
24 changes: 24 additions & 0 deletions lib/Response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Response {
String originatorConversationId;
String conversationId;
String responseDescription;

Response(
{this.originatorConversationId,
this.conversationId,
this.responseDescription});

Response.fromJson(Map<String, dynamic> json) {
originatorConversationId = json['OriginatorConversationId'];
conversationId = json['ConversationId'];
responseDescription = json['ResponseDescription'];
}

Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['OriginatorConversationId'] = this.originatorConversationId;
data['ConversationId'] = this.conversationId;
data['ResponseDescription'] = this.responseDescription;
return data;
}
}
26 changes: 26 additions & 0 deletions lib/fluttermpesa.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
library fluttermpesa;

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:fluttermpesa/Request.dart';
import 'package:fluttermpesa/Response.dart';
import 'AccessToken.dart';

class Mpesa {

var productionUrl="https://api.safaricom.co.ke/";
var sandboxUrl="https://sandbox.safaricom.co.ke/";


Future<AccessToken> accessToken() async{

final http.Response response = await http.get( sandboxUrl+"/oauth/v1/generate?grant_type=client_credentials");

return AccessToken.fromJson(jsonDecode(response.body));

}

Future<Response> lipaNaMpesa(Request request) async{
var json = jsonEncode(request.toJson());
final http.Response response = await http.post( sandboxUrl+"/Mpesa/b2c/v1/paymentrequest",
headers: {"Content-Type": "application/json"},
body: json);
return Response.fromJson(jsonDecode(response.body));

}



}
14 changes: 14 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -67,6 +67,20 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
http:
dependency: "direct main"
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.1"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.4"
image:
dependency: transitive
description:
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
http: ^0.12.1

dev_dependencies:
flutter_test:

0 comments on commit dccc1f3

Please sign in to comment.