NetTalk Central

Author Topic: CyberSource Payment API  (Read 937 times)

JHojka

  • Newbie
  • *
  • Posts: 46
    • View Profile
    • Email
CyberSource Payment API
« on: April 13, 2023, 07:07:23 AM »
Code for creating the post and header information.

  MAP
CyberSource:FormatMessage (),STRING
CyberSource:GenerateHeader (*NetWebClient pWeb,STRING PostString)
CyberSource:GenerateSignatureFromParams(STRING signatureParams, STRING secretKey),STRING
  END

CODE
      PostURL = 'https://apitest.cybersource.com/pts/v2/payments'
      PostString = CyberSource:FormatMessage()
      CyberSource:GenerateHeader(ThisWebClient, PostString)     
      ThisWebClient.Post(PostURL,PostString)

CyberSource:FormatMessage PROCEDURE

element                  Group,Name('element')
clientReferenceInformation Group,Name('clientReferenceInformation')
code_                        STRING(255),Name('code')
                           End
paymentInformation         Group,Name('paymentInformation')
card                         Group,Name('card')
number                         STRING(255),Name('number')
expirationMonth                STRING(255),Name('expirationMonth')
expirationYear                 STRING(255),Name('expirationYear')
                             End
                           End
orderInformation           Group,Name('orderInformation')
amountDetails                Group,Name('amountDetails')
totalAmount                    STRING(255),Name('totalAmount')
currency                       STRING(255),Name('currency')
                             End
billTo                       Group,Name('billTo')
firstName                      STRING(255),Name('firstName')
lastName                       STRING(255),Name('lastName')
address1                       STRING(255),Name('address1')
locality                       STRING(255),Name('locality')
administrativeArea             STRING(255),Name('administrativeArea')
postalCode                     STRING(255),Name('postalCode')
country                        STRING(255),Name('country')
email                          STRING(255),Name('email')
phoneNumber                    STRING(255),Name('phoneNumber')
                             End
                           End
                         End

json                     jsonClass
strElement StringTheory

  CODE

  CLEAR(element)
  element.clientReferenceInformation.code_ = 'TC50171_3'
  element.paymentInformation.card.number = '4111111111111111'
  element.paymentInformation.card.expirationMonth = '12'
  element.paymentInformation.card.expirationYear = '2031'
  element.orderInformation.amountDetails.totalAmount = '102.21'
  element.orderInformation.amountDetails.currency = 'USD'
  element.orderInformation.billTo.firstName = 'John'
  element.orderInformation.billTo.lastName = 'Doe'
  element.orderInformation.billTo.address1 = '1 Market St'
  element.orderInformation.billTo.locality = 'san francisco'
  element.orderInformation.billTo.administrativeArea = 'CA'
  element.orderInformation.billTo.postalCode = '94105'
  element.orderInformation.billTo.country = 'US'
  element.orderInformation.billTo.email = 'test@cybs.com'
  element.orderInformation.billTo.phoneNumber = '4158880000'
  json.start()
  json.SetTagCase(jf:CaseAsIs)
  json.Save(element,strElement)
  strElement.LineEndings(st:Unix)
  return strElement.GetValue()

CyberSource:GenerateHeader PROCEDURE  (*NetWebClient pWeb,STRING PostString)

lSecretKey STRING(100)
KeyId STRING(100)
MerchantId  STRING(100)
HostName  STRING(100)
HostTarget  STRING(100)

strPost   StringTheory
strHost   StringTheory

  CODE

      lSecretKey = 'yourkey'
      KeyId = 'yourid'
      MerchantId = 'yourco'

      HostName = 'apitest.cybersource.com'
      HostTarget = 'post /pts/v2/payments/'

      strPost.SetValue(CLIP(PostString))
      strPost.SetValue(NetMakeHash(strPost.GetValuePtr(),strPost.Length(),net:CALG_SHA_256))
      strPost.Base64Encode()

      strHost.SetValue('host: ' & CLIP(HostName) & '<10>' &|
                                   '(request-target): ' & CLIP(HostTarget) & '<10>' &|
                                   'digest: SHA-256=' & strPost.GetValue() & '<10>' &|
                                   'v-c-merchant-id: ' & CLIP(MerchantId) & '')

      pWeb.CustomHeader = 'v-c-merchant-id: ' & CLIP(MerchantId) & '<13,10>' &|
                                   'v-c-date: "' & CLIP(pWeb.CreateGMTDate()) & '"' & '<13,10>' &|
                                   'digest: SHA-256=' & strPost.GetValue() & '<13,10>' &|
                                   'Signature: keyid="' & CLIP(KeyId) & '", algorithm="HmacSHA256",' &|
                                   ' headers="host (request-target) digest v-c-merchant-id", signature="' &|
                                   CyberSource:GenerateSignatureFromParams(strHost.GetValue(), lSecretKey) & '"'

CyberSource:GenerateSignatureFromParams PROCEDURE  (STRING signatureParams,STRING secretKey) ! Declare Procedure

strParams StringTheory
strKey StringTheory

  CODE

  strParams.SetValue(signatureParams,st:clip)
  strKey.SetValue(secretKey,st:clip)
  strKey.Base64Decode()
  strParams.ToUnicode()
  strParams.SetValue(NetMakeHMAC(strParams.GetValuePtr(), strParams.Length(), strKey.GetValue(), net:CALG_SHA_256))
  strParams.Base64Encode()
  return strParams.GetValue()