SDK Usage (iOS / Android)

SDK Guide for iOS / Android

Demo project


SDK iOS Installation / Usage

Cocoapod

You can download MFSDK by adding this line to your Podfile

pod 'MyFatoorah'
pod repo update
pod install
pod 'MyFatoorah'
pod repo update
pod install

Swift Package Manager

You can download MFSDK by adding the https://dev.azure.com/myfatoorahsc/_git/MF-SDK-iOS-Demo repository as a Swift Package

Import framework in AppDelegate:

import MFSDK
import MFSDK

Add below code in the didFinishLaunchingWithOptions method:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// set up your My Fatoorah Merchant details
MFSettings.shared.configure(token: <#Put your token here#>, country: <# Country of your account #>, environment: <# Test or Live #>)


// you can change color and title of nvgigation bar
let them = MFTheme(navigationTintColor: .white, navigationBarTintColor: .lightGray, navigationTitle: "Payment", cancelButtonTitle: "Cancel")
MFSettings.shared.setTheme(theme: them)
return true
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// set up your My Fatoorah Merchant details
    [[MFSettings shared] configureWithToken:@<#Put your token here#> country: <#Country of your account#>  environment:<#Live or Test#>];


// you can change color and title of nvgigation bar
MFTheme* theme = [[MFTheme alloc] initWithNavigationTintColor:[UIColor whiteColor] navigationBarTintColor:[UIColor lightGrayColor] navigationTitle:@"Payment" cancelButtonTitle:@"Cancel"];
[[MFSettings shared] setThemeWithTheme:theme];

return YES;
}

SDK Android Installation / Usage

1- Add mavenCentral() under allprojects/repositories and buildscript/repositories in the project Gradle file.

2-Add the following dependency in the app Gradle file:

implementation 'com.myfatoorah:myfatoorah:2.2.7'

Add bellow line in the onCreate() method of your Application class:

// set up your My Fatoorah Merchant details
MFSDK.INSTANCE.init("Put you Token API Key here", MFCountry.KUWAIT, MFEnvironment.TEST);
// set up your My Fatoorah Merchant details
MFSDK.init("Put you Token API Key here", MFCountry.KUWAIT, MFEnvironment.TEST)

Add bellow code in the onCreate() method of your Activity:

// You can custom your action bar, but this is optional not required to set this line
 MFSDK.INSTANCE.setUpActionBar("MyFatoorah Payment", R.color.toolbar_title_color, R.color.toolbar_background_color, true);
// You can custom your action bar, but this is optional not required to set this line
MFSDK.setUpActionBar("MyFatoorah Payment", R.color.toolbar_title_color, R.color.toolbar_background_color, true)
// To hide action bar
// MFSDK.setUpActionBar(isShowToolBar = false)

📘

After Testing

Once your testing is finished, simply replace environment from TEST to LIVE and the API URL with the live, click here for more information.


Initiate/Execute Payment

As described earlier for the Gateway Integration, we are going to have the SDK integrated with the same steps to make a successful integration with the SDK.

🚧

Initiate Payment

As a good practice, you don't have to call the Initiate Payment function every time you need to execute payment, but you have to call it at least once to save the PaymentMethodId that you will need to call Execute Payment

// initiatePayment 
        let invoiceValue = 5.0
        var selectedPaymentMethod = 1
        let initiatePayment = MFInitiatePaymentRequest(invoiceAmount: invoiceValue, currencyIso: .kuwait_KWD)
        MFPaymentRequest.shared.initiatePayment(request: initiatePayment, apiLanguage: .english) { [weak self] (response) in
            switch response {
            case .success(let initiatePaymentResponse):
                var paymentMethods = initiatePaymentResponse.paymentMethods
                if let paymentMethods = initiatePaymentResponse.paymentMethods, !paymentMethods.isEmpty {
                    selectedPaymentMethod = paymentMethods[0].paymentMethodId
                }
            case .failure(let failError):
                print(failError)
            }
        }
        
    
    // executePayment 
        let request = MFExecutePaymentRequest(invoiceValue: invoiceValue, paymentMethod: paymentMethod.paymentMethodId)
        
        // Uncomment this to add ptoducts for your invoice
        // var productList = [MFProduct]()
        // let product = MFProduct(name: "ABC", unitPrice: 1, quantity: 2)
        // productList.append(product)
        // request.invoiceItems = productList
        
        MFPaymentRequest.shared.executePayment(request: request, apiLanguage: .english) { [weak self] (response,invoiceId) in
            switch response {
            case .success(let executePaymentResponse):
                print("\(executePaymentResponse.invoiceStatus ?? "")")
            case .failure(let failError):
                print(failError)
            }
        }


// ```MFPaymentDelegate```
// You can conform this protocol to listen for invoice status, now this protocol has `didInvoiceCreated`
// method to get the invoice id immediately after creating the invoice. 

class ViewController: UIViewController {

    override func viewDidLoad() {
    
        // Set delegate for your view controller
        MFSettings.shared.delegate = self
    }
}

// conforms ```MFPaymentDelegate```
extension ViewController: MFPaymentDelegate {
    func didInvoiceCreated(invoiceId: String) {
        print("#\(invoiceId)")
    }
}
double invoiceValue = 5.0;
        MFInitiatePaymentRequest* initiatePayment = [[MFInitiatePaymentRequest alloc] initWithInvoiceAmount:invoiceValue currencyIso:MFCurrencyISOKuwait_KWD];
        [[MFPaymentRequest shared] initiatePaymentWithRequest:initiatePayment apiLanguage:MFAPILanguageEnglish completion:^(MFInitiatePaymentResponse * initiatePaymentResponse, MFFailResponse * failResponse) {
            if (initiatePaymentResponse != NULL) {
                if ([initiatePaymentResponse.paymentMethods count] > 0) {
                    NSLog(@"%@",initiatePaymentResponse);
                }
            } else {
                    NSLog(@"%@",failResponse);
                    
                }
            }
        }];
    
        MFExecutePaymentRequest* request = [[MFExecutePaymentRequest alloc] initWithInvoiceValue:invoiceValue paymentMethod:_paymentMethodId];
        //         Uncomment this to add ptoducts for your invoice
        // NSMutableArray* productList = [[NSMutableArray alloc] init];
        // MFProduct* product = [[MFProduct alloc] initWithName:@"ABC" unitPrice:1 quantity:2];
        // [productList addObject:product];
        // request.invoiceItems = productList;
        [[MFPaymentRequest shared] executePaymentWithRequest:request apiLanguage:MFAPILanguageEnglish completion:^(MFPaymentStatusResponse * paymentStatusResponse, MFFailResponse * failResponse, NSString* invoiceId) {
            if (paymentStatusResponse != NULL) {
                NSLog(@"%@",paymentStatusResponse);
            } else {
                NSLog(@"%@",failResponse);
            }
        }];


// ```MFPaymentDelegate```
// You can conform this protocol to listen for invoice status, now this protocol has `didInvoiceCreated`
// method to get the invoice id immediately after creating the invoice. 

// ViewController.h
@interface ViewController : UIViewController<MFPaymentDelegate>
.
.
@end


// ViewController.m
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [MFSettings shared].delegate = self;
}

// conforms `MFPaymentDelegate`
- (void)didInvoiceCreatedWithInvoiceId:(NSString * _Nonnull)invoiceId {
    NSLog(@"invoice id: %@", invoiceId);
}
// initiatePayment 

MFInitiatePaymentRequest request = new MFInitiatePaymentRequest(0.100, MFCurrencyISO.KUWAIT_KWD);
MFSDK.INSTANCE.initiatePayment(
    request,
    MFAPILanguage.EN,
    (MFResult<MFInitiatePaymentResponse> result) -> {
        if (result instanceof Success) {
            Log.d(TAG, "Response: " + new Gson().toJson(
                    ((Success<MFInitiatePaymentResponse>) result).getResponse()));
        } else if (result instanceof Fail) {
            Log.d(TAG, "Error: " + new Gson().toJson(((Fail) result).getError()));
        }

        return Unit.INSTANCE;
    });


// executePayment 

MFExecutePaymentRequest request = new MFExecutePaymentRequest(1, 0.100);
MFSDK.INSTANCE.executePayment(
        this,
        request,
        MFAPILanguage.EN,
        (String invoiceId) -> {
            Log.d(TAG, "invoiceId: " + invoiceId);
            return Unit.INSTANCE;
        },
        (String invoiceId, MFResult<MFGetPaymentStatusResponse> result) -> {
            if (result instanceof Success)
                Log.d(TAG, "Response: " + new Gson().toJson(((Success<MFGetPaymentStatusResponse>) result).getResponse()));
            else if (result instanceof Fail)
                Log.d(TAG, "Error: " + new Gson().toJson(((Fail) result).getError()));

            return Unit.INSTANCE;
        });
// initiatePayment 

val request = MFInitiatePaymentRequest(0.100, MFCurrencyISO.KUWAIT_KWD)
MFSDK.initiatePayment(
    request,
    MFAPILanguage.EN
) { result: MFResult<MFInitiatePaymentResponse> ->
    when (result) {
        is Success ->
            Log.d(TAG, "Response: " + Gson().toJson(result.response))
        is Fail ->
            Log.d(TAG, "Fail: " + Gson().toJson(result.error))
    }
}
    

// executePayment 

val request = MFExecutePaymentRequest(1, 0.100)
MFSDK.executePayment(
    this,
    request,
    MFAPILanguage.EN,
    onInvoiceCreated = {
        Log.d(TAG, "invoiceId: $it")
    }
) { invoiceId: String, result: MFResult<MFGetPaymentStatusResponse> ->
    when (result) {
        is Success -> 
            Log.d(TAG, "Response: " + Gson().toJson(result.response))
        is Fail -> 
            Log.d(TAG, "Fail: " + Gson().toJson(result.error))
    }
}

Direct Payment/Tokenization

As we have explained earlier in the [Direct Payment] integration and how it works, it also has the same scenario for the SDK implementation, you have to know the following steps to understand how it works:

  • Get the payment method that allows Direct Payment by calling initiatePayment to get paymentMethodId
  • Collect card info from user MFCardInfo(cardNumber: "51234500000000081", cardExpiryMonth: "05", cardExpiryYear: "21", cardSecurityCode: "100", saveToken: false)
  • If you want to save your credit card info and get a token for next payment you have to set saveToken: true and you will get the token in the response read more in Tokenization
  • If you want to execute a payment through a saved token you have use MFCardInfo(cardToken: "put your token here")
  • Now you are ready to execute the payment, please check the following sample code
let card = MFCardInfo(cardNumber: "51234500000000081", cardExpiryMonth: "05", cardExpiryYear: "21", HolderName: "John", cardSecurityCode: "100", saveToken: true) //MFCardInfo(cardToken: "token")
         // card.bypass = false // default is true
        let invoiceValue = 5.0
        
        let paymentMethod = 2 // if you don't Know this you have to call initiatePayment
        let request = MFExecutePaymentRequest(invoiceValue: invoiceValue, paymentMethod: 2)
        MFPaymentRequest.shared.executeDirectPayment(request: request, cardInfo: card, apiLanguage: .english) { [weak self] response, invoiceId in
            switch response {
            case .success(let directPaymentResponse):
                if let cardInfoResponse = directPaymentResponse.cardInfoResponse, let card = cardInfoResponse.cardInfo {
                    print("Status: with card number \(card.number)")
                }
                if let invoiceId = invoiceId {
                    print("Success with invoiceId \(invoiceId)")
                }
            case .failure(let failError):
                print("Error: \(failError.errorDescription)")
                if let invoiceId = invoiceId {
                    print("Fail: \(failError.statusCode) with invoiceId \(invoiceId)")
                }
            }
        }
MFExecutePaymentRequest* request = [self getExecutePaymentRequest:paymentMethodId];

    MFCardInfo* card = [self getCardInfo];

    [self startLoading];

    [[MFPaymentRequest shared] executeDirectPaymentWithRequest:request cardInfo:card apiLanguage:MFAPILanguageEnglish completion:^(MFDirectPaymentResponse * response, MFFailResponse * error, NSString * invoiceId) {


        if (response !=NULL) {

            if (response.cardInfoResponse != NULL) {

                NSLog(@"%@",[NSString stringWithFormat:@"Status: with card number: %@", response.cardInfoResponse.cardInfo.number]);

            }

            if (invoiceId != NULL) {
                NSLog(@"%@", [NSString stringWithFormat:@"Success with invoice id %@", invoiceId ]);

            }

        }

    }];
MFExecutePaymentRequest request = new MFExecutePaymentRequest(2, 0.100);

// MFCardInfo mfCardInfo = new MFCardInfo("Your token here");
MFCardInfo mfCardInfo = new MFCardInfo("5123450000000008", "09", "21", "100", false);

MFSDK.INSTANCE.executeDirectPayment(
        this,
        request,
        mfCardInfo,
        MFAPILanguage.EN,
        (String invoiceId) -> {
            Log.d(TAG, "invoiceId: " + invoiceId);
            return Unit.INSTANCE;
        },
        (String invoiceId, MFResult<MFDirectPaymentResponse> result) -> {
            if (result instanceof Success) 
                Log.d(TAG, "Response: " + new Gson().toJson(((Success<MFDirectPaymentResponse>) result).getResponse()));
            else if (result instanceof Fail) 
                Log.d(TAG, "Error: " + new Gson().toJson(((Fail) result).getError()));

            return Unit.INSTANCE;
        });
val request = MFExecutePaymentRequest(2, 0.100)

// val mfCardInfo = MFCardInfo("Your token here")
val mfCardInfo = MFCardInfo("5123450000000008", "09", "21", "100", true)

MFSDK.executeDirectPayment(
    this,
    request,
    mfCardInfo,
    MFAPILanguage.EN,
    onInvoiceCreated = {
        Log.d(TAG, "invoiceId: $it")
    }
) { invoiceId: String, result: MFResult<MFDirectPaymentResponse> ->
    when (result) {
        is Success ->
            Log.d(TAG, "Response: " + Gson().toJson(result.response))
        is Fail ->
            Log.d(TAG, "Fail: " + Gson().toJson(result.error))
    }
}

Apple Pay Payment (iOS SDK Only)

To use Apple Pay payment, your account should have the Apple Pay payment method enabled.
For iOS 13.0 and above

let invoiceValue = 5.0
    let paymentMethod = 2 // if you don't Know this you have to call initiatePayment
    let request = MFExecutePaymentRequest(invoiceValue: invoiceValue, paymentMethod: 2)
    MFPaymentRequest.shared.executeApplePayPayment(request: request, apiLanguage: .arabic) { [weak self] response, invoiceId  in
        switch response {
        case .success(let executePaymentResponse):
            if let invoiceStatus = executePaymentResponse.invoiceStatus {
                self?.showSuccess(invoiceStatus)
            }
        case .failure(let failError):
            self?.showFailError(failError)
        }
    }
NSDecimalNumber * decimalNumber = [NSDecimalNumber decimalNumberWithString:self.amountTextField.text];
    NSDecimal invoiceValue = [decimalNumber decimalValue];
    
    MFExecutePaymentRequest* request = [[MFExecutePaymentRequest alloc] initWithInvoiceValue:invoiceValue paymentMethod:paymentMethodId];
    [[MFPaymentRequest shared] executeApplePayPaymentWithRequest:request apiLanguage:MFAPILanguageEnglish completion:^(MFPaymentStatusResponse * response, MFFailResponse * error, NSString * invoiceId) {
        [self stopLoading];
        if (response != NULL) {
            if (response.invoiceStatus != NULL) {
                [self showSuccess:response.invoiceStatus];
            } else {
                [self showFailError:error];
            }
        }  else {
                [self showFailError:error];
        }
    }];

If your app support iOS versions below iOS 13.0, follow these steps:
1- Open your project on Xcode go to your app target, click Info tab, scroll bottom, add URL type, set identifier filed with your bundle id or anything else, and add URL Schemes with saying urlScheme. Make sure it is a unique URL schema for your APP. Finally, Copy the URL for the next steps.
2- Call InitiatePayment endpoint and take Apple Pay 'PaymentMethodId'
3- Create your request and call the 'executeApplePayPayment' method, please note you have to pass the urlScheme argument with URL schemes from the first step.

let invoiceValue = 5.0
    let urlScheme = "urlschema" //urlSchema from step one 
    let paymentMethod = 2 // if you don't Know this you have to call initiatePayment
    let request = MFExecutePaymentRequest(invoiceValue: invoiceValue, paymentMethod: 2)
    MFPaymentRequest.shared.executeApplePayPayment(request: request, urlScheme: urlScheme, apiLanguage: .arabic) { [weak self] response, invoiceId  in
        switch response {
        case .success(let executePaymentResponse):
            if let invoiceStatus = executePaymentResponse.invoiceStatus {
                self?.showSuccess(invoiceStatus)
            }
        case .failure(let failError):
            self?.showFailError(failError)
        }
    }
NSDecimalNumber * decimalNumber = [NSDecimalNumber decimalNumberWithString:self.amountTextField.text];
    NSDecimal invoiceValue = [decimalNumber decimalValue];
    NSString * urlScheme = @"applepay";
    MFExecutePaymentRequest* request = [[MFExecutePaymentRequest alloc] initWithInvoiceValue:invoiceValue paymentMethod:paymentMethodId];
    [[MFPaymentRequest shared] executeApplePayPaymentWithRequest:request urlScheme:urlScheme apiLanguage:MFAPILanguageEnglish completion:^(MFPaymentStatusResponse * response, MFFailResponse * error, NSString * invoiceId) {
        [self stopLoading];
        if (response != NULL) {
            if (response.invoiceStatus != NULL) {
                [self showSuccess:response.invoiceStatus];
            } else {
                [self showFailError:error];
            }
        }  else {
                [self showFailError:error];
        }
    }];

4- Go to your AppDelegate and implement:

application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool

Add the following code inside it:

@UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    .
    .
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if let paymentId = url[MFConstants.paymentId] {
            NotificationCenter.default.post(name: .applePayCheck, object: paymentId)
        }
        return true
    }
    .
    }
@implementation AppDelegate
    . 
    .
        
     - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
            if (url[MFConstants.paymentId] != NULL) {
                [[NSNotificationCenter defaultCenter] postNotificationName:MFConstants.applePayNotificationName object:url[MFConstants.paymentId]];
            }
            return true;
        }
    .
    
    @end

Please note if your app have SceneDelegate so you should implement func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) instead of func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool

@available(iOS 13.0, *)
 func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
     if let url = URLContexts.first?.url {
        if let paymentId = url[MFConstants.paymentId] {
             NotificationCenter.default.post(name: .applePayCheck, object: paymentId)
         }
     }
 }

👍

Apple Pay Test

Use the Apple document that explains how to test Apple pays on your device.


Send Payment

We have explained in the Send Payment section earlier, the different usage cases for it and how it works, here we are going to embed some sample code for calling it through the SDK on the different platforms

let invoiceValue = 5.0
        var notificationOption : MFNotificationOption =  .all
        /*
         notificationOption = .sms
         notificationOption = .email
         notificationOption = .link
         */
        
        let invoice = MFSendPaymentRequest(invoiceValue: invoiceValue, notificationOption: notificationOption, customerName: "customerName")
        
        invoice.customerEmail = "[email protected]"// must be email Required if you choose notificationOption .all or  .email
        invoice.customerMobile = "mobile no"//Required if you choose notificationOption .all or .sms
        invoice.mobileCountryIsoCode = MFMobileCountryCodeISO.kuwait.rawValue
        
        MFPaymentRequest.shared.sendPayment(request: invoice, apiLanguage: .english) { [weak self] (result) in
            switch result {
            case .success(let sendPaymentResponse):
                if let invoiceURL = sendPaymentResponse.invoiceURL {
                    print("result: RedirectUrl is \(invoiceURL)")
                }
            case .failure(let failError):
                print("Error: \(failError)")
            }
        }
double invoiceValue = 5.0;
    MFSendPaymentRequest* request = [[MFSendPaymentRequest alloc] initWithInvoiceValue:invoiceValue notificationOption:MFNotificationOptionAll customerName:@"Test"];
    request.mobileCountryIsoCode = [MFEnumRawValue rawValueWithEnumValue:MFMobileCountryCodeISOKuwait];
    [[MFPaymentRequest shared] sendPaymentWithRequest:request apiLanguage:MFAPILanguageEnglish completion:^(MFSendPaymentResponse * sendPaymentResponse, MFFailResponse * failResponse) {
        
        if(sendPaymentResponse != NULL) {
            NSLog(@"%@",sendPaymentResponse.invoiceURL);
            
        } else {
            NSLog(@"%@",failResponse.errorDescription);
        }
        
    }];
MFSendPaymentRequest request = new MFSendPaymentRequest(0.100, "Customer name", MFNotificationOption.LINK);
MFSDK.INSTANCE.sendPayment(request, MFAPILanguage.EN, (MFResult<MFSendPaymentResponse> result) -> {
        if (result instanceof MFResult.Success)
            Log.d(TAG, "Response: " + new Gson().toJson(((MFResult.Success<MFSendPaymentResponse>) result).getResponse()));
        else if (result instanceof MFResult.Fail)
            Log.d(TAG, "Error: " + new Gson().toJson(((MFResult.Fail) result).getError()));

        return Unit.INSTANCE;
    });
val request = MFSendPaymentRequest(0.100, "Customer name", MFNotificationOption.LINK)
MFSDK.sendPayment(request, MFAPILanguage.EN) { result: MFResult<MFSendPaymentResponse> ->
    when(result){
        is MFResult.Success ->
            Log.d(TAG, "Response: " + Gson().toJson(result.response))
        is MFResult.Fail ->
            Log.d(TAG, "Fail: " + Gson().toJson(result.error))
    }
}

Payment Inquiry

We have explain the main usage for the Payment Inquiry function, that will enable your application to get the full details about a certain invoice / payment. You can use this function within your application on the different platforms as well. Here we are explaining some samples of its usage through the SDK.

let paymentStatusRequest = MFPaymentStatusRequest(Key: "id", KeyType: .invoiceId)
        MFPaymentRequest.shared.getPaymentStatus(paymentStatus: paymentStatusRequest, apiLanguage: .english) { [weak self] (response) in
            self?.stopRequestLoading()
            switch response {
            case .success(let paymentStatusResponse):
                print("\(paymentStatusResponse.invoiceStatus)")
            case .failure(let failError):
                print("\(failError)")
            }
        }
MFPaymentStatusRequest* request = [[MFPaymentStatusRequest alloc]initWithKey:@"1234" KeyType:MFKeyTypeInvoiceId];
    
    [[MFPaymentRequest shared] getPaymentStatusWithPaymentStatus:request apiLanguage:MFAPILanguageEnglish completion:^(MFPaymentStatusResponse * paymentStatusResponse, MFFailResponse * failResponse) {
        if(paymentStatusResponse != NULL) {
            NSLog(@"%@",paymentStatusResponse.invoiceStatus);
        } else {
            NSLog(@"%@",failResponse);
        }
    }];
MFGetPaymentStatusRequest request = new MFGetPaymentStatusRequest("12345", MFKeyType.INVOICE_ID);
MFSDK.INSTANCE.getPaymentStatus(request, "en", new MFCallback<MFGetPaymentStatusResponse>() {
    @Override
    public void onResponse(boolean isSuccess, @Nullable MFGetPaymentStatusResponse response, @Nullable MFError error) {
        if (isSuccess)
            Log.d(TAG, "Response: " + new Gson().toJson(response));
        else
            Log.d(TAG, "Error: " + new Gson().toJson(error));
    }
});
val request = MFGetPaymentStatusRequest("12345", MFKeyType.INVOICE_ID)
MFSDK.getPaymentStatus(request, "en", object: MFCallback<MFGetPaymentStatusResponse>{
    override fun onResponse(isSuccess: Boolean, response: MFGetPaymentStatusResponse?, error: MFError?) {
        if(isSuccess)
            Log.d(TAG, "Response: " + Gson().toJson(response))
        else
            Log.d(TAG, "Error: " + Gson().toJson(error))
    }
})

Android Embedded Payment Usage

Step 1:

Add MFPaymentCardView in your xml layout like the following:

<com.myfatoorah.sdk.views.embeddedpayment.MFPaymentCardView
   android:id="@+id/mfPaymentView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"/>

Note: you could custom a lot of properties of the payment card view like the following:

<com.myfatoorah.sdk.views.embeddedpayment.MFPaymentCardView
   android:id="@+id/mfPaymentView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   app:inputColor="@color/paymentCardViewTextColor"
   app:labelColor="@color/paymentCardViewLabelColor"
   app:errorColor="@color/paymentCardViewValidateTextColor"
   app:cardHolderNameHint="cardHolderNameHint"
   app:cardNumberHint="cardNumberHint"
   app:expiryDateHint="expiryDateHint"
   app:cvvHint="cvvHint"
   app:showLabels="false"
   app:cardHolderNameLabel="cardHolderNameLabel"
   app:cardNumberLabel="cardNumberLabel"
   app:expiryDateLabel="expiryDateLabel"
   app:cvvLabel="cvvLabel"/>

Step 2:

You need to call initiateSession() function to create session. You need to do this for each payment separately. Session is valid for only one payment. and inside it's success state, call load() function and pass it the session response, to load the payment card view on the screen, like the following:

Note: If you want to use saved card option with embedded payment, send the parameter customerIdentifier in the MFInitiateSessionRequest with a unique value for each customer. This value cannot be used for more than one Customer. Check commented lines in the following code.

// val request = MFInitiateSessionRequest(customerIdentifier = "12345")
// MFSDK.initiateSession(request)

MFSDK.initiateSession {
    when (it) {
        is MFResult.Success -> {
            mfPaymentView.load(it.response)
        }
        is MFResult.Fail -> {
            Log.d(TAG, "Fail: " + Gson().toJson(it.error)) 
        }
    }
}
// MFInitiateSessionRequest request = new MFInitiateSessionRequest("12332212");
// MFSDK.INSTANCE.initiateSession(request, (MFResult<MFInitiateSessionResponse> result) -> {

MFSDK.INSTANCE.initiateSession(null, (MFResult<MFInitiateSessionResponse> result) -> {
    if (result instanceof MFResult.Success) {
        mfPaymentView.load(((Success<MFInitiateSessionResponse>) result).getResponse());
    }
    if (result instanceof MFResult.Fail) {
        Log.d(TAG, "Fail: " + new Gson().toJson(((Fail) result).getError()));
    }

    return Unit.INSTANCE;
});

Note: The initiateSession() function should called after MFSDK.init() function (that we mentioned above).

Step 3:

Finally, you need to handle your Pay button to call the pay() function, copy the below code to your pay event handler section:

val request = MFExecutePaymentRequest(0.100)

mfPaymentView.pay(
    this,
    request,
    MFAPILanguage.EN,
    onInvoiceCreated = {
        Log.d(TAG, "invoiceId: $it")
    }
) { invoiceId: String, result: MFResult<MFGetPaymentStatusResponse> ->
    when (result) {
        is MFResult.Success -> {
            Log.d(TAG, "Response: " + Gson().toJson(result.response))
        }
        is MFResult.Fail -> {
            Log.d(TAG, "Fail: " + Gson().toJson(result.error))
        }
    }
    Log.d(TAG, "invoiceId: $invoiceId")
}
MFExecutePaymentRequest request = new MFExecutePaymentRequest(0.100);

        mfPaymentView.pay(
                this,
                request,
                MFAPILanguage.EN,
                (String invoiceId) -> {
                    Log.d(TAG, "invoiceId: " + invoiceId);
                    return Unit.INSTANCE;
                },
                (String invoiceId, MFResult<MFGetPaymentStatusResponse> result) -> {
                    if (result instanceof MFResult.Success) {
                        Log.d(TAG, "Response: " + new Gson().toJson(((Success<MFGetPaymentStatusResponse>) result).getResponse()));
                    }
                    else if (result instanceof MFResult.Fail) {
                        String error = new Gson().toJson(((Fail) result).getError());
                        Log.d(TAG, "Fail: " + error);
                    }

                    Log.d(TAG, "invoiceId:" + invoiceId);

                    return Unit.INSTANCE;
                });

Embedded Payment for iOS

Usage*
1- Create variable from MFCardPaymentView, you can add it:
In storyboard

  • Drag view to view
  • Select the dragged view.
  • In right navigation select 'Identity Inspector'.
  • Set Class MFPaymentCardView and Module MFSDK
11621162

Or by code

let cardPaymentView = MFCardPaymentView()
view.addSubview(cardPaymentView)
cardPaymentView.translatesAutoresizingMaskIntoConstraints = false
cardPaymentView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
cardPaymentView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
cardPaymentView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
cardPaymentView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
cardPaymentView.heightAnchor.constraint(equalToConstant: 220).isActive = true
MFPaymentCardView * paymentCardView = [[MFPaymentCardView alloc] init];
[self.view addSubview:paymentCardView];
[[paymentCardView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor] setActive:YES];
[[paymentCardView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor] setActive:YES];
[[paymentCardView.widthAnchor constraintEqualToAnchor:self.view.widthAnchor] setActive:YES];
[[paymentCardView.heightAnchor constraintEqualToConstant:220] setActive:YES];

2- You can configure card with labels, placeholders, colors, height, border radius also by code or storyboard

// get default configure
    var configure = MFCardConfigureBuilder.default
    // but you can create yours
    // set placeholders
    configure.setPlaceholder(MFCardPlaceholder(cardHolderNamePlaceholder: "Name", cardNumberPlaceholder: "Number", expiryDatePlaceholder: "MM / YY", cvvPlaceholder: "CVV"))
//        //set labels
    configure.setLabel(MFCardLabel(cardHolderNameLabel: "Card holder name", cardNumberLabel: "Card number", expiryDateLabel: "MM / YY", cvvLabel: "CVV", showLabels: true))
//        // set theme
    configure.setTheme(MFCardTheme(inputColor: .black, labelColor: .black, errorColor: .red))
    
    
    // set labels and texts font size
    configure.setFontSize(15)
    
    // set border width
    configure.setBorderWidth(1)
    
    // set border radius for input fields
    configure.setBorderRadius(8)
    
     // create the new configure and assigned to payment card view
    paymentCardView.configure = configure.build()
// get default configure
    MFCardConfigureBuilder * configure = [MFCardConfigureBuilder default];
    // but you can create yours
    // set placeholders
    MFCardPlaceholder * placeholder = [[MFCardPlaceholder alloc] initWithCardHolderNamePlaceholder:@"Name on card" cardNumberPlaceholder:@"Card number" expiryDatePlaceholder:@"MM / YY" cvvPlaceholder:@"CVV"];
    [configure setPlaceholder:placeholder];

    // set labels
    MFCardLabel * label = [[MFCardLabel alloc] initWithCardHolderNameLabel:@"Card holder name" cardNumberLabel:@"Card Number" expiryDateLabel:@"Expiry Date" cvvLabel:@"CVV" showLabels:YES];
    [configure setLabel:label];

    // set theme
    MFCardTheme * theme = [[MFCardTheme alloc] initWithInputColor:UIColor.blackColor labelColor:UIColor.blackColor errorColor:UIColor.redColor];
    [configure setTheme:theme];


    // set labels and texts font size
    [configure setFontSize:14];

    // set border width
    [configure setBorderWidth:1];

    // set border radius
    [configure setBorderRadius:8];

    // create the new configure and assigned to payment card view
    paymentCardView.configure = [configure build];

3- Initiate session and get session id to setup MFCardPaymentView

MFPaymentRequest.shared.initiateSession(apiLanguage: .english) { response in
    switch response {
    case .success(let session):
        cardPaymentView.load(initiateSession: session)
    case .failure(let error):
        print(error)
    }
}
[[MFPaymentRequest shared] initiateSessionWithApiLanguage:MFAPILanguageEnglish completion:^(MFInitiateSessionResponse * response, MFFailResponse * error) {
            if (error == NULL) {
                [paymentCardView loadWithInitiateSession:response];
            } else {
                NSLog(error);
            }
    }];

4- Now your MFPaymentCardView was setup, so what you need to enter you card info and call pay

// create request, make sure you don't set paymentMethodId, because it override sessionId and this is wrong.
let request = MFExecutePaymentRequest(invoiceValue: 5)

// call pay method to make your order
cardPaymentView.pay(request, .english) { response, invoiceId in
    switch response {
    case .success(let paymentStatus):
        print(paymentStatus)
    case .failure(let error):
        print(error)
    }
}
// create request, make sure you don't set paymentMethodId, because it override sessionId and this is wrong.
    NSDecimalNumber * decimalNumber = [NSDecimalNumber decimalNumberWithString:self.amountTextField.text];
    NSDecimal invoiceValue = [decimalNumber decimalValue];
    MFExecutePaymentRequest * request = [[MFExecutePaymentRequest alloc] initWithInvoiceValue:invoiceValue];
    [_paymentCardView pay:request :MFAPILanguageEnglish completion:^(MFPaymentStatusResponse * response, MFFailResponse * error, NSString * invoiceId) {
        if (response != NULL) {
            if (response.invoiceStatus != NULL) {
                [self showSuccess:response.invoiceStatus];
            } else {
                [self showFailError:error];
            }
        }else {
            [self showFailError:error];
        }
    }];

Embedded Payment for iOS

1- Create variable from MFApplePayButton, you can create it from code or storyboard as you like.

let applePayButton = MFApplePayButton()
view.addSubview(applePayButton)
applePayButton.translatesAutoresizingMaskIntoConstraints = false
applePayButton.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
applePayButton.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
applePayButton.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
applePayButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
applePayButton.heightAnchor.constraint(equalToConstant: 80).isActive = true

2- Initiate session and get session id to handle apple pay

let invoiceValue = Decimal(string: amountTextField.text ?? "0") ?? 0
        let request = MFExecutePaymentRequest(invoiceValue: invoiceValue, displayCurrencyIso: .kuwait_KWD)
    MFPaymentRequest.shared.initiateSession(apiLanguage: .english) { [weak self] response in
     switch response {
      case .success(let session):
        self?.applePayButton.load(session, (request, .english, completion: { response, invoiceId in
            self?.activityIndicator.stopAnimating()
            switch response {
             case .success(let executePaymentResponse):
              if let invoiceStatus = executePaymentResponse.invoiceStatus {
                self?.showSuccess(invoiceStatus)
              }
             case .failure(let error):
                    self?.showFailError(error)
                    }
                })
      case .failure(let error):
                print("#initiate session", error.localizedDescription)
        }
}

Did this page help you?