GopayCheckoutKit 1.2.0

GopayCheckoutKit 1.2.0

Maintained by muhammad fauzi masykur.



  • By
  • veritrans

Gopay Checkout iOS SDK

iOS library to enable GoPay checkout flow via Midtrans API.

SDK Installation

Manual Installation

  1. Download this repo
  2. Drag the GopayCheckoutKit.xcframework to your poroject on xcode
  3. On popup, choose copy items if needed and create groups then click finish
  4. build and run

cocoapods installation

If you're using cocoapods, add this to your .podfile

target 'MyApp' do
  pod 'GopayCheckoutKit'
end

then do pod install

SDK Integration

For Swift, you need to import GopayCheckoutKit in your AppDelegate.Swift File and every class where you plan to use the sdk method.

For Objective-C, you need to import GopayCheckoutKit.h in your AppDelegate.h and every class where you plan to use the sdk method.

You also need to setup your app url scheme, this is used to let the SDK go back to close the webview automatically and navigate to previous screen. For example, your app url scheme should be something like myapp://, all lowercased. To setup your app url scheme , There're two ways to do this, You can choose either:

  1. Go to Project Settings -> Info, and add inside The URL Types section a new URL scheme. Add something of the sort of myapp or any name you preferred, lowercased.

  2. Alternatively, you can also go to your info.plist file and paste this code, you can rename the myapp value with your preferred name.

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>myapp</string>
            </array>
        </dict>
    </array>

SDK Initialization

SDK initialization can be done anywhere when you want to start to use the sdk's method. In this example, we'll use AppDelegate to setup the sdk within the application:didfinishlaunchingwithoptions:.

Eversince ios 13, Apple introduces SceneDelegate, so if your app target includes ios 13 and above, you'll also need to add the handleCallbackUrl on the SceneDelegate file within the openURLContexts: method.

Important notes

For the callbackUrl parameter, please use your app url scheme with this format myapp://home, by adding home to your app url scheme, as shown in the code below

Obj-C

AppDelegate file

- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
...

[GPYClient  initWithMerchantServerURL:MERCHANT_SERVER_URL  merchantId:MERCHANT_ID callbackUrl:@"myapp://home" isLoggingEnabled:NO];

...
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    
    [GPYClient handleCallbackUrl:url];
    return YES;
}

SceneDelegate file

- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts{
    NSURL *url = URLContexts.allObjects.firstObject.URL;
    [GPYClient handleCallbackUrl:url];
}
Swift

AppDelegate File

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
	GPYClient.initWithMerchantServerURL(MERCHANT_SERVER_URL, merchantId: MERCHANT_ID, callbackUrl: "myapp://home", isLoggingEnabled: false)

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

	GPYClient.handleCallbackUrl(url)
	
    return true
}

SceneDelegate File

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
	if let url = URLContexts.first?.url {
  	 GPYClient.handleCallbackUrl(url)
	}
}

Link Account

Obj-C

GPYPartnerDetails *details = [[GPYPartnerDetails alloc]initWithPhoneNumber:@"8212345678" countryCode:@"62"];

[GPYClient linkAccountWithPaymentType:@"gopay" gopayPartnerDetails:details viewController:self completion:^(GPYLinkAccountResult * _Nullable result, NSError * _Nullable error) {
    if (result) {
        // do something
    } else{
        NSLog(@"error %@", error);
    }
}]; 

Swift

let partnerDetails = GPYPartnerDetails(phoneNumber: "812345678", countryCode: "62")
GPYClient.linkAccount(withPaymentType: "gopay", gopayPartnerDetails: partnerDetails, viewController: self) { (result, error) in
    if (result != nil){
        // do something
    } else {
        // error
    }
}

Enquire Account And Payment Info

Obj-C

[GPYClient enquireAccountWithAccountID:ACCOUNT_ID completion:^(GPYAccountInfo * _Nullable result, NSError * _Nullable error) {
        if (result) {
            // do something
        } else{
            NSLog(@"error %@", error);
        }
    }];

Swift

GPYClient.enquireAccount(ACCOUNT_ID) { (result, error) in
 		if (result != nil) {
 		//do something
       	} else {
       	// error
        }
}

Disable / Unlink Account

Obj-C

[GPYClient disableAccountWithAccountID:ACCOUNT_ID completion:^(GPYDisableAccountResult * _Nullable result, NSError * _Nullable error) {
        if (result) {
         // do something
        } else {
            NSLog(@"error %@", error);
        }
    }];

Swift

GPYClient.disableAccount(withAccountID: ACCOUNT_ID) { (result, error) in
	if (result != nil){
     // do something
    } else {
     // error
    }
}

Create Transaction

Obj-C

GPYGopayDetails *gopayDetails = [[GPYGopayDetails alloc]initWithAccountID:ACCOUNT_ID paymentOptionToken:PAYMENT_OPTION_TOKEN];


GPYTransactionDetails *transDetails = [[GPYTransactionDetails alloc]initWithGrossAmount:@100 orderID:ORDER_ID currency:@"IDR"];

GPYCustomerDetails *customerDetails = [[GPYCustomerDetails alloc]initWithPhoneNumber:@"08123456789" firstName:@"fauzi" lastName:nil email:nil];

GPYItemDetails *itemDetails = [[GPYItemDetails alloc]initWithIthItemID:@"itemId1" name:@"one piece t-shirt" price:@100 quantity:@1 category:@"clothing"];

[GPYClient createTransactionWithPaymentType:@"gopay" viewController:self gopayDetails:gopayDetails transactionDetails:transDetails customerDetails:customerDetails itemDetails:@[itemDetails] completion:^(GPYTransactionResult * _Nullable result, NSError * _Nullable error) {
    
    if (result) {
        // do something
        // Transaction status will be set to "Pending", To actually know the latest transaction status, you should hit get transaction API
    } else {
        NSLog(@"error transaksi %@", error);
    }
}];

Swift

let gopayDetais = GPYGopayDetails(accountID: ACCOUNT_ID ?? "", paymentOptionToken: PAYMENT_OPTIONS_TOKEN ?? "")
let transDetails = GPYTransactionDetails(grossAmount: 200, orderID: "abc111", currency: "IDR")
let cusDetails = GPYCustomerDetails(phoneNumber: "088888888", firstName: "fauzi", lastName: "cihuy", email: "[email protected]")
let itemDetails1 = GPYItemDetails(ithItemID: "aabb1", name: "one piece t-shirt", price: 100, quantity: 1, category: "shirts")
let itemDetails2 = GPYItemDetails(ithItemID: "aabb2", name: "one piece pants", price: 100, quantity: 1, category: "pants")

GPYClient.createTransaction(withPaymentType: "gopay", viewController: self, gopayDetails: gopayDetais, transactionDetails: transDetails, customerDetails: cusDetails, itemDetails: [itemDetails1, itemDetails2]) { (result, error) in
    if (result != nil) {
     	// do something
    } else {
    	// error
    }
}

Gopay Checkout Response Processor

This response processor is used to help process and handle the webview if you hit the Partner API directly.

Link Account

Obj-C

[GPYCheckoutProcessor processAccountLinkingWithResponse:linkingResponse viewController:viewController completion:^(GPYLinkAccountResult * _Nullable result, NSError * _Nullable error) {
	if (result) {
		//do something
    } else{
    	// error
    }
}];

Swift

GPYCheckoutProcessor.processAccountLinking(with: RESPONSE, viewController: self) { (result, error) in
    if (result != nil){
        // do something
    } else {
        // error
    }
}

Create Transaction

Obj-C

[GPYCheckoutProcessor processCreateTransactionWithResponse:transactionResponse viewController:viewController completion:^(GPYTransactionResult * _Nullable result, NSError * _Nullable error) {
	if (result) {
       // do something
    } else {
       // error 
    }
}];

Swift

GPYCheckoutProcessor.processCreateTransaction(with: TRANSACTION_RESPONSE, viewController: self) { (result, error) in
    if (result != nil){
        // do something
    } else {
        // error
    }
}