Android Embed

Integrate PortOne Payment Gateway with your Android app using the Android Embed SDK, enabling secure and efficient payment processing. This guide will walk you through the steps needed to integrate the SDK and prepare your app for checkout.

The PortOne Android SDK simplifies the integration of the PortOne Payment Gateway into your Android app, offering a secure, reliable, and efficient way to accept payments. With this SDK, you can seamlessly embed your app to a variety of payment channels, providing a smooth payment experience for users


Sample App

Check the sample app to integrate on GitHub


Prerequisites

  • Create an account on PortOne:
    Before proceeding with the integration, ensure you have created an account on PortOne to access their services and functionalities.
  • Enable Payment Channels and Methods:
    Customize and enable the specific payment channels and methods that align with your business requirements and preferences.
  • Android application for the integration:
    You will need an existing Android application in which you intend to integrate the PortOne Android SDK for payment processing capabilities.
  • authKey to access the SDK:
    Obtain an authorization key (authKey) from the PortOne Team, as it is required to securely access and utilize the features of the PortOne SDK in your Android application. authKey will be issued by the PortOne Team by sending us email on this ** [email protected]

Integration

Steps to integrate your Android application with PortOne Android SDK.

  1. Install PortOne Android SDK and Authorise it.
  2. Set the Intent Filters in the Manifests
  3. Set Intent Receivers for Payment Status
  4. Setup to Obtain JWT Token from the Server
  5. Generate a Signature Hash for Payload

1. Install PortOne Android SDK and Authorise it.

To begin, add the PortOne Android SDK to your project by adding the dependency to your build.gradle file.

  • In the build.gradle (:app) file, add:
implementation 'com.github.iamport-intl:portone-android-native-sdk:V3.0.48'
  • Next, add the authKey to your gradle.properties file:.
authKey= XXXXXXXXXXXXXXXXXXXXXX
  • Authorize the SDK by adding the provided authKey to your gradle.properties file, which you can request by emailing the PortOne team at [email protected].

Then, configure your build.gradle (:Project) or settings.gradle to reference the key:

  1. Build.gradle (:Project) Setup:

    repositories {
            maven { url '<https://maven.google.com/>' }
            maven{
                url '<https://jitpack.io>'
                credentials { username authKey }
            }
        }
    
  2. Settings.gradle Setup:

    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            maven {
                url 'https://maven.google.com/'
            }
            maven {
                url 'https://jitpack.io'
                credentials { username authKey } // Add your generated authKey here
            }
        }
    }
    

Include the necessary code in your build.gradle () file, specifying the project dependency and Kotlin version.

buildscript {
    ext.kotlin_version = "1.5.10" // Specify the Kotlin version here
    
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.2" // Add the Android Gradle plugin version
        
        // Add any other dependencies needed for your project setup
    }
}

2. Set the Intent Filters in the Manifests

  • Next, you need to add intent filters in your AndroidManifest.xml to handle payment status updates and navigate users back to your app after payment.

    <activity android:name=".CheckoutActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:scheme="portone"
                android:host="checkout" />
        </intent-filter>
    </activity>
    
    

This setup ensures your app can handle the redirection URL after payment completion. For detailed instructions, refer to the deep linking guide here.

3. Set Intent Receivers for Payment Status:

You’ll need to handle the payment status response using the onActivityResult method. Here’s an example:.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (resultCode == RESULT_CODE && data != null) {
            when (requestCode) {
                PAYOUT_REQUEST_CODE, PAYMENT_STATUS_REQUEST_CODE -> {
                    val paymentStatus: CheckoutConnectDto.UpdatedPaymentStatusResponse? =
                        (data.getSerializableExtra(PAYMENT_STATUS)
                            ?: "Empty") as CheckoutConnectDto.UpdatedPaymentStatusResponse
                }

            }

        }
    }

This method receives the payment status and updates the user interface with the payment result.

4. Have a setup to get JWT token from the server

The PortOne SDK requires a JWT token for authentication. You need to implement a server-side process to generate this token and retrieve it in your Android app.

Steps:

  1. Implement server logic to generate a JWT token.
  2. In your Android app, fetch the token to process the checkout.

The process for generating a JWT token can be found in detail here

5. Generate Signture Hash

To generate a signature hash, create it on the server side using a secret key that will be included in the payload. This ensures secure processing of transactions.

Note: Generating a signature hash is optional if you have whitelisted your app while initializing the checkout. Detailed instructions on this are provided in the checkout setup section.

Steps:

  1. Implement server logic to generate a signature hash.
  2. In your Android app, fetch the signature hash to process the checkout.

The process for generating a Signature Hash can be found in detail here


Android Embed

PortOne's Checkout offers a streamlined integration experience, simplifying the process for merchants. This variant involves calling a single method with the essential payload, which results in the PortOne SDK opening a webpage seamlessly. By handling the user interface within the SDK, merchants can focus on the payment flow without concerns about UI intricacies.


Initialization

To begin using the PortOne SDK, initialize the PortOne SDK instance in your activity. Set the environment to either "sandbox" for testing or "live" for production.

portOne = PortOneImpl(
            context = this,
            environment = RequestBodies.environment,
            applicationId = BuildConfig.APPLICATION_ID)

Parameters:

  • context: The activity context where the SDK is being initialized.
  • environment: Defines the environment in which the SDK operates.
    • "sandbox" for testing
    • "live" for production
  • applicationId: (Optional) The unique ID of your application.

Note: The applicationId parameter is required if the merchant wants to whitelist the app. When an app is whitelisted, the signature hash generation becomes optional, as the app is pre-authorized to securely process payments without it.


Update Payment Status

As part of the setup for app-to-app flow, a deep link is configured to handle the transaction response. When the payment process completes, the app receives an intent with payment status data. It’s essential to check if this intent is available, and if so, pass its content to the SDK to update the transaction status.

if (null != intent && null != intent.data) {
    val paymentStatus = intent.data.toString()
    portOne.updatePaymentStatus(paymentStatus)
}

Note:This step is crucial for completing the transaction in the app-to-app flow, as it synchronizes the payment status with the server.


Implementation

This is the method that has been utilised to process the web checkout.

val checkoutDetails = CheckoutEmbedDto.CheckoutUsingEmbedRequest()
portOne.checkoutUsingEmbed(
            token = token,
            clientKey = portoneKey,
            request = getEmbedCheckoutRequest()
        )
ParametersData Type
tokenStringmandatory
portoneKeyStringmandatory
requestCheckoutEmbedDto.CheckoutUsingEmbedRequestmandatory

Parameter list for CheckoutEmbedDto.CheckoutUsingEmbedRequest

portone_key
string · required

The unique PortOne key for the merchant.


merchant_details
object
The JSON object for merchant details
name
string · The name of the merchant
logo
string · The logo of the merchant
back_url
string · The URL of the merchant site
promo_code
string · The promo code enabled on the order by the merchant
promo_discount
int · The promo code discount amount on the order by the merchant
shipping_charges
double · The shipping charges set by the merchant

merchant_order_id
string · required

The unique merchant order reference generated by the merchant.


signature_hash
string · required

The signature hash of transaction details.


amount
double · required

The amount of the transaction.


currency
string · required

The currency of the transaction.


country_code
string · required

The country code of the transaction.


billing_details
object
The JSON object for billing details
billing_name
string · The billing first and middle name
billing_surname
string · The billing last name
billing_email
string · The billing email address
billing_phone
string · The billing phone number
billing_address
object
The JSON object containing full address
city
string · City name
country_code
string · 2-digit country code
country_name
string · Full country name
locale
string · Region locale
line_1
string · Line 1 of the address
line_2
string · Line 2 of the address
postal_code
string · Postal code of the area
state
string · State/province of the country

shipping_details
object
The JSON object for shipping details
shipping_name
string · The shipping first and middle name
shipping_surname
string · The shipping last name
shipping_email
string · The shipping email address
shipping_phone
string · The shipping phone number
shipping_address
object
The JSON object containing full address
city
string · City name
country_code
string · 2-digit country code
country_name
string · Full country name
locale
string · Region locale
line_1
string · Line 1 of the address
line_2
string · Line 2 of the address
postal_code
string · Postal code of the area
state
string · State/province of the country

order_details
array of objects
The JSON array for order details
id
string · The unique identifier of the order item
price
double · The price of the product
name
string · The name of the product
quantity
integer · The quantity of the product
image
string · The URL of the product image

success_url
string · required

The URL of the success page hosted by the merchant.


failure_url
string · required

The URL of the failure page hosted by the merchant.


expiry_hours
int · required

The expiry time in hours for the checkout session.


redirect_url
string · required

The URL for redirection after the transaction.


environment
string · required

The environment for the transaction, either sandbox or live.


Response:

Receiving the payment status within the override method onActivityResult is a common practice in integration to handle payment callbacks. By implementing this method during the integration process, developers can capture and process the payment status information returned by the payment gateway or SDK. This allows for real-time feedback on the payment's success or failure, enabling further actions to be taken based on the outcome of the transaction.


Possible Error Scenarios:

INVALID_UNAUTHORIZED_JWT_TOKEN_ERROR

  1. Ensure that the PortOne Key and Secret Key belong to the same account.
  2. Confirm that the Secret Key has not been altered.
  3. Verify that the Bearer keyword precedes the generated token with a space. Example: Bearer $jwtToken.
  4. Check if the token expiration time is after the current time.

INVALID_UNAUTHORIZED_TRANSACTION_SIGNATURE_ERROR

  1. Validate if all parameters align with the payload/request.
  2. Ensure that the PortOne key matches with the payload and the account.

INVALID_UNAUTHORIZED_TRANSACTION_IAMPORTKEY_ERROR

  1. Confirm that the PortOne key matches with the payload and the account.

INVALID_PAYMENT_CHANNEL

  1. Validate that the payment channels and methods included in the payload are enabled in the PortOne portal.

INVALID_ENVIRONMENT

  1. Verify that an environment (sandbox or live) has been specified.

Summation of order value, tax, duties, shipping, and discount should equal the total amount

  1. If items are provided, ensure that the values match the total amount calculation formula: sum(items price * items quantity) + shipping charge - discount = amount.
  2. Mandatory parameters in the payload:
    • price
    • promo_discount (0 accepted)
    • shipping_charges (0 accepted)