Swift
Swift
Global Payments Card Authorization
See more Global Payments Examples
Demonstrates how to send a card payments authorization request.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// if you don't have a Client ID yet you can still quickly test some basic request types using the following URL and credentials:
// Test URL - https://test.realexpayments.com/epage-remote.cgi
// Client ID: realexsandbox
// Shared Secret: Po8lRRT67a
var testUrl: String? = "https://test.realexpayments.com/epage-remote.cgi"
var clientID: String? = "realexsandbox"
var sharedSecret: String? = "Po8lRRT67a"
var amount: String? = "1001"
var currency: String? = "EUR"
var cardNumber: String? = "4263970000005262"
// We'll be sending the following XML in the body of the request:
// <?xml version="1.0" encoding="UTF-8"?>
// <request type="auth" timestamp="20180613141207">
// <merchantid>MerchantId</merchantid>
// <account>internet</account>
// <channel>ECOM</channel>
// <orderid>N6qsk4kYRZihmPrTXWYS6g</orderid>
// <amount currency="EUR">1001</amount>
// <card>
// <number>4263970000005262</number>
// <expdate>0425</expdate>
// <chname>James Mason</chname>
// <type>VISA</type>
// <cvn>
// <number>123</number>
// <presind>1</presind>
// </cvn>
// </card>
// <autosettle flag="1"/>
// <sha1hash>87707637a34ba651b6185718c863abc64b673f20</sha1hash>
// </request>
// Use this online tool to generate code from sample XML:
// Generate Code to Create XML
// Get the current date/time in this format: 20180613141207
let dt = CkoDateTime()!
dt.setFromCurrentSystemTime()
var dtStr: String? = dt.get(asIso8601: "YYYYMMDDhhmmss", bLocal: true)
// Generate a unique order ID
let prng = CkoPrng()!
var orderId: String? = prng.genRandom(numBytes: 32, encoding: "base64url")
// Compute the sha1hash
let crypt = CkoCrypt2()!
crypt.hashAlgorithm = "sha1"
crypt.encodingMode = "hexlower"
let sbA = CkoStringBuilder()!
sbA.append(value: "timestamp.merchantid.orderid.amount.currency.cardnumber")
var numReplaced: Int = sbA.replace(value: "timestamp", replacement: dtStr).intValue
numReplaced = sbA.replace(value: "merchantid", replacement: clientID).intValue
numReplaced = sbA.replace(value: "orderid", replacement: orderId).intValue
numReplaced = sbA.replace(value: "amount", replacement: amount).intValue
numReplaced = sbA.replace(value: "currency", replacement: currency).intValue
numReplaced = sbA.replace(value: "cardnumber", replacement: cardNumber).intValue
var hashA: String? = crypt.hashStringENC(str: sbA.getAsString())
let sbB = CkoStringBuilder()!
sbB.append(value: hashA)
sbB.append(value: ".")
sbB.append(value: sharedSecret)
var hashB: String? = crypt.hashStringENC(str: sbB.getAsString())
let xml = CkoXml()!
xml.tag = "request"
xml.addAttribute(name: "type", value: "auth")
xml.addAttribute(name: "timestamp", value: dtStr)
xml.updateChildContent(tagPath: "merchantid", value: clientID)
xml.updateChildContent(tagPath: "account", value: "internet")
xml.updateChildContent(tagPath: "channel", value: "ECOM")
xml.updateChildContent(tagPath: "orderid", value: orderId)
xml.updateAttrAt(tagPath: "amount", autoCreate: true, attrName: "currency", attrValue: currency)
xml.updateChildContent(tagPath: "amount", value: amount)
xml.updateChildContent(tagPath: "card|number", value: cardNumber)
xml.updateChildContent(tagPath: "card|expdate", value: "0425")
xml.updateChildContent(tagPath: "card|chname", value: "James Mason")
xml.updateChildContent(tagPath: "card|type", value: "VISA")
xml.updateChildContent(tagPath: "card|cvn|number", value: "123")
xml.updateChildContent(tagPath: "card|cvn|presind", value: "1")
xml.updateAttrAt(tagPath: "autosettle", autoCreate: true, attrName: "flag", attrValue: "1")
xml.updateChildContent(tagPath: "sha1hash", value: hashB)
let resp = CkoHttpResponse()!
success = http.httpStr(verb: "POST", url: testUrl, bodyStr: xml.getXml(), charset: "utf-8", contentType: "application/xml", response: resp)
if success == false {
print("\(http.lastErrorText!)")
return
}
print("Response Status Code: \(resp.statusCode.intValue)")
print("Response Body:")
print("\(resp.bodyStr!)")
if resp.statusCode.intValue != 200 {
print("Failed.")
return
}
// A status code of 200 indicates we received an XML response, but it could be an error message.
// Here's an example of an error response:
// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
// <response timestamp="20200418142356">
// <orderid>N6qsk4kYRZihmPrTXWYS6g</orderid>
// <result>508</result>
// <message>Invalid timestamp: '{' Value exceeds allowable limit: '}'</message>
// </response>
// We need to check the "result" within the XML.
xml.load(xmlData: resp.bodyStr)
var result: Int = xml.getChildIntValue(tagPath: "result").intValue
print("result = \(result)")
// A successful result looks like this:
// <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
// <response timestamp="20200418145519">
// <merchantid>realexsandbox</merchantid>
// <account>internet</account>
// <orderid>Cw93I1nFWVZuaATh46qMUCBlCcfrOvLo65C2cq5X-AY</orderid>
// <result>00</result>
// <authcode>L3pHww</authcode>
// <message>AUTH CODE: L3pHww</message>
// <pasref>96838535689610806</pasref>
// <cvnresult>M</cvnresult>
// <timetaken>87</timetaken>
// <authtimetaken>67</authtimetaken>
// <batchid>6322506</batchid>
// <sha1hash>2fd2f15f97f1775779fe9bda536dc8317a4b39f6</sha1hash>
// </response>
if result == 0 {
print("authcode = \(xml.getChildContent(tagPath: "authcode")!)")
print("Success.")
}
else {
print("Failed.")
}
}