Giter Club home page Giter Club logo

khalti-sdk-ios's Introduction

Khalti

IDE Language Version License Platform

Requirements

This system is tested on Xcode 8, 9 and 10 with swift version 3, 4 and 4.2.

Xcode: >=8

Swift: 3, 4 and 4.2

Example

Swift : Clone project and use 'Example' directory. Do 'pod install'.

Objective-C : Clone project and use 'Example Obj-c' directory. Do 'pod install'.

Installation guide

Khalti is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Khalti'

Usage

Adding CustomSchme (Needed only if either card and/or ebanking are made available)

Khalti uses custom Scheme: So merhant should setup URLScheme unique for their app. We have made usability as of user case.

Khalti scheme setup overview

After adding Url Scheme create global constant for same customUrlScheme as below

let khaltiUrlScheme:String = "KhaltiPayExampleScheme"

Requirements

To work around with this redirection you have to implement some openUrl in Appdelegate.swift.

Khalti.shared.defaultAction() returns true if you initiate payment through Khalti. Khalti.shared.action(with: url) is needed for complete action after ebanking and card payment. (Needed only if either card and/or ebanking are made available)

Note: Using Khalti.shared.action(with: url) is needed only if either card and/or ebanking are made available.

Add following code to Appdelegate.swift

 func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    Khalti.shared.action(with: url)
    return Khalti.shared.defaultAction() // Or true 
}

Using at particular ViewController

At your viewController during action of pay add initiate config file

When instantiating Config pass public key, product id, product name, amount (in paisa). Product web url and additional data are optional.

let TEST_CONFIG:Config = Config(publicKey: khaltiMerchantKey, amount: 1000, productId: "1234567890", productName: "Dragon_boss", productUrl: "http://gameofthrones.wikia.com/wiki/Dragons",additionalData: additionalData)
// Data passed here are based on Example project

Note: Public is provided to every merchant of khalti. Intially test is available to every merchant and live key is provided after MoU signup with Khalti.

At this stage the scheme named you declared earlier is passed to Khatli.shared.appUrlScheme

Khalti.shared.appUrlScheme = khaltiUrlScheme // see above for file khaltiUrlScheme
// This can be used at appdelegate during didfinishlaunching. 
// This must be included only if either card and/or ebanking are made available.

Finally present the khaltiPay Viewcontroller by calling public funcation

Khalti.present(caller: self, with: TEST_CONFIG, delegate: self)

Params of present function of Khalti

param Value
caller viewController from where you initate payment.
with Config object
delegate self

Delegate must be assigned to same Viewcontroller to get callback action from KhaltiPayDelegate.

Example as used in Example Project

let extra:[String : Any] =  ["no":false,"yes":true,"int" : 0, "float":12.23]
        
let jsonData = try? JSONSerialization.data(withJSONObject: extra, options: JSONSerialization.WritingOptions())
let jsonString = String(data: jsonData!, encoding: .utf8)!
        
let additionalData:Dictionary<String,String> = [
    "merchant_name" : "Merchant Company Name",
    "merchant_extra" : jsonString
]
        
Khalti.shared.appUrlScheme = khaltiUrlScheme
let khaltiMerchantKey = "test_public_key_dc74e0fd57cb46cd93832aee0a507256" // This key is from local server so it won't work if you use the example as is it. Use your own public test key
        
let TEST_CONFIG:Config = Config(publicKey: khaltiMerchantKey, amount: 1000, productId: "1234567890", productName: "Dragon_boss", productUrl: "http://gameofthrones.wikia.com/wiki/Dragons",additionalData: additionalData, ebankingPayment:false) // This makes only khalti Payment available
Khalti.present(caller: self, with: TEST_CONFIG, delegate: self)

Config file has property ebankingPayment with default value true,, indication the cardPayment facility is available.If you want ebankingPayment nto available to your users then set ebankingPayment option to false while creating config object. And config file also support property cardPayment with default value false, indication the cardPayment facility is not available. If you want cardPayment available to your users then set cardPayment option to true while creating config object. Please read merchant terms and conditions before enabling this feature. Additionally, Config class also accepts a Dictionary<String,String> which you can use to pass any additional data. Make sure you add a merchant_ prefix in your map key.

Using delegates

The viewController you implement pay action should contain KhaltiPayDelegate implementing onCheckOutSuccess(data: Dictionary<String, Any>) onCheckOutError(action: String, message: String)

extension YourViewController: KhaltiPayDelegate {
    func onCheckOutSuccess(data: Dictionary<String, Any>) {
        print(data)
        print("Oh there is success message received")
    }
    
    func onCheckOutError(action: String, message: String) {
        print(action)
        print(message)
        print("Oh there occure error in payment")
    }
}

Summary

Callback Methods

Method Description
onCheckOutSuccess(data: Dictionary<String, Any>) This method is called when a transaction has been completed and confirmed by the user. A dictionary containing an access token, required to verify the transaction and data passed through Config instance is returned. Once this method is called, use the access token to verify the transaction. Please follow the verification process for further instructions.
onCheckOutError(action: String, message: String, data:Dictionary<String,Any>?) This method is called when an error occurs during payment initiation and confirmation. Action, message and errordata value is passed where action defines, the current action being performed, message defines the error and data defines the errorData associated with error if exists.
Response Sample
Success Messsage
Key Value Type
mobile 98XXXXXXXX String
product_name Product Name String
product_identity Product Id String
product_url Product Url String
amount 100 Int
token token String
cardPayment false Bool

The success message also contains all the key and value provide as extra data while initiating Config.

Error Messsage
Variable Description Type
action initiate/confirm/ebanking String
message Detail Error Message String
data data of error (Optional) Dictionary<String,Any>?

Support

For Queries, feel free to mail us at: here

Check out the API Documentation.

khalti-sdk-ios's People

Contributors

abnormalbbk avatar bikash-giri avatar rjndra avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

khalti-sdk-ios's Issues

Swift 4

Please update your sdk for swift 4 I get
Use of unresolved identifier 'NSForegroundColorAttributeName'
I don't want to have to integrate the sdk manually instead of cocoapods.

Requesting onDismiss/onCancel Delegate

I am requesting a delegate to be called whenever the user presses the cancel button to dismiss the viewController.

Right now we only have 2 delegates: onCheckOutSuccess and onCheckOutError

Pay amount not being displayed in decimal

As stated in the title, even when the amount is sent (eg. 83150) the SDK only displays 831 and not as 831.50 which was the intended payment amount.

The issue here is in displaying the correct value to the user. If the user see's "831" instead of "831.50" and then he/she is charged "831.50", this might create a skepticism among the users.

This is the screen from the example as it is currently.

Simulator Screen Shot - iPhone 11 - 2020-06-10 at 19 37 04

After changing the line 57 in KhaltiPaymentViewController.swift from

self.payInitiateButton.setTitle("PAY RS \(amount/100)", for: .normal)

to

self.payInitiateButton.setTitle("PAY RS \(Float(Float(amount)/Float(100)))", for: .normal)

then i got the desired result.

Simulator Screen Shot - iPhone 11 - 2020-06-10 at 19 44 03

Seems like this issue is just when displaying the amount value on the screen. Even it displays "831" instead of "831.50", the payment amount is 831.50, which is the correct amount value.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.