Card Refund (NOT Present)

Overview

A Refund (Card Not Present) transaction is a vital method for reversing payments in various remote or online settings. This process involves returning funds to the customer's credit or debit card for a transaction that was completed without the physical presence of the card. It is essential for scenarios where a customer requests a return or refund for a purchase made online, over the phone, or via mail order. This type of transaction is a staple in environments such as e-commerce, subscription services, and remote sales, where prompt and accurate handling of refunds is crucial. By using Refund (Card Not Present) transactions, merchants can ensure that customers are credited back promptly and accurately, maintaining customer satisfaction and trust in the merchant's services.


Create a Card Refund (NOT Present) Transaction Request

// Import necessary classes
import com.pxpfinancial.sdk.pos.TransactionRequest
import com.pxpfinancial.sdk.pos.PointOfInteraction
import com.pxpfinancial.sdk.pos.Amounts
import com.pxpfinancial.sdk.pos.TransactionMethod
import java.text.SimpleDateFormat
import java.util.Date

// Define merchant and transaction details
val merchant = "MERCHANT-1"
val site = "SITE-1"
val merchantOrderId = "This_is_my_merchant_order_id"
val merchantTransactionId = "This_is_my_merchant_transaction_id"
val merchantTransactionDate = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX").format(Date())
val systemTransactionId = "This_is_my_system_transaction_id"

// Transaction amount details
val amounts = Amounts(
    currencyCode = "GBP",
    transaction = 20.00
)

// Function to build transaction request
fun buildTransactionRequest(): TransactionRequest {
    return TransactionRequest(      	
      	systemTransactionId = systemTransactionId,  
      	amounts = amounts,
      	merchant = merchant,
      	merchantOrderId = merchantOrderId,
      	merchantTransactionId = merchantTransactionId,
      	merchantTransactionDate = merchantTransactionDate,      	
      	operation = "Refund",
        site = site
    )
}

Handle a Card Refund (NOT Present) Transaction Response

// Import necessary classes
import com.pxpfinancial.sdk.pos.TransactionResponse
import kotlinx.coroutines.*
import java.text.SimpleDateFormat
import java.util.Date

// Function to handle transaction response
private fun handleTransactionResponse(response: TransactionResponse?) {
    response ?: run {
        println("Transaction timed out or no response received.")
        return
    }

    // Accessing fields in the base response
    val state = response.state
    val approvalCode = response.approvalCode
    val merchantOrderId = response.merchantOrderId
    val merchantTransactionId = response.merchantTransactionId
    val systemTransactionId = response.systemTransactionId
    val providerTransactionId = response.providerTransactionId
    
    // Accessing fields in card response
    val cardScheme = response.card.cardScheme
  	val cardScheme = response.card.gatewayTokenId  
  	
    // Accessing fields in provider response
    val provider = response.provider.provider
    val providerCode = response.provider.code
    val providerMessage = response.provider.message
  	
  	// Accessing fields for receipt data
    val merchantId = response.receiptData.merchantId
    val maskedPrimaryAccountNumber = response.receiptData.maskedPrimaryAccountNumber
  	val expiryDateMonth = response.receiptData.expiryDateMonth
  	val expiryDateYear = response.receiptData.expiryDateYear
  
    // Additional business logic based on the transaction state
    when (state) {
        "Authorised" -> {
            // Handle authorised transaction
            println("Transaction authorised successfully.")
            // Perform further actions like updating UI, saving transaction details, etc.
        }
        "Declined" -> {
            // Handle declined transaction
            println("Transaction declined.")
            // Perform actions based on the declined state
        }
        else -> {
            // Handle other transaction states if necessary
            println("Transaction state: $state")
        }
    }
}

// Simulated usage of request and response handling
fun main() {
    // Build transaction request
    val transactionRequest = buildTransactionRequest()

    // Simulate async processing and response handling
    CoroutineScope(Dispatchers.Default).launch {
        val pxpPosSdk = com.pxpfinancial.sdk.pos.Transaction("http://127.0.0.1:8080", "YOUR_API_KEY")

        // Process the transaction
        val result = withTimeoutOrNull(10000L) { // 10 seconds timeout
            suspendCancellableCoroutine<TransactionResponse?> { continuation ->
                pxpPosSdk.processTransaction(transactionRequest) { response ->
                    continuation.resume(response, onCancellation = null)
                }
            }
        }

        // Handle the transaction response
        handleTransactionResponse(result)
    }
}