Skip to content

Commit

Permalink
lipa api fully intergrated
Browse files Browse the repository at this point in the history
  • Loading branch information
okirialbert committed Jan 14, 2019
1 parent dc9928c commit 8bc124c
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 108 deletions.
38 changes: 19 additions & 19 deletions .firebase/hosting.ZGlzdC9sb2N1cw.cache
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
index.html,1546577796068,3841ce25cbe82aa41b3688452320b66bf987b01e5bc88b8416c0481eb905abf8
3rdpartylicenses.txt,1546577796032,52012ce7117a49ccc70c456ba193be1c6c40ffc6f1a7c0f004f4d4610802cbbf
runtime.ec2944dd8b20ec099bf3.js,1546577796036,fe4da65f4a99554b6b5dee1b019cbfa532b97645ced5cd91edc6a20c7e630d83
assets/assignblue.svg,1546577796072,e85bae118eee208cb5deed4427d57bb08d964f75d15220bd9c0c6c080d743a59
assets/baseline-assignment_turned_in-24px.svg,1546577796068,992b4fd1ab9a1726e9e932a813accee31c62976cf0da990c3ecdc2a93c16009d
polyfills.c6871e56cb80756a5498.js,1546577796036,2eb1a3871df9dea4195b0a4edb18fc8237ff07cbb5e5d7432fb12ae0d01e99ac
assets/baseline-security-24px.svg,1546577796068,81428ccdf8f33f0f196ff1a2fa1cfb35f7a9acde114fa23134c840c5b32c71d5
assets/baseline-timer-24px.svg,1546577796068,e9c29148ed2cec5be21e0748a43a247013798b190b30f7b8bb3bceda82c7dae1
assets/folder.svg,1546577796068,ca8f653cd24dd1249a6511ef924df12d0693648e5092e47d00b17cdb2e96057a
assets/mpesa.png,1546577796068,fcbabd855a0008578eab19146e10ddcc95329e6831405b3990242b12a349e1fb
assets/paper.svg,1546577796072,47a771558dd16b6e18601c4e32af27fe951dce4476780de10aecd7aa0a7aa212
assets/pros-and-cons.svg,1546577796068,9fcbae3182ca437ba34aa404c4893b18a8c311f7ed8b48ea322f8d0b60830494
assets/scholarship.svg,1546577796072,08665df205c2be9bd9996c749c88a8c77a38e40d1f8d741ee5a777e2acca39b3
styles.b449a033228d440ca021.css,1546577796036,4e4d829b693a77eb5b180f38db5bcf217df4569f8f23c85617230decab9167ac
banner-locus_short.a54dc9829755c3aecc53.jpg,1546577796036,afe2cd026248097c6218d9a8de41d1dd4e547d78a55b1ec4974f4456082e0d6a
assets/banner-locus_short.jpg,1546577796068,afe2cd026248097c6218d9a8de41d1dd4e547d78a55b1ec4974f4456082e0d6a
assets/banner-locus.jpg,1546577796068,30c190467d1ba0a4eaa9b87cab35df052cde783c23d27be1be1c9e0536e6347d
banner-locus.f9adb3ec86e53db615ef.jpg,1546577796032,30c190467d1ba0a4eaa9b87cab35df052cde783c23d27be1be1c9e0536e6347d
main.5c3455530b6210a41014.js,1546577796032,b8b8b9cde2df1976e6a998abc55569f793aeb942b36596733546a00658ff6a8d
index.html,1547325484066,4f7389c6e6baaefed69efc254b8ce7f27d616cc4dcb7f76eed233fbfe1aabd5c
runtime.ec2944dd8b20ec099bf3.js,1547325484062,fe4da65f4a99554b6b5dee1b019cbfa532b97645ced5cd91edc6a20c7e630d83
assets/assignblue.svg,1547325484066,e85bae118eee208cb5deed4427d57bb08d964f75d15220bd9c0c6c080d743a59
3rdpartylicenses.txt,1547325484062,2174b9d6b3f06e84b1c8ccc56a026d47c00923b23642b883483366bacb6dbd3f
assets/baseline-assignment_turned_in-24px.svg,1547325484066,992b4fd1ab9a1726e9e932a813accee31c62976cf0da990c3ecdc2a93c16009d
assets/baseline-security-24px.svg,1547325484066,81428ccdf8f33f0f196ff1a2fa1cfb35f7a9acde114fa23134c840c5b32c71d5
assets/baseline-timer-24px.svg,1547325484066,e9c29148ed2cec5be21e0748a43a247013798b190b30f7b8bb3bceda82c7dae1
assets/folder.svg,1547325484066,ca8f653cd24dd1249a6511ef924df12d0693648e5092e47d00b17cdb2e96057a
assets/mpesa.png,1547325484066,fcbabd855a0008578eab19146e10ddcc95329e6831405b3990242b12a349e1fb
assets/paper.svg,1547325484066,47a771558dd16b6e18601c4e32af27fe951dce4476780de10aecd7aa0a7aa212
assets/pros-and-cons.svg,1547325484066,9fcbae3182ca437ba34aa404c4893b18a8c311f7ed8b48ea322f8d0b60830494
assets/scholarship.svg,1547325484066,08665df205c2be9bd9996c749c88a8c77a38e40d1f8d741ee5a777e2acca39b3
polyfills.c6871e56cb80756a5498.js,1547325484062,2eb1a3871df9dea4195b0a4edb18fc8237ff07cbb5e5d7432fb12ae0d01e99ac
banner-locus_short.a54dc9829755c3aecc53.jpg,1547325484062,afe2cd026248097c6218d9a8de41d1dd4e547d78a55b1ec4974f4456082e0d6a
assets/banner-locus_short.jpg,1547325484066,afe2cd026248097c6218d9a8de41d1dd4e547d78a55b1ec4974f4456082e0d6a
styles.b449a033228d440ca021.css,1547325484062,4e4d829b693a77eb5b180f38db5bcf217df4569f8f23c85617230decab9167ac
assets/banner-locus.jpg,1547325484066,30c190467d1ba0a4eaa9b87cab35df052cde783c23d27be1be1c9e0536e6347d
banner-locus.f9adb3ec86e53db615ef.jpg,1547325484062,30c190467d1ba0a4eaa9b87cab35df052cde783c23d27be1be1c9e0536e6347d
main.031349d76c9deae753a4.js,1547325484062,04919db08bbeb7cc7337724e7e8d37dab8431f9baa283a78b08fcbe482a6194b
3 changes: 2 additions & 1 deletion angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "locus:build"
"browserTarget": "locus:build",
"proxyConfig": "src/proxy.conf.json"
},
"configurations": {
"production": {
Expand Down
4 changes: 2 additions & 2 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import { AccountComponent } from './front/account/account.component';
import { CheckoutComponent } from './front/checkout/checkout.component';
import { WalletdialogComponent } from './front/walletdialog/walletdialog.component';
import { HttpClientModule } from '@angular/common/http';
import { Buffer } from "buffer";
import { MpesaService } from './services/mpesa.service';
import { DatePipe } from '@angular/common';


@NgModule({
Expand Down Expand Up @@ -72,7 +72,7 @@ import { MpesaService } from './services/mpesa.service';

],
providers: [
MpesaService,
MpesaService, DatePipe

],
bootstrap: [AppComponent],
Expand Down
3 changes: 1 addition & 2 deletions src/app/front/assignment/assignment.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
<button (click)="tokenizer()" class="mat-raised-button">
Mpesa token generator
</button>
{{oAuthToken}}
<button (click)="lipaFunction()" class="mat-raised-button">
<button (click)="lipaFunct()" class="mat-raised-button">
Lipa Mpesa Function
</button>
<mat-horizontal-stepper [linear]="isLinear" #stepper>
Expand Down
64 changes: 14 additions & 50 deletions src/app/front/assignment/assignment.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,17 @@ import { tap, finalize } from 'rxjs/operators';

import { AngularFireAuth } from "@angular/fire/auth";
import { AngularFireDatabase } from "@angular/fire/database";
import { Router, ActivatedRoute } from '@angular/router';
import * as firebase from 'firebase/app'
import { GlobalService } from "../../services/global.service";
import { MpesaService } from "../../services/mpesa.service";
import { error } from 'util';
import { HttpResponse, HttpHeaders } from '@angular/common/http';
import { Time } from '@angular/common';
import { HttpClient } from '@angular/common/http';

export interface authToken {
access_token: string,
expires_in: string
}

export interface DialogData {
animal: string;
name: string;
}


export interface Assignment {
value: string;
Expand Down Expand Up @@ -65,12 +58,13 @@ export class AssignmentComponent implements OnInit {
isLinear = false;
firstFormGroup: FormGroup;
secondFormGroup: FormGroup;
public json: JSON;
public result: authToken;

public oAuthToken: string;
public oAuthExp: string;
public lipaAuth: string;
url = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest"
public Amount: string = "5";

constructor(
private _formBuilder: FormBuilder,
Expand All @@ -84,7 +78,7 @@ export class AssignmentComponent implements OnInit {
public globalService: GlobalService,

public mpesa: MpesaService,
public http: HttpClient
public http: HttpClient,

) {

Expand Down Expand Up @@ -192,50 +186,20 @@ export class AssignmentComponent implements OnInit {
}

tokenizer() {
this.mpesa.getConfig().subscribe(
body => {
this.result = body;
this.oAuthToken = this.result.access_token;
this.oAuthExp = this.result.expires_in;
}
)
this.mpesa.getConfig()

}

lipaFunction() {
this.lipaAuth = "Bearer " + this.oAuthToken;
const json = {
"BusinessShortCode": "174379",
"Password": "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919",
"Timestamp": "2019-01-15T22:18:36+03:00",
"TransactionType": "CustomerPayBillOnline",
"Amount": "1",
"PartyA": "254701737488",
"PartyB": "174379",
"PhoneNumber": "254701737488",
"CallBackURL": "",
"AccountReference": " ",
"TransactionDesc": "Pay to Researh Locus and begin your transaction"
}
const httpOptions = {
headers: new HttpHeaders({
'Authorization': this.lipaAuth,
'Access-Control-Allow-Origin': "http://localhost:4200",
"Content-Type": "application/json",
'accept': 'application/json, application/xml'

})
};
console.log(this.lipaAuth, json)

this.http.post(this.url, json, httpOptions).pipe(
tap(
(body) => {
console.log("comming up" + body)
}
)
lipaFunct() {
this.mpesa.lipaFunction(this.Amount).subscribe(
response => {
console.log("Sucess", response);
},
error => {
console.log("Error", error);
}

)

}

}
72 changes: 66 additions & 6 deletions src/app/services/mpesa.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse, HttpRequest, HttpHeaders } from "@angular/common/http";
import { Headers } from '@angular/http';
import { HttpClient, HttpResponse, HttpRequest, HttpHeaders, HttpErrorResponse } from "@angular/common/http";
import { tap, retry, catchError } from 'rxjs/operators';
import { Buffer } from 'buffer'
import { Observable } from "rxjs";
import { Observable, throwError } from "rxjs";
import { DatePipe } from '@angular/common';
import { toBase64String } from '@angular/compiler/src/output/source_map';

export interface authToken {
access_token: string,
Expand All @@ -22,27 +23,86 @@ export class MpesaService {
public host: any;
public auth_token: any;


public consumer_key = "dtroPUFZaYP6hDJO6N0KjdRqrWj4bVOo";
public consumer_secret = "12DJQ25KqNN9C5KF";
public url: string = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials";
result: authToken;
public oAuthToken: any;
public oAuthExp: any;
public lipaAuth: string;
public BusinessShortcode: string = "174379";
public timeStamp: Date;
public time: any;
public passWord: string;
public passKey: string = "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919";
public lipaUrl: string = "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest";
public json: any;


constructor(
public http: HttpClient,
public datePipe: DatePipe

) {

}

getConfig() {
this.auth = "Basic " + new Buffer(this.consumer_key + ":" + this.consumer_secret).toString("base64");
const httpOptions = {
headers: new HttpHeaders({
'Authorization': this.auth,
'Access-Control-Allow-Origin': "http://localhost:4200",
"Authorization": this.auth,
"Content-Type": "application/json"
})
};
return this.http.get<authToken>(this.url,
return this.http.get<authToken>("auth",
httpOptions
).subscribe(
body => {
this.result = body;
this.oAuthToken = this.result.access_token;
this.oAuthExp = this.result.expires_in;
console.log(this.oAuthExp, this.oAuthToken)
}
);
}

lipaFunction(ammount) {
this.lipaAuth = "Bearer " + this.oAuthToken;

this.timeStamp = new Date()

this.time = this.datePipe.transform(this.timeStamp, 'yyyyMMddhhmmss')
this.passWord = btoa(this.BusinessShortcode + this.passKey + this.time)
console.log(this.passWord)

this.json = {
"BusinessShortCode": this.BusinessShortcode,
"Password": this.passWord,
"Timestamp": this.time,
"TransactionType": "CustomerPayBillOnline",
"Amount": ammount,
"PartyA": "254701737488",
"PartyB": this.BusinessShortcode,
"PhoneNumber": "254701737488",
"CallBackURL": "https://console.firebase.google.com/project/research-locus/database/research-locus/data/users/2uSXI3Nv8fgaSeiVVmc9wrSqoQJ2",
"AccountReference": "test",
"TransactionDesc": "Pay to Researh Locus and begin your transaction"
}
this.httpOptions = {
headers: new HttpHeaders({
'Authorization': this.lipaAuth,
'Content-Type': 'application/json',
"Access-Control-Allow-Methods": "POST, GET, OPTIONS",
'Accept': 'text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8',
'Access-Control-Allow-Headers': 'X-Custom-Header, Upgrade-Insecure-Requests,origin, x-requested-with',
'Access-Control-Allow-Origin': "*"

})
};
return this.http.post("lipa", this.json, this.httpOptions);

}

}
28 changes: 0 additions & 28 deletions src/app/services/mpesascript.js

This file was deleted.

20 changes: 20 additions & 0 deletions src/proxy.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"/auth": {
"target": "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials",
"secure": false,
"logLevel":"debug",
"pathRewrite": {
"^/auth": ""
},
"changeOrigin": true
},
"/lipa": {
"target": "https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest",
"secure": false,
"logLevel":"debug",
"pathRewrite": {
"^/lipa": ""
},
"changeOrigin": true
}
}

0 comments on commit 8bc124c

Please sign in to comment.