Chilkat Online Tools

PureBasic / Afterpay Online US API v2 / Create Checkout

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

    ; This example assumes the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success.i

    CkHttp::setCkBasicAuth(http, 1)
    CkHttp::setCkLogin(http, "100100248")
    CkHttp::setCkPassword(http, "117f3fe5a01a0e400f3e4782baf8ffa43a9d21e9217a15f50d545bc72480d081ecaecc1d6a42ac7a785b7437232bd4cf62dbee679b78a75a3892547059018f9b")

    ; Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    ; The following JSON is sent in the request body.

    ; {
    ;   "amount": {
    ;     "amount": "37.00",
    ;     "currency": "USD"
    ;   },
    ;   "consumer": {
    ;     "phoneNumber": "2120000000",
    ;     "givenNames": "Joe",
    ;     "surname": "Consumer",
    ;     "email": "test@afterpay.com"
    ;   },
    ;   "billing": {
    ;     "name": "Joe Consumer",
    ;     "line1": "1004 Point Lobos Ave",
    ;     "area1": "San Francisco",
    ;     "region": "CA",
    ;     "postcode": "94121",
    ;     "countryCode": "US",
    ;     "phoneNumber": "2120000000"
    ;   },
    ;   "shipping": {
    ;     "name": "Joe Consumer",
    ;     "line1": "1004 Point Lobos Ave",
    ;     "area1": "San Francisco",
    ;     "region": "CA",
    ;     "postcode": "94121",
    ;     "countryCode": "US",
    ;     "phoneNumber": "2120000000"
    ;   },
    ;   "items": [
    ;     {
    ;       "name": "Blue Carabiner",
    ;       "sku": "12341234",
    ;       "quantity": 1,
    ;       "pageUrl": "https://www.afterpay-merchant.com/carabiner-354193.html",
    ;       "imageUrl": "https://img.afterpay-merchant.com/carabiner-7378-391453-1.jpg",
    ;       "price": {
    ;         "amount": "40.00",
    ;         "currency": "USD"
    ;       },
    ;       "categories": [
    ;         [
    ;           "Sporting Goods",
    ;           "Climbing Equipment",
    ;           "Climbing",
    ;           "Climbing Carabiners"
    ;         ],
    ;         [
    ;           "Discounts",
    ;           "Climbing"
    ;         ]
    ;       ]
    ;     },
    ;     {
    ;       "name": "Jeans",
    ;       "sku": "12341235",
    ;       "quantity": 1,
    ;       "pageUrl": "https://www.afterpay-merchant.com/jeans-354193.html",
    ;       "imageUrl": "https://img.afterpay-merchant.com/jeans-7378-391453-1.jpg",
    ;       "price": {
    ;         "amount": "20.00",
    ;         "currency": "USD"
    ;       }
    ;     }
    ;   ],
    ;   "discounts": [
    ;     {
    ;       "displayName": "10% Off Subtotal",
    ;       "amount": {
    ;         "amount": "3.00",
    ;         "currency": "USD"
    ;       }
    ;     }
    ;   ],
    ;   "merchant": {
    ;     "redirectConfirmUrl": "https://www.afterpay-merchant.com/confirm",
    ;     "redirectCancelUrl": "https://www.afterpay-merchant.com/cancel"
    ;   },
    ;   "merchantReference": "merchant-order-number",
    ;   "taxAmount": {
    ;     "amount": "0.00",
    ;     "currency": "USD"
    ;   },
    ;   "shippingAmount": {
    ;     "amount": "10.00",
    ;     "currency": "USD"
    ;   }
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"amount.amount","37.00")
    CkJsonObject::ckUpdateString(json,"amount.currency","USD")
    CkJsonObject::ckUpdateString(json,"consumer.phoneNumber","2120000000")
    CkJsonObject::ckUpdateString(json,"consumer.givenNames","Joe")
    CkJsonObject::ckUpdateString(json,"consumer.surname","Consumer")
    CkJsonObject::ckUpdateString(json,"consumer.email","test@afterpay.com")
    CkJsonObject::ckUpdateString(json,"billing.name","Joe Consumer")
    CkJsonObject::ckUpdateString(json,"billing.line1","1004 Point Lobos Ave")
    CkJsonObject::ckUpdateString(json,"billing.area1","San Francisco")
    CkJsonObject::ckUpdateString(json,"billing.region","CA")
    CkJsonObject::ckUpdateString(json,"billing.postcode","94121")
    CkJsonObject::ckUpdateString(json,"billing.countryCode","US")
    CkJsonObject::ckUpdateString(json,"billing.phoneNumber","2120000000")
    CkJsonObject::ckUpdateString(json,"shipping.name","Joe Consumer")
    CkJsonObject::ckUpdateString(json,"shipping.line1","1004 Point Lobos Ave")
    CkJsonObject::ckUpdateString(json,"shipping.area1","San Francisco")
    CkJsonObject::ckUpdateString(json,"shipping.region","CA")
    CkJsonObject::ckUpdateString(json,"shipping.postcode","94121")
    CkJsonObject::ckUpdateString(json,"shipping.countryCode","US")
    CkJsonObject::ckUpdateString(json,"shipping.phoneNumber","2120000000")
    CkJsonObject::ckUpdateString(json,"items[0].name","Blue Carabiner")
    CkJsonObject::ckUpdateString(json,"items[0].sku","12341234")
    CkJsonObject::ckUpdateInt(json,"items[0].quantity",1)
    CkJsonObject::ckUpdateString(json,"items[0].pageUrl","https://www.afterpay-merchant.com/carabiner-354193.html")
    CkJsonObject::ckUpdateString(json,"items[0].imageUrl","https://img.afterpay-merchant.com/carabiner-7378-391453-1.jpg")
    CkJsonObject::ckUpdateString(json,"items[0].price.amount","40.00")
    CkJsonObject::ckUpdateString(json,"items[0].price.currency","USD")
    CkJsonObject::ckUpdateString(json,"items[0].categories[0][0]","Sporting Goods")
    CkJsonObject::ckUpdateString(json,"items[0].categories[0][1]","Climbing Equipment")
    CkJsonObject::ckUpdateString(json,"items[0].categories[0][2]","Climbing")
    CkJsonObject::ckUpdateString(json,"items[0].categories[0][3]","Climbing Carabiners")
    CkJsonObject::ckUpdateString(json,"items[0].categories[1][0]","Discounts")
    CkJsonObject::ckUpdateString(json,"items[0].categories[1][1]","Climbing")
    CkJsonObject::ckUpdateString(json,"items[1].name","Jeans")
    CkJsonObject::ckUpdateString(json,"items[1].sku","12341235")
    CkJsonObject::ckUpdateInt(json,"items[1].quantity",1)
    CkJsonObject::ckUpdateString(json,"items[1].pageUrl","https://www.afterpay-merchant.com/jeans-354193.html")
    CkJsonObject::ckUpdateString(json,"items[1].imageUrl","https://img.afterpay-merchant.com/jeans-7378-391453-1.jpg")
    CkJsonObject::ckUpdateString(json,"items[1].price.amount","20.00")
    CkJsonObject::ckUpdateString(json,"items[1].price.currency","USD")
    CkJsonObject::ckUpdateString(json,"discounts[0].displayName","10% Off Subtotal")
    CkJsonObject::ckUpdateString(json,"discounts[0].amount.amount","3.00")
    CkJsonObject::ckUpdateString(json,"discounts[0].amount.currency","USD")
    CkJsonObject::ckUpdateString(json,"merchant.redirectConfirmUrl","https://www.afterpay-merchant.com/confirm")
    CkJsonObject::ckUpdateString(json,"merchant.redirectCancelUrl","https://www.afterpay-merchant.com/cancel")
    CkJsonObject::ckUpdateString(json,"merchantReference","merchant-order-number")
    CkJsonObject::ckUpdateString(json,"taxAmount.amount","0.00")
    CkJsonObject::ckUpdateString(json,"taxAmount.currency","USD")
    CkJsonObject::ckUpdateString(json,"shippingAmount.amount","10.00")
    CkJsonObject::ckUpdateString(json,"shippingAmount.currency","USD")

    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")
    CkHttp::ckSetRequestHeader(http,"Accept","application/json")

    resp.i = CkHttp::ckPostJson3(http,"https://api.us-sandbox.afterpay.com/v2/checkouts","application/json",json)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)
    CkHttpResponse::ckDispose(resp)



    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-u '100100248:117f3fe5a01a0e400f3e4782baf8ffa43a9d21e9217a15f50d545bc72480d081ecaecc1d6a42ac7a785b7437232bd4cf62dbee679b78a75a3892547059018f9b'
	-H "Accept: application/json"
	-H "Content-Type: application/json"
	-d '{
         "amount": {
            "amount": "37.00",
            "currency": "USD"
         },
         "consumer": {
            "phoneNumber": "2120000000",
            "givenNames": "Joe",
            "surname": "Consumer",
            "email": "test@afterpay.com"
         },
        "billing": {
            "name": "Joe Consumer",
            "line1": "1004 Point Lobos Ave",
            "area1": "San Francisco",
            "region": "CA",
            "postcode": "94121",
            "countryCode": "US",
            "phoneNumber": "2120000000"
        },
        "shipping": {
            "name": "Joe Consumer",
            "line1": "1004 Point Lobos Ave",
            "area1": "San Francisco",
            "region": "CA",
            "postcode": "94121",
            "countryCode": "US",
            "phoneNumber": "2120000000"
        },
        "items":[
            {
                "name": "Blue Carabiner",
                "sku": "12341234",
                "quantity": 1,
                "pageUrl": "https://www.afterpay-merchant.com/carabiner-354193.html",
                "imageUrl": "https://img.afterpay-merchant.com/carabiner-7378-391453-1.jpg",
                "price": {
                    "amount": "40.00",
                    "currency": "USD"
                },
                "categories": [
                            ["Sporting Goods", "Climbing Equipment", "Climbing", "Climbing Carabiners"],
                            ["Discounts", "Climbing"]
                ]
            },
            {
             "name": "Jeans",
             "sku": "12341235",
             "quantity": 1,
             "pageUrl": "https://www.afterpay-merchant.com/jeans-354193.html",
             "imageUrl": "https://img.afterpay-merchant.com/jeans-7378-391453-1.jpg",
             "price": {
                 "amount": "20.00",
                 "currency": "USD"
                }
            }
         ],
         "discounts": [
            {
                "displayName": "10% Off Subtotal",
                "amount": {
                    "amount": "3.00",
                    "currency": "USD"
                }
            }
         ],
         "merchant": {
            "redirectConfirmUrl": "https://www.afterpay-merchant.com/confirm",
            "redirectCancelUrl": "https://www.afterpay-merchant.com/cancel"
         },
         "merchantReference": "merchant-order-number",
         "taxAmount": {
            "amount": "0.00",
            "currency": "USD"
         },
         "shippingAmount": {
             "amount": "10.00",
             "currency": "USD"
         }
      }'
https://api.us-sandbox.afterpay.com/v2/checkouts

Postman Collection Item JSON

{
  "name": "Create Checkout",
  "event": [
    {
      "listen": "test",
      "script": {
        "id": "6e77d6b5-ef19-40b1-a665-41bbc1cff1c8",
        "exec": [
          "var jsonData = JSON.parse(responseBody);",
          "postman.setEnvironmentVariable(\"token\", jsonData.token);"
        ],
        "type": "text/javascript"
      }
    },
    {
      "listen": "prerequest",
      "script": {
        "id": "28dad3a2-886b-4a2a-8605-6257dee08b82",
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Accept",
        "value": "application/json"
      },
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n         \"amount\": {\n            \"amount\": \"37.00\",\n            \"currency\": \"USD\"\n         },\n         \"consumer\": {\n            \"phoneNumber\": \"2120000000\",\n            \"givenNames\": \"Joe\",\n            \"surname\": \"Consumer\",\n            \"email\": \"test@afterpay.com\"\n         },\n        \"billing\": {\n            \"name\": \"Joe Consumer\",\n            \"line1\": \"1004 Point Lobos Ave\",\n            \"area1\": \"San Francisco\",\n            \"region\": \"CA\",\n            \"postcode\": \"94121\",\n            \"countryCode\": \"US\",\n            \"phoneNumber\": \"2120000000\"\n        },\n        \"shipping\": {\n            \"name\": \"Joe Consumer\",\n            \"line1\": \"1004 Point Lobos Ave\",\n            \"area1\": \"San Francisco\",\n            \"region\": \"CA\",\n            \"postcode\": \"94121\",\n            \"countryCode\": \"US\",\n            \"phoneNumber\": \"2120000000\"\n        },\n        \"items\":[\n            {\n                \"name\": \"Blue Carabiner\",\n                \"sku\": \"12341234\",\n                \"quantity\": 1,\n                \"pageUrl\": \"https://www.afterpay-merchant.com/carabiner-354193.html\",\n                \"imageUrl\": \"https://img.afterpay-merchant.com/carabiner-7378-391453-1.jpg\",\n                \"price\": {\n                    \"amount\": \"40.00\",\n                    \"currency\": \"USD\"\n                },\n                \"categories\": [\n                            [\"Sporting Goods\", \"Climbing Equipment\", \"Climbing\", \"Climbing Carabiners\"],\n                            [\"Discounts\", \"Climbing\"]\n                ]\n            },\n            {\n             \"name\": \"Jeans\",\n             \"sku\": \"12341235\",\n             \"quantity\": 1,\n             \"pageUrl\": \"https://www.afterpay-merchant.com/jeans-354193.html\",\n             \"imageUrl\": \"https://img.afterpay-merchant.com/jeans-7378-391453-1.jpg\",\n             \"price\": {\n                 \"amount\": \"20.00\",\n                 \"currency\": \"USD\"\n                }\n            }\n         ],\n         \"discounts\": [\n            {\n                \"displayName\": \"10% Off Subtotal\",\n                \"amount\": {\n                    \"amount\": \"3.00\",\n                    \"currency\": \"USD\"\n                }\n            }\n         ],\n         \"merchant\": {\n            \"redirectConfirmUrl\": \"https://www.afterpay-merchant.com/confirm\",\n            \"redirectCancelUrl\": \"https://www.afterpay-merchant.com/cancel\"\n         },\n         \"merchantReference\": \"merchant-order-number\",\n         \"taxAmount\": {\n            \"amount\": \"0.00\",\n            \"currency\": \"USD\"\n         },\n         \"shippingAmount\": {\n             \"amount\": \"10.00\",\n             \"currency\": \"USD\"\n         }\n      }"
    },
    "url": {
      "raw": "https://api.us-sandbox.afterpay.com/v2/checkouts",
      "protocol": "https",
      "host": [
        "api",
        "us-sandbox",
        "afterpay",
        "com"
      ],
      "path": [
        "v2",
        "checkouts"
      ]
    },
    "description": "This endpoint creates a checkout that is used to initiate the afterpay payment process. Afterpay uses the information in the checkout request to assist with the consumer’s pre-approval process."
  },
  "response": [
  ]
}