Giter Club home page Giter Club logo

kotlin_client's Introduction

Paylike's API Client in kotlin

This implementation is based on Paylike/js-client

High-level client for the API documented at: Paylike API reference. It is using paylike_kotlin_request under the hood.

Examples

Gradle setup

repositories {
    // ...
    maven {
        url 'https://jitpack.io'
    }
    // ...
}

dependencies {
    // ...
    implementation 'com.github.paylike:kotlin_client:[version]' // see the badge under the title
    // ...
}

Example code for payment flow

import com.github.paylike.kotlin_client.BuildConfig
import com.github.paylike.kotlin_client.PaylikeClient
import com.github.paylike.kotlin_client.domain.dto.payment.request.PaymentData
import com.github.paylike.kotlin_client.domain.dto.payment.request.card.ExpiryDto
import com.github.paylike.kotlin_client.domain.dto.payment.request.card.PaylikeCardDto
import com.github.paylike.kotlin_client.domain.dto.payment.request.integration.PaymentIntegrationDto
import com.github.paylike.kotlin_client.domain.dto.tokenize.request.TokenizeData
import com.github.paylike.kotlin_client.domain.dto.tokenize.request.TokenizeTypes
import com.github.paylike.kotlin_money.PaymentAmount
import kotlinx.coroutines.runBlocking

/**
 * This a dummy example usage flow, this code wont finish successfully.
 */
fun main() {
    // create client instance
    val client = PaylikeClient("TestID01")
    // get your merchant id, preferably from environmental variable
    // in your local.properties file set: PaylikeMerchantApiKey=<yourMerchantId>
    val yourMerchantId = BuildConfig.PaylikeMerchantApiKey

    // indicate coroutineScope
    runBlocking {
        // Tokenize the card data
        val responseCard = client.tokenize(TokenizeData(TokenizeTypes.PCN, "4100000000000000"))
        val responseCode = client.tokenize(TokenizeData(TokenizeTypes.PCSC, "111"))

        // create the payment dto
        val paymentAmount = PaymentAmount("EUR", 1, 0)
        val paymentCard = PaylikeCardDto(
            number = responseCard,
            cvc = responseCode,
            expiry = ExpiryDto(12,2023),
        )
        val paymentData = PaymentData(
            integration = PaymentIntegrationDto(yourMerchantId), // here comes your merchant id
            amount = paymentAmount,
            card = paymentCard,
        )

        // Payment flow:
        // Send the payment request
        val tdsResponse = client.paymentCreate(paymentData)
        // If the response is HTML, then you encountered a payment challenge
        // like TDS
        if (tdsResponse.isHTML) {
            val htmlBody = tdsResponse.htmlBody
            val hints = tdsResponse.paymentResponse.hints!!
            // handle tds
            // ...
            // get the hint form the tds
            val tdsHint = String() // dummy hint from tds
            // add it to the list
            val freshSet = mutableSetOf<String>()
            freshSet.addAll(hints)
            freshSet.add(tdsHint)
            paymentData.hints = freshSet.toList()
            // after auth we start continue the payment process
            val paymentResponse = client.paymentCreate(paymentData)
            // and finally we get the transactionId at the end of the flow
            val transactionId = paymentResponse.paymentResponse.transactionId
        }
    }
}

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.