Skip to content

Commit

Permalink
Merge pull request #1 from Shek863/main
Browse files Browse the repository at this point in the history
Migration to the new version of the widget
  • Loading branch information
sethgnavo authored Oct 15, 2022
2 parents 2c91d78 + 3c90c01 commit b5c8a9b
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/kkiapay-ios-sdk.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 28 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ import KKiaPaySDK;

```swift
private var kkiaPay: KKiaPay{
KKiaPay(amount: "3000",
phone: "97000000",
data: "Hello world",
KKiaPay(amount: 3000,
phone: "22997000000",
publicAPIKey: "xxxxxxxxxxxxxxxxxxx",
partnerId: "AxXxXXxId",
countries: ["BJ"],
paymentMethods: ["momo","card"],
data: "Hello world",
sandbox: true,//set this to false in production
theme: "#4E6BFC",
name: "John Doe",
Expand All @@ -40,7 +43,7 @@ private var kkiaPay: KKiaPay{
}
```

##### Get the transaction data back
##### Get the transaction data back
Once the payment is successful, the KKiaPayViewModel sends a KKiaPayTransactionData to the calling view via the onReceive callback.
```swift
kkiaPay.onReceive(self.viewModel.paymentData.receive(on: RunLoop.main)){paymentData in
Expand All @@ -54,7 +57,7 @@ kkiaPay.onReceive(self.viewModel.paymentData.receive(on: RunLoop.main)){paymentD
}
```

The onReceive function of the kkiaPay instance listens to the state of the paymentData and exposes it.
The onReceive function of the kkiaPay instance listens to the state of the paymentData and exposes it.

## Example

Expand All @@ -66,13 +69,17 @@ struct ContentView: View {
//Create a view model instance to use later
@ObservedObject var viewModel = KKiaPayViewModel()
@State private var showWebView = false
@State var text = "Pay Now"

//Initialise the Kkiapay Instance
private var kkiaPay: KKiaPay{
KKiaPay(amount: "3000",
phone: "97000000",
data: "Hello world",
KKiaPay(amount: 3000,
phone: "22997000000",
publicAPIKey: "xxxxxxxxxxxxxxxxxxx",
partnerId: "AxXxXXxId",
countries: ["BJ"],
paymentMethods: ["momo","card"],
data: "Hello world",
sandbox: true,//set this to false in production
theme: "#4E6BFC",
name: "John Doe",
Expand All @@ -86,20 +93,24 @@ struct ContentView: View {
Button {
showWebView.toggle()
} label: {
Text("Pay")
Text(self.text)
}
.sheet(isPresented: $showWebView) {

//Get the transaction data back
kkiaPay.onReceive(self.viewModel.paymentData.receive(on: RunLoop.main)){paymentData in

if(paymentData.isSuccessful){
print("The amount of the transaction is " + paymentData.amount+" with id "+paymentData.transactionId)

self.text = "PAYMENT WAS SUCCESSFUL \n\nThe amount of the transaction is \(paymentData.amount) Fcfa with id \(paymentData.transactionId)"

print("The amount of the transaction is \(paymentData.amount) with id \(paymentData.transactionId)")
showWebView = false
}else{
}else {
print("The payment was not successful")
}
}

}
}
}
Expand All @@ -116,17 +127,19 @@ struct ContentView_Previews: PreviewProvider {

<table>
<tr><td>Argument</td><td>Type</td><td>Required</td><td>Details</td></tr>
<tr><td>phone</td><td>String</td><td>Yes</td><td>Valid mobile money number to debit. ex : 22997000000 </td></tr>
<tr><td>phone</td><td>String</td><td>Yes</td><td>Valid mobile money number to debit. ex : 22967434270 </td></tr>
<tr><td>amount</td><td>Numeric</td><td>Yes</td><td>Amount to debit from user account (XOF) </td></tr>
<tr><td>name</td><td>String</td><td>No</td><td>Client firstname and lastname </td></tr>
<tr><td>email</td><td>String</td><td>No</td><td>Client email address </td></tr>
<tr><td>partnerId</td><td>String</td><td>No</td><td>Your id to find transaction</td></tr>
<tr><td>countries</td><td>List of String</td><td>No</td><td>Set widget countries ex: ["CI"] </td></tr>
<tr><td>paymentMethods</td><td>List of String</td><td>No</td><td>Set widget payment methods ex: ["momo","card"] </td></tr>
<tr><td>theme</td><td>String</td><td>No</td><td> the hexadecimal code of the color you want to give to your widget </td></tr>
<tr><td>apikey</td><td>String</td><td>Yes</td><td>public api key</td></tr>
<tr><td>sandbox</td><td>Boolean</td><td>No</td><td>The true value of this attribute allows you to switch to test mode</td></tr>
<tr><td>successCallback</td><td>Function</td><td>No</td><td>This function is called once the payment has been successfully made</td></tr>
<tr><td>successCallback</td><td>Function</td><td>Yes</td><td>This function is called once the payment has been successfully made</td></tr>
</table>

### Issues and feedback

Please file [issues](https://github.com/kkiapay/kkiapay-ios-sdk/issues/new)
to send feedback or report a bug. Thank you!
to send feedback or report a bug. Thank you!
64 changes: 36 additions & 28 deletions Sources/KKiaPaySDK/KKiaPay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ public struct KKiaPay: UIViewRepresentable {
Coordinator(self)
}

let amount:String?
let amount:Int?
let phone:String?
let data:String?
let publicAPIKey:String?
let sandbox:Bool?
let theme:String?
let name:String?
let email:String?
let partnerId:String?
let countries:[String]?
let paymentMethods:[String]?
let callback:String?

@ObservedObject public var viewModel : KKiaPayViewModel
public init(amount:String,

public init(amount:Int,
phone:String,
publicAPIKey:String,
partnerId:String?,
countries:[String]?,
paymentMethods:[String]?,
data:String,
sandbox:Bool,
theme:String,
Expand All @@ -43,6 +49,9 @@ public struct KKiaPay: UIViewRepresentable {
self.amount=amount
self.phone=phone
self.publicAPIKey=publicAPIKey
self.countries=countries
self.partnerId=partnerId
self.paymentMethods=paymentMethods
self.data=data
self.sandbox=sandbox
self.theme=theme
Expand All @@ -51,70 +60,69 @@ public struct KKiaPay: UIViewRepresentable {
self.callback=callback
self.viewModel=viewModel
}

private func base64EncodedUrl() -> String {

let encodedData = Data("{\"amount\":\"\(amount ?? "")\",\"phone\": \"\(phone ?? "")\",\"data\": \"\(data ?? "")\",\"key\": \"\(publicAPIKey ?? "")\",\"sandbox\":\(sandbox ?? true),\"theme\": \"\(theme ?? "")\", \"name\": \"\(name ?? "")\", \"email\": \"\(email ?? "")\",\"callback\": \"\(callback ?? "")\",\"sdk\":\"ios\",\"reason\":\"\"}".utf8).base64EncodedString()

return "https://widget-v2.kkiapay.me/?="+encodedData


let encodedData = Data("{\"paymentMethods\":\(paymentMethods ?? ["momo","card"]),\"countries\":\(countries ?? ["BJ","CI"]),\"position\":\"left\",\"partnerId\":\"\(partnerId ?? "")\",\"serviceId\":\"INTEGRATION\",\"amount\":\(amount ?? 1),\"phoneNumber\": \"\(phone ?? "")\",\"data\": \"\(data ?? "")\",\"key\": \"\(publicAPIKey ?? "")\",\"sandbox\":\(sandbox ?? true),\"theme\":\"\(theme ?? "")\",\"fullname\":\"\(name ?? "")\",\"email\": \"\(email ?? "")\",\"callback\": \"\(callback ?? "")\",\"sdk\":\"ios\",\"reason\":\"\"}".utf8).base64EncodedString()
return "https://widget-v3.kkiapay.me/?"+encodedData

}

public func makeUIView(context: Context) -> WKWebView {

let preferences = WKPreferences()
preferences.javaScriptEnabled = true

let configuration = WKWebViewConfiguration()
configuration.preferences = preferences

let webView = WKWebView(frame: CGRect.zero, configuration: configuration)
webView.navigationDelegate = context.coordinator
webView.allowsBackForwardNavigationGestures = true
webView.scrollView.isScrollEnabled = true

return webView
}

public func updateUIView(_ webView: WKWebView, context: Context) {
let url = URL(string: base64EncodedUrl())
let request = URLRequest(url: url!)
webView.load(request)
}

public class Coordinator : NSObject, WKNavigationDelegate {
var parent: KKiaPay
var valueSubscriber: AnyCancellable? = nil
var webViewNavigationSubscriber: AnyCancellable? = nil

init(_ uiWebView: KKiaPay) {
self.parent = uiWebView
}

deinit {
valueSubscriber?.cancel()
webViewNavigationSubscriber?.cancel()
}

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}

// This function is essential for intercepting every navigation in the webview
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

let urlString = navigationAction.request.url?.absoluteString

if(urlString!.contains("transaction_id")){//Payment successful
print("KKIAPAYSDK: PAYMENT WAS SUCCESSFUL")

let transactionId = getQueryStringParameter(url: urlString!, param: "transaction_id")
self.parent.viewModel.paymentData.send(KKiaPayData(amount: self.parent.amount ?? "",

self.parent.viewModel.paymentData.send(KKiaPayData(amount: "\(self.parent.amount ?? 1)",
transactionId: transactionId ?? "",isSuccessful:true))
}else{
self.parent.viewModel.paymentData.send(KKiaPayData(amount: self.parent.amount ?? "",transactionId: "",isSuccessful:false))

self.parent.viewModel.paymentData.send(KKiaPayData(amount: "\(self.parent.amount ?? 1)",transactionId: "",isSuccessful:false))
}

// This allows the navigation
Expand Down

0 comments on commit b5c8a9b

Please sign in to comment.