Skip to content

Instantly share code, notes, and snippets.

@jagdeepsingh
Last active February 10, 2024 18:25
Show Gist options
  • Save jagdeepsingh/bda3b98ff2ff3727cac82a61e859f606 to your computer and use it in GitHub Desktop.
Save jagdeepsingh/bda3b98ff2ff3727cac82a61e859f606 to your computer and use it in GitHub Desktop.
BlueSnap Payment Gateway

Description Link
Home https://home.bluesnap.com
Developer Hub https://developers.bluesnap.com
Authentication https://developers.bluesnap.com/docs/authentication
Payment API https://developers.bluesnap.com/v8976-JSON/docs

Contents:

1 Register for merchant account

Log in to developer console here.

If you don't have an account already, sign up for it here

Back to Top

2 Request API credentials

On the dashboard, go to Settings > API Settings and fill out below form:

Click on "Request API Credentials".

Under API Credentials, note down Username, Password, and Default Store ID.

Under Client Side Encryption, note Client Side Encryption Key.

These will be used to make API requests later.

Back to Top

Add JavaScript file on HTML page:

= javascript_include_tag 'https://gateway.bluesnap.com/js/cse/v1.0.3/bluesnap.js'

In the payment form, set data-bluesnap attribute on credit card fields which need to be encrypted. For each of those fields, a new hidden field will be created by BlueSnap with its name attribute set to value of data-bluesnap. See details here. You must do this for card number and security code (cvv) fields. For others, it is optional.

Add following JavaScript:

var blueSnap = new BlueSnap(<CLIENT_SIDE_ENCRYPTION_KEY>);
blueSnap.encrypt(<FORM_ID>);

Your fields have been encrypted. The encrypted values can now be used to make transactions on BlueSnap.

Back to Top

4 Vaulted shopper

uri = URI('https://ws.bluesnap.com/services/2/vaulted-shoppers')

http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true

credentials = '<USERNAME>:<PASSWORD>'
authorization = "Basic #{Base64.encode64(credentials).strip}"

request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['Authorization'] = authorization

request.body = { paymentSources: {
                  creditCardInfo: [{
                    creditCard: {
                      expirationYear: 2020,
                      encryptedCardNumber: '$bs_card_number_enc=',
                      encryptedSecurityCode: '$bs_security_code_enc=',
                      expirationMonth: '09'
                    }
                  }]
                 },
                 firstName: 'Jagdeep',
                 lastName: 'Singh',
                 shopperCurrency: 'USD' }.to_json
                 
response = http.request(request)
 => #<Net::HTTPOK 200 200 readbody=true>
 
response.code
 => "200"
 
JSON.parse response.body
 => => {"vaultedShopperId"=>12341111, "firstName"=>"Jagdeep", "lastName"=>"Singh", "softDescriptor"=>"BLS&#x2a;TODO", "shopperCurrency"=>"USD", "paymentSources"=>{"creditCardInfo"=>[{"billingContactInfo"=>{"firstName"=>"Jagdeep", "lastName"=>"Singh"}, "creditCard"=>{"cardLastFourDigits"=>"1111", "cardType"=>"VISA", "cardSubType"=>"CREDIT", "expirationMonth"=>"09", "expirationYear"=>"2020"}, "processingInfo"=>{"cvvResponseCode"=>"ND", "avsResponseCodeZip"=>"U", "avsResponseCodeAddress"=>"U", "avsResponseCodeName"=>"U"}}]}}

In sandbox mode, replace https://ws.bluesnap.com with https://sandbox.bluesnap.com.

Back to Top

uri = URI('https://ws.bluesnap.com/services/2/transactions')

http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true

request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['Authorization'] = authorization

request.body = { amount: 19.99,
                 vaultedShopperId: 12341111,
                 recurringTransaction: 'ECOMMERCE',
                 currency: 'USD',
                 creditCard: {
                   cardLastFourDigits: 1111,
                   cardType: 'VISA'
                 },
                 cardTransactionType: 'AUTH_CAPTURE' }.to_json
                 
response = http.request(request)
 => #<Net::HTTPOK 200 200 readbody=true>
 
JSON.parse response.body
 => {"cardTransactionType"=>"AUTH_CAPTURE", "transactionId"=>"1011011001", "recurringTransaction"=>"ECOMMERCE", "softDescriptor"=>"BLS&#x2a;TODO", "amount"=>19.99, "currency"=>"USD", "vaultedShopperId"=>12341111, "creditCard"=>{"cardLastFourDigits"=>"1111", "cardType"=>"VISA", "cardSubType"=>"CREDIT"}, "processingInfo"=>{"processingStatus"=>"success", "avsResponseCodeZip"=>"U", "avsResponseCodeAddress"=>"U", "avsResponseCodeName"=>"U"}}

Back to Top

Use the code from purchase above. Only change is in the value of cardTransactionType. Set it to "AUTH_ONLY" for authorize.

request.body = { amount: 19.99,
                 vaultedShopperId: 12341111,
                 recurringTransaction: 'ECOMMERCE',
                 currency: 'USD',
                 creditCard: {
                   cardLastFourDigits: 1111,
                   cardType: 'VISA'
                 },
                 cardTransactionType: 'AUTH_ONLY' }.to_json

Back to Top

7.1 Full capture

uri = URI('https://ws.bluesnap.com/services/2/transactions')

http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true

request = Net::HTTP::Put.new(uri)
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['Authorization'] = authorization

request.body = { cardTransactionType: 'CAPTURE', transactionId: 1234511111 }.to_json

response = http.request(request)
 => #<Net::HTTPOK 200 200 readbody=true>
 
JSON.parse response.body
 => {"cardTransactionType"=>"CAPTURE", "transactionId"=>"1234511111", "recurringTransaction"=>"ECOMMERCE", "softDescriptor"=>"BLS&#x2a;TODO", "amount"=>100.0, "openToCapture"=>0, "currency"=>"USD", "cardHolderInfo"=>{"firstName"=>"Jagdeep", "lastName"=>"Singh"}, "vaultedShopperId"=>12341111, "creditCard"=>{"cardLastFourDigits"=>"1111", "cardType"=>"VISA", "cardSubType"=>"CREDIT"}, "processingInfo"=>{"processingStatus"=>"SUCCESS", "avsResponseCodeZip"=>"U", "avsResponseCodeAddress"=>"U", "avsResponseCodeName"=>"U"}}

7.2 Partial capture

request.body = { cardTransactionType: 'CAPTURE', transactionId: 1234511111, amount: 19.99 }.to_json

response = http.request(request)
 
JSON.parse response.body
 => {"cardTransactionType"=>"CAPTURE", "transactionId"=>"1234511111", "recurringTransaction"=>"ECOMMERCE", "softDescriptor"=>"BLS&#x2a;TODO", "amount"=>19.99, "openToCapture"=>80.01, "currency"=>"USD", "cardHolderInfo"=>{"firstName"=>"Jagdeep", "lastName"=>"Singh"}, "vaultedShopperId"=>12341111, "creditCard"=>{"cardLastFourDigits"=>"1111", "cardType"=>"VISA", "cardSubType"=>"CREDIT"}, "processingInfo"=>{"processingStatus"=>"SUCCESS", "avsResponseCodeZip"=>"U", "avsResponseCodeAddress"=>"U", "avsResponseCodeName"=>"U"}}

Back to Top

Same as capture but set cardTransactionType to "AUTH_REVERSAL".

request.body = { cardTransactionType: 'AUTH_REVERSAL', transactionId: 1234511111 }.to_json

response = http.request(request)
 
JSON.parse response.body
 => {"cardTransactionType"=>"AUTH_REVERSAL", "transactionId"=>"1234511111", "recurringTransaction"=>"ECOMMERCE", "softDescriptor"=>"BLS&#x2a;TODO", "amount"=>100.0, "openToCapture"=>100.0, "currency"=>"USD", "cardHolderInfo"=>{"firstName"=>"Jagdeep", "lastName"=>"Singh"}, "vaultedShopperId"=>12341111, "creditCard"=>{"cardLastFourDigits"=>"1111", "cardType"=>"VISA", "cardSubType"=>"CREDIT"}, "processingInfo"=>{"processingStatus"=>"SUCCESS", "avsResponseCodeZip"=>"U", "avsResponseCodeAddress"=>"U", "avsResponseCodeName"=>"U"}}

Back to Top

9.1 Full refund

uri = URI('https://ws.bluesnap.com/services/2/transactions/1234511111/refund')

http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true

request = Net::HTTP::Put.new(uri)
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['Authorization'] = authorization

request.body = {}.to_json
response = http.request(request)
 => #<Net::HTTPNoContent 204 204 readbody=true>

response.body
 => nil

9.2 Partial refund

To refund a partial amount, pass it in query string as below:

uri = URI('https://ws.bluesnap.com/services/2/transactions/1234511111/refund?amount=19.99')

http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = true

request = Net::HTTP::Put.new(uri)
request['Content-Type'] = 'application/json'
request['Accept'] = 'application/json'
request['Authorization'] = authorization

response = http.request(request)
 => #<Net::HTTPNoContent 204 204 readbody=true>

Back to Top

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment