Created
March 12, 2022 16:01
-
-
Save prestigegodson/40c61e0f8fc469079938c1722a58534d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component, OnInit, ViewChild} from '@angular/core'; | |
import {SharedService} from '../../../services/shared-service/shared.service'; | |
import {Constants} from '../../../util/Constants'; | |
import {AnonymousService} from '../../../services/anonymous-service'; | |
import {AuthService} from '../../../services/auth-service/auth-service.service'; | |
import {MdSnackBar, MdSnackBarConfig} from '@angular/material'; | |
import {FormBuilder, FormGroup, FormGroupDirective, Validators} from '@angular/forms'; | |
import {ErrorService} from '../../../util/error_service/error.service'; | |
import {ToastService} from '../../../util/toast-service/toast.service'; | |
import {MobileNetworks} from '../../../util/mobile-networks'; | |
import {TransactionsResponse} from '../../../util/models/transactionsResponse.model'; | |
import {FlutterWaveResponse} from '../../../util/models/flutterwaveResponse.model'; | |
import {RechargeResponse} from "../../../util/models/rechargeResponse.model"; | |
import {Router} from '@angular/router'; | |
declare var window: any; | |
@Component({ | |
selector: 'app-guest-airtime', | |
templateUrl: './guest-airtime.component.html', | |
styleUrls: ['./guest-airtime.component.scss'] | |
}) | |
export class GuestAirtimeComponent implements OnInit { | |
public airtimeForm: FormGroup; | |
mobnumPattern = '^((\\+91-?)|0)?[0-9]{7,14}$'; | |
@ViewChild(FormGroupDirective) _airtimeForm; | |
public networksWithId = []; | |
public networksLogoPath: any = []; | |
public setId = null; | |
public isVerifyDetails: boolean; | |
public networks: any; | |
public isloading: boolean; | |
public isLoadingNetworks: boolean; | |
public errorResponse: any; | |
public userType : string; | |
public isRecharging : boolean; | |
public switchState: string; | |
getpaidSetup: any; | |
ravePay:any; | |
transactionID: string; | |
public selectedNetwork: string; | |
public amount: number; | |
public mobile: number; | |
public network: any; | |
private previousRechargeObj: any; | |
public rechargeObj : any = { | |
'amount': 0, | |
'fromWallet': '', | |
'networkId': 0, | |
'receiverMsisdn': '', | |
'networkName': '' | |
}; | |
constructor( public _sharedService : SharedService, public anonymousService: AnonymousService, | |
public snackBar: MdSnackBar,public error: ErrorService, public authService: AuthService, public fb: FormBuilder, public errorService: ErrorService, | |
public toast: ToastService, public router : Router) { | |
this.isLoadingNetworks = false; | |
this.isVerifyDetails = false; | |
this.switchState = 'recharge'; | |
this.networks = []; | |
this.networksLogoPath = MobileNetworks.networksLogoPath; | |
this.isloading = false; | |
this.createForm(); | |
} | |
createForm() { | |
this.airtimeForm = this.fb.group({ | |
amount: ['', Validators.required], | |
mobile: ['', [Validators.required, Validators.pattern(this.mobnumPattern)]], | |
networkId: ['', [Validators.required]], | |
}); | |
} | |
//// Sort data //////////////////// | |
getNetworksWithId() { | |
this.networksWithId = []; | |
this.networks.sort(function(a, b) { | |
return - ( a.id - b.id || a.networkName.localeCompare(b.networkName) ); | |
}); | |
for (let i = 0; i < this.networks.length; i++ ) { | |
const sortedNetworks: any = {id: 0, logoPath: '', data: {}}; | |
sortedNetworks.id = i + 1; | |
sortedNetworks.logoPath = this.networksLogoPath[this.networks[i]['networkCodeName']]; | |
sortedNetworks.data = this.networks[i]; | |
this.networksWithId.push(sortedNetworks); | |
this.isLoadingNetworks = false; | |
} | |
console.log('************ Networks with id ****************'); | |
console.log(this.networksWithId); | |
localStorage.setItem('networks', JSON.stringify(this.networksWithId)); | |
} | |
// | |
setNetwork(id : any){ | |
const networkName = this.networksWithId.filter((network) => network.data.id == id)[0].data.networkName; | |
this.selectedNetwork = networkName; | |
console.log(networkName); | |
} | |
getNetworkName(id:any){ | |
return this.networksWithId.filter((network) => network.data.id == id)[0].data.networkName; | |
} | |
// ////////////////// get all networks info //////////////////////////////////////////////////// | |
private getAllNetworks = () => { | |
this.isLoadingNetworks = true; | |
this.anonymousService.getNetworks().subscribe( | |
response => { | |
console.log('************ Networks list ****************'); | |
console.log(response.data); | |
this.networks = response.data; | |
this.getNetworksWithId(); | |
}, | |
err => { | |
console.log(err); | |
this.errorResponse = this.error.errorHandlerWithText(this.getAllNetworks, err); | |
this.isLoadingNetworks = false; | |
console.log(this.errorResponse); | |
} | |
) | |
}; | |
private generateTransaction = () => { | |
this.isRecharging = true; | |
this.anonymousService.generateTransactionId().subscribe( | |
response => { | |
console.log('************ generateTransactionId ****************'); | |
console.log(response.data[0].transactionId); | |
this.transactionID = response.data[0].transactionId; | |
this.payWithRave(); | |
}, | |
err => { | |
console.log(err); | |
this.error.errorHandlerWithText(this.generateTransaction, err); | |
} | |
) | |
}; | |
checkNum() { | |
if (this.airtimeForm.value.mobile.length !== 11) { | |
this.airtimeForm.controls.mobile.setErrors({'numberValid': true}); | |
} | |
} | |
resetForm(){ | |
this._airtimeForm.resetForm(); | |
this._airtimeForm.form.markAsPristine(); | |
this._airtimeForm.form.markAsUntouched(); | |
this._airtimeForm.form.updateValueAndValidity(); | |
} | |
// | |
backToStart(){ | |
this.isVerifyDetails = false; | |
} | |
// | |
// | |
// Submit recharge form | |
onSubmit() { | |
this.isloading = true; | |
this.rechargeObj.networkId = this.network; | |
this.rechargeObj.receiverMsisdn = this.mobile; | |
this.rechargeObj.amount = this.amount; | |
this.rechargeObj.networkName = this.getNetworkName(this.network); | |
localStorage.setItem(Constants.CUSTOMER_KEY + 'recharge' , JSON.stringify(this.rechargeObj)); | |
// this.generateTransactionId(); | |
this.switchState = 'recharge-verify'; | |
console.log('recharge Request', this._airtimeForm.value); | |
console.log('recharge rechargeObj', this.rechargeObj); | |
} | |
// | |
ngOnInit() { | |
this.getAllNetworks(); | |
} | |
goToPage(){ | |
this.switchState = 'recharge'; | |
} | |
// | |
// | |
// | |
public payWithRave() { | |
this.isRecharging = false; | |
console.log('paying'); | |
var ravePay = window.getpaidSetup({ | |
PBFPubKey: Constants.CUSTOMER_GUEST_PAYMENT_KEY, | |
customer_email: 'support@onerecharge.com', | |
amount: this.rechargeObj.amount, | |
customer_phone: this.rechargeObj.receiverMsisdn, | |
currency: 'NGN', | |
payment_method: 'both', | |
txref: this.transactionID, | |
meta: [{ | |
metaname: 'flightID', | |
metavalue: 'AP1234' | |
}], | |
onclose: function() { | |
}, | |
callback: function(response) { | |
const txref = response.tx.txRef; // collect flwRef returned and pass to a server page to complete status check. | |
console.log('txref ', txref); | |
console.log('This is the response returned after a charge', response); | |
const flutterResponse = new FlutterWaveResponse(response.data, response.name, response.success, response.tx); | |
if ( response.tx.chargeResponseCode === '00' || response.tx.chargeResponseCode === '0' ) | |
{ | |
// redirect to a success page | |
const guestRechargeObj = { | |
'amount': 0, | |
'cardLast4Digit': '', | |
'networkId': 0, | |
'paymentId': 0, | |
'receiverMsisdn': '', | |
'status': '', | |
'transactionId': '', | |
'transactionRef': '' | |
}; | |
const previousRechargeObj: any = JSON.parse(localStorage.getItem(Constants.CUSTOMER_KEY + 'recharge')); | |
guestRechargeObj.receiverMsisdn = previousRechargeObj.receiverMsisdn; | |
guestRechargeObj.networkId = previousRechargeObj.networkId; | |
guestRechargeObj.amount = previousRechargeObj.amount; | |
guestRechargeObj.paymentId = flutterResponse.tx.paymentId; | |
guestRechargeObj.transactionId = flutterResponse.tx.txRef; | |
guestRechargeObj.status = flutterResponse.tx.status; | |
const paymentObj = { | |
'payload' : guestRechargeObj, | |
'type': 'airtime', | |
'homeUrl' : '/', | |
'packageTransactionUrl' : '', | |
'repeatTransactionUrl' : '' | |
} | |
console.log('paymentObj', paymentObj); | |
localStorage.setItem(Constants.GUEST_KEY + 'paymentObjKey', JSON.stringify(paymentObj)); | |
window.location.href = '#/payment-status'; | |
ravePay.close(); | |
} | |
else { | |
// redirect to a failure page. | |
} | |
// this.ravePay.close(); // use this to close the modal immediately after payment. | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment