Sample code for 30+ languages & platforms
Swift

Calculate a X_RISKIFIED_HMAC_SHA256 for riskified.com API Calls

See more REST Misc Examples

Demonstrates how to calculate the value for the X_RISKIFIED_HMAC_SHA256 header for riskified.com HTTP requests (REST API calls).

Chilkat Swift Downloads

Swift

func chilkatTest() {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Create the JSON that is to be the body of the HTTP request.
    // The JSON created by this code is shown at the bottom of this example.
    // 
    // The JSON Code Generator at http://tools.chilkat.io/ can be used
    // to generate the following code from sample JSON.

    let json = CkoJsonObject()!
    json.updateNull(jsonPath: "order.cancel_reason")
    json.updateNull(jsonPath: "order.cancelled_at")
    json.updateString(jsonPath: "order.cart_token", value: "68778783ad298f1c80c3bafcddeea02f")
    json.updateNull(jsonPath: "order.closed_at")
    json.updateString(jsonPath: "order.created_at", value: "2008-01-10T11:00:00-05:00")
    json.updateString(jsonPath: "order.currency", value: "USD")
    json.updateString(jsonPath: "order.email", value: "bob.norman@hostmail.com")
    json.updateString(jsonPath: "order.gateway", value: "authorize_net")
    json.updateNumber(jsonPath: "order.id", numericStr: "450789469")
    json.updateString(jsonPath: "order.total_discounts", value: "0.00")
    json.updateString(jsonPath: "order.total_price", value: "409.94")
    json.updateString(jsonPath: "order.updated_at", value: "2008-01-10T11:00:00-05:00")
    json.updateString(jsonPath: "order.note", value: "some note made by the shop’s stuff member")
    json.updateNull(jsonPath: "order.browser_ip")
    json.updateString(jsonPath: "order.discount_codes[0].amount", value: "10.00")
    json.updateString(jsonPath: "order.discount_codes[0].code", value: "TENOFF")
    json.updateString(jsonPath: "order.line_items[0].title", value: "IPod Nano - 8gb - green")
    json.updateNumber(jsonPath: "order.line_items[0].price", numericStr: "199.00")
    json.updateNumber(jsonPath: "order.line_items[0].product_id", numericStr: "632910392")
    json.updateNumber(jsonPath: "order.line_items[0].quantity", numericStr: "1")
    json.updateString(jsonPath: "order.line_items[0].sku", value: "IPOD2008GREEN")
    json.updateString(jsonPath: "order.shipping_lines[0].code", value: "Free Shipping")
    json.updateString(jsonPath: "order.shipping_lines[0].price", value: "0.00")
    json.updateString(jsonPath: "order.shipping_lines[0].title", value: "Free Shipping")
    json.updateNull(jsonPath: "order.payment_details.avs_result_code")
    json.updateNull(jsonPath: "order.payment_details.credit_card_bin")
    json.updateString(jsonPath: "order.payment_details.credit_card_company", value: "Visa")
    json.updateString(jsonPath: "order.payment_details.credit_card_number", value: "XXXX-XXXX-XXXX-4242")
    json.updateNull(jsonPath: "order.payment_details.cvv_result_code")
    json.updateString(jsonPath: "order.payment_details.authorization_id", value: "RK346IK124")
    json.updateString(jsonPath: "order.billing_address.address1", value: "Chestnut Street 92")
    json.updateString(jsonPath: "order.billing_address.address2", value: "")
    json.updateString(jsonPath: "order.billing_address.city", value: "Louisville")
    json.updateNull(jsonPath: "order.billing_address.company")
    json.updateString(jsonPath: "order.billing_address.country", value: "United States")
    json.updateString(jsonPath: "order.billing_address.country_code", value: "US")
    json.updateString(jsonPath: "order.billing_address.first_name", value: "Bob")
    json.updateString(jsonPath: "order.billing_address.last_name", value: "Norman")
    json.updateString(jsonPath: "order.billing_address.name", value: "Bob Norman")
    json.updateString(jsonPath: "order.billing_address.phone", value: "555-625-1199")
    json.updateString(jsonPath: "order.billing_address.province", value: "Kentucky")
    json.updateString(jsonPath: "order.billing_address.province_code", value: "KY")
    json.updateString(jsonPath: "order.billing_address.zip", value: "40202")
    json.updateString(jsonPath: "order.shipping_address.address1", value: "Chestnut Street 92")
    json.updateString(jsonPath: "order.shipping_address.address2", value: "")
    json.updateString(jsonPath: "order.shipping_address.city", value: "Louisville")
    json.updateNull(jsonPath: "order.shipping_address.company")
    json.updateString(jsonPath: "order.shipping_address.country", value: "United States")
    json.updateString(jsonPath: "order.shipping_address.country_code", value: "US")
    json.updateString(jsonPath: "order.shipping_address.first_name", value: "Bob")
    json.updateString(jsonPath: "order.shipping_address.last_name", value: "Norman")
    json.updateString(jsonPath: "order.shipping_address.name", value: "Bob Norman")
    json.updateString(jsonPath: "order.shipping_address.phone", value: "555-625-1199")
    json.updateString(jsonPath: "order.shipping_address.province", value: "Kentucky")
    json.updateString(jsonPath: "order.shipping_address.province_code", value: "KY")
    json.updateString(jsonPath: "order.shipping_address.zip", value: "40202")
    json.updateString(jsonPath: "order.customer.created_at", value: "2013-04-23T13:36:50-04:00")
    json.updateString(jsonPath: "order.customer.email", value: "bob.norman@hostmail.com")
    json.updateString(jsonPath: "order.customer.first_name", value: "Bob")
    json.updateNumber(jsonPath: "order.customer.id", numericStr: "207119551")
    json.updateString(jsonPath: "order.customer.last_name", value: "Norman")
    json.updateNull(jsonPath: "order.customer.note")
    json.updateNumber(jsonPath: "order.customer.orders_count", numericStr: "0")
    json.updateBool(jsonPath: "order.customer.verified_email", value: true)

    // Emit the JSON in compact format..
    json.emitCompact = true
    // Get the JSON that will be HMAC'd and will also be the contents of the HTTP request body.
    var jsonBody: String? = json.emit()

    let crypt = CkoCrypt2()!
    crypt.encodingMode = "hex"
    crypt.hashAlgorithm = "sha256"
    crypt.macAlgorithm = "hmac"

    crypt.setMacKeyString(key: "55fe0f4d4023bbdfbc124cabd88bf9bb")

    var hmacHexStr: String? = crypt.macStringENC(inText: jsonBody)
    if crypt.lastMethodSuccess != true {
        print("\(crypt.lastErrorText!)")
        return
    }

    // We need a lowercase hmacHexStr...
    let sbHmacHex = CkoStringBuilder()!
    sbHmacHex.append(value: hmacHexStr)
    sbHmacHex.toLowercase()

    hmacHexStr = sbHmacHex.getAsString()

    print("The value of the X_RISKIFIED_HMAC_SHA256 should be: \(hmacHexStr!)")

    // This example is only to show the HMAC SHA256 calculation.
    // See examples of sending REST requests to riskified.com at http://rest-examples.chilkat.io/riskified/default.cshtml

    // ----------------------------------------------
    // This is the JSON created by the above code..

    // {
    //   "order": {
    //     "cancel_reason": null,
    //     "cancelled_at": null,
    //     "cart_token": "68778783ad298f1c80c3bafcddeea02f",
    //     "closed_at": null,
    //     "created_at": "2008-01-10T11:00:00-05:00",
    //     "currency": "USD",
    //     "email": "bob.norman@hostmail.com",
    //     "gateway": "authorize_net",
    //     "id": 450789469,
    //     "total_discounts": "0.00",
    //     "total_price": "409.94",
    //     "updated_at": "2008-01-10T11:00:00-05:00",
    //     "note": "some note made by the shop’s stuff member",
    //     "browser_ip": null,
    //     "discount_codes": [
    //         {
    //             "amount": "10.00",
    //             "code": "TENOFF"
    //         }
    //     ],
    //     "line_items": [
    //         {
    //             "title": "IPod Nano - 8gb - green",
    //             "price": 199.00,
    //             "product_id": 632910392,
    //             "quantity": 1,
    //             "sku": "IPOD2008GREEN"
    //         }
    //     ],
    //     "shipping_lines": [
    //         {
    //             "code": "Free Shipping",
    //             "price": "0.00",
    //             "title": "Free Shipping"
    //         }
    //     ],
    //     "payment_details": {
    //         "avs_result_code": null,
    //         "credit_card_bin": null,
    //         "credit_card_company": "Visa",
    //         "credit_card_number": "XXXX-XXXX-XXXX-4242",
    //         "cvv_result_code": null,
    //         "authorization_id": "RK346IK124"
    //     },
    //     "billing_address": {
    //         "address1": "Chestnut Street 92",
    //         "address2": "",
    //         "city": "Louisville",
    //         "company": null,
    //         "country": "United States",
    //         "country_code": "US",
    //         "first_name": "Bob",
    //         "last_name": "Norman",
    //         "name": "Bob Norman",
    //         "phone": "555-625-1199",
    //         "province": "Kentucky",
    //         "province_code": "KY",
    //         "zip": "40202"
    //     },
    //     "shipping_address": {
    //         "address1": "Chestnut Street 92",
    //         "address2": "",
    //         "city": "Louisville",
    //         "company": null,
    //         "country": "United States",
    //         "country_code": "US",
    //         "first_name": "Bob",
    //         "last_name": "Norman",
    //         "name": "Bob Norman",
    //         "phone": "555-625-1199",
    //         "province": "Kentucky",
    //         "province_code": "KY",
    //         "zip": "40202"
    //     },
    //     "customer": {
    //         "created_at": "2013-04-23T13:36:50-04:00",
    //         "email": "bob.norman@hostmail.com",
    //         "first_name": "Bob",
    //         "id": 207119551,
    //         "last_name": "Norman",
    //         "note": null,
    //         "orders_count": 0,
    //         "verified_email": true
    //     }
    //  }
    // }

}