---
updatedAt: 2025-11-11T01:51:38.000Z
agentTools:
  projectIndex: https://docs.velosimo.io/llms.txt
---

# How to implement Payment SDK

## Algorithms

There are five algorithms you must create

### initialize\_payment

1. Create an algorithm called initialize\_payment with a parameter called params

![1920](https://files.readme.io/b7f150e-ss1.png "ss1.png")

2. Write your code in the code block

![1519](https://files.readme.io/0708efe-ss2.png "ss2.png")

There are three main components you need to make the algorithm work.

* Settings object
* Payment object
* Response method

```ruby initialize_payment
Tenant.notify(message: "Initialize Example payment", type: :info)
Tenant.notify(message: "#{params}", type: :info)

# params will contain fields
# - payment = Payment object

# get settings from the Settings datatype
setting = get_setting("EXAMPLE_SETTINGS")

# get Payment object for transaction
payment = params["payment"]

# print to the notification log
Tenant.notify(message: "Payment #{payment.to_hash}", type: :info)

# Example of how to use a template as the response
{ 
  "action" => "render",
  "template" => "Example::Example Payment Page",
  "data" => {
    "referenceId" => payment["referenceId"],
    "transId" => SecureRandom.uuid(),
    "amount" => payment["transactionData"]["amount"]
   }
}

# Example of how to send data to user page
#{
#  "action" => "return",
#  "data" => page
#}


# Example of how to redirect to another site
#{
#  "action" => "redirect",
#  "redirect_to" => "http://urltohostedpayments"
# }
```

```
        a. Get Settings object - To retrieve the record from the Settings datatype, call the get_setting algorithm 
            and pass the name of the record
```

```ruby Get settings from the Settings datatype
setting = get_setting("EXAMPLE_SETTINGS")
```

```
        b. Get Payment object - retrieve the Payment object that is sent to the algorithm through 
             params
```

```ruby Get Payment object for transaction
payment = params["payment"]
```

```
        c. Response method - There are three options for a response method:
                  - Render a template
```

```ruby Render a template
{ 
  "action" => "render",
  "template" => "Example::Example Payment Page",
  "data" => {
    "referenceId" => payment["referenceId"],
    "transId" => SecureRandom.uuid(),
    "amount" => payment["transactionData"]["amount"]
   }
}
```

```
                - Send data to user page
```

```ruby Send data to user page
{
  "action" => "return",
  "data" => page
}
```

```
                - Redirect to another site
```

```ruby Redirect to another site
{
 "action" => "redirect",
 "redirect_to" => "http://urltohostedpayments"
}
```

3. Save the algorithm once completed.

### process\_payment

1. Create an algorithm called process\_payment with a parameter called params

![777](https://files.readme.io/4d6072a-ss3.png "ss3.png")

2. Write your code in the code block

![1167](https://files.readme.io/bece5be-ss4.png "ss4.png")

```ruby process_payment
Tenant.notify(message: "Process Example process_payment", type: :info)
Tenant.notify(message: "#{params}", type: :info)
begin
    # lookup the Payment entry by referenceId
  	payment = get_payment(params["referenceId"])
  
    if payment.nil?
      Cenit.fail("No Payment entry found for referenceId #{params["referenceId"]}")
    end
  
  	# Check if the current status is Pending Payment and apply payment if so
  	if payment["transactionStatus"].casecmp?("Pending Payment")
      
      # update the transactionStatus to either Approved or Failed
      status = ""
      if params["status"].eql?("APR")
        status = "Approved"
      end
      
      payment["transactionStatus"] = status
      
      # update the transactionId with the response from the payment provider
      payment["transactionId"] = params["transId"]
      
      # save any parameters sent back from the payment provider for later use
      payment["transactionData"] = params
      
      # add the payment provider transaction id as the transactionData.referenceId
      payment["transactionData"]["referenceId"] = params["transId"]
      
      # set the transactionData.payee to the info returned from the payment provider
      payment["transactionData"]["payee"] = "#{params["payeeFirstname"]} #{params["payeeLastname"]}"
      
      # set the transactionData.payee to the info returned from the payment provider
      payment["transactionData"]["payeeAddress"] = "#{params["payeeAddress"]}"
      
      # optionally build a URL to the paymnet providers detail page
      payment["transactionData"]["detailURL"] = "https://paymentprovider/detail"

      # set the transactionData.type 
      payment["transactionData"]["type"] = "Credit Card"
      # payment["transactionData"]["type"] = "Check"
     
      # optionally set the convenience or service fee charged by the payment provder
      #payment["transactionData"]["convFee"] = 0

      # save the payment
      payment = save_payment(payment)

      # return a success message as required by the payment provider
      #{
      #  "action"=> "return",
      #  "code"=> "200",
      #  "data"=> "Payment completed successfully"
      #}
      
      # redirect to transactionData.redirectURL which 
      # will exist on the Payment after calling save_payment with the
      # transactionStatus of Approved or Failed
 	  {
        "action" => "redirect",
        "redirect_to" => payment["transactionData"]["redirectURL"]
      }
    elsif payment["transactionStatus"].casecmp?("Approved") || payment["transactionStatus"].casecmp?("Failed")
      {
        "action" => "redirect",
        "redirect_to" => payment["transactionData"]["redirectURL"]
      }
    else
      {
        "action"=> "return",
        "code"=> "500",
        "data"=> "Payment in invalid state of #{payment["transactionStatus"]} for further actions"
      }
    end
rescue StandardError => ex
  code = ex.message =~ /^\[(\d+)\]/ ? $1.to_i : 500
  message = ex.message.gsub(/^\[(\d+)\] - /, '')
 
  {
      "action"=> "return",
      "code"=> code,
      "data"=> message
  }
end
```

```
        a. Lookup Payment record

                 - Begin the algorithm by looking up the Payment record by the referenceId
```

```ruby Lookup Payment entry by referenceId
payment = get_payment(params["referenceId"])
```

```
                 - Add an if statement to check if the response from get_payment is null and to end the execution if it is
```

```ruby Null Payment check
if payment.nil?
   Cenit.fail("No Payment entry found for referenceId #{params["referenceId"]}")
end
```

```
        b. Check if current status is Pending

                 - Check if the current status is Pending Payment and apply payment if so
```

```ruby Check if current status is Pending
if payment["transactionStatus"].casecmp?("Pending Payment")
```

```
                 - Update the transactionStatus to either Approved or Failed
```

```ruby Update transaction status
status = ""
if params["status"].eql?("APR")
  status = "Approved"
end

payment["transactionStatus"] = status
```

```
                 - Update the transactionId with the response from the payment provider
```

```ruby Update transactionId
payment["transactionId"] = params["transId"]
```

```
                 - Save any parameters sent back from the payment provider for later use
```

```ruby Save parameters sent back from payment provider
payment["transactionData"] = params
```

```
                 - Add the payment provider transaction id as the transactionData.referenceId
```

```ruby Add payment provider transactionId
payment["transactionData"]["referenceId"] = params["transId"]
```

```
                 - Set the transactionData.payee to the info returned from the payment provider
```

```ruby Set transactionData.payee
payment["transactionData"]["payee"] = "#{params["payeeFirstname"]} #{params["payeeLastname"]}"
```

```
                 - Set the transactionData.payeeAddress to the info returned from the payment provider
```

```ruby Set transactionData.payeeAddress
payment["transactionData"]["payeeAddress"] = "#{params["payeeAddress"]}"
```

```
                 - Optionally build a URL to the payment providers detail page
```

```ruby Optional
payment["transactionData"]["detailURL"] = "https://paymentprovider/detail"
```

```
                 - Set the transactionData.type 
```

```ruby Set transactionData.type
payment["transactionData"]["type"] = "Credit Card"

OR

payment["transactionData"]["type"] = "Check"
```

```
                 - Optionally set the convenience or service fee charged by the payment provder
```

```ruby Optional
payment["transactionData"]["convFee"] = 0
```

```
                 - Save the payment
```

```ruby Save payment
payment = save_payment(payment)
```

```
        c. Redirect to transactionData.redirectURL which will exist on the Payment after calling save_payment with the transactionStatus of Approved or Failed
```

```ruby Redirect
{
  "action" => "redirect",
  "redirect_to" => payment["transactionData"]["redirectURL"]
}
elsif payment["transactionStatus"].casecmp?("Approved") || payment["transactionStatus"].casecmp?("Failed")
{
  "action" => "redirect",
  "redirect_to" => payment["transactionData"]["redirectURL"]
}
else
{
  "action"=> "return",
  "code"=> "500",
  "data"=> "Payment in invalid state of #{payment["transactionStatus"]} for further actions"
}
end
```

```
        d. Ending code
```

```ruby End
rescue StandardError => ex
  code = ex.message =~ /^\[(\d+)\]/ ? $1.to_i : 500
  message = ex.message.gsub(/^\[(\d+)\] - /, '')
 
  {
      "action"=> "return",
      "code"=> code,
      "data"=> message
  }
end
```

### payment\_confirmation

1. Create an algorithm called payment\_confirmation with a parameter called params

![698](https://files.readme.io/37884ac-ss5.png "ss5.png")

2. Write your code in the code block

![1181](https://files.readme.io/4e69676-ss6.png "ss6.png")

```ruby
Tenant.notify(message: "Process Example payment_confirmation", type: :info)
Tenant.notify(message: "#{params}", type: :info)
begin
    # lookup the Payment entry by referenceId
  	payment = get_payment(params["referenceId"])
  
    if payment.nil?
      Cenit.fail("No Payment entry found for referenceId #{params["referenceId"]}")
    end
  
 	 # Check if the Payment is Approved or Failed and redirect
  	if payment["transactionStatus"].casecmp?("Approved") || payment["transactionStatus"].casecmp?("Failed")
      
 	  {
        "action" => "redirect",
        "redirect_to" => payment["transactionData"]["redirectURL"]
      }
    else
      {
        "action"=> "return",
        "code"=> "500",
        "data"=> "Payment in invalid state of #{payment["transactionStatus"]} for further actions"
      }
    end
rescue StandardError => ex
  code = ex.message =~ /^\[(\d+)\]/ ? $1.to_i : 500
  message = ex.message.gsub(/^\[(\d+)\] - /, '')
 
  {
      "action"=> "return",
      "code"=> code,
      "data"=> message
  }
end
```

```
        a. Similarly to the process_payment algorithm, begin payment_confirmation by looking up the Payment record by referenceId and check if it is null
```

```ruby Get & Check Payment
payment = get_payment(params["referenceId"])
  
  if payment.nil?
  Cenit.fail("No Payment entry found for referenceId #{params["referenceId"]}")
  end
```

```
       b. Check if the Payment status is Approved or Failed and redirect
```

```ruby Check status
if payment["transactionStatus"].casecmp?("Approved") || payment["transactionStatus"].casecmp?("Failed")

  {
    "action" => "redirect",
    "redirect_to" => payment["transactionData"]["redirectURL"]
  }
else
  {
    "action"=> "return",
    "code"=> "500",
    "data"=> "Payment in invalid state of #{payment["transactionStatus"]} for further actions"
  }
end
```

```
        c. End code
```

```ruby End
rescue StandardError => ex
  code = ex.message =~ /^\[(\d+)\]/ ? $1.to_i : 500
  message = ex.message.gsub(/^\[(\d+)\] - /, '')
 
  {
      "action"=> "return",
      "code"=> code,
      "data"=> message
  }
end
```

3. Save algorithm once completed.

### cancel\_payment

1. Create an algorithm called cancel\_payment with a parameter called params

![1920](https://files.readme.io/350a1c1-ss7.png "ss7.png")

2. Write your code in the code block

![1100](https://files.readme.io/d35c062-ss8.png "ss8.png")

```ruby cancel_payment
Tenant.notify(message: "Process Example cancel_payment", type: :info)
Tenant.notify(message: "#{params}", type: :info)

payment = get_payment(params["referenceId"])

if payment.nil?
  Cenit.fail("No Payment entry found for referenceId #{params["referenceId"]}")
end

payment["transactionStatus"] = "Cancel"
payment["transactionId"] = params["SECURETOKENID"]
payment["transactionData"] = params
payment = save_payment(payment)
  
if payment["transactionData"]["redirectURL"]
  {
    "action" => "redirect",
    "redirect_to" => payment["transactionData"]["redirectURL"]
  }
else
  {
        "action"=> "return",
        "code"=> "200",
    	"data"=> "Payment cancelled"
    }
end
```

```
        a. Lookup Payment record by reference Id and check if it is null or not
```

```ruby Lookup Payment
payment = get_payment(params["referenceId"])

if payment.nil?
  Cenit.fail("No Payment entry found for referenceId #{params["referenceId"]}")
end
```

```
        b. Set Payment record details and save Payment
```

```ruby Set details
payment["transactionStatus"] = "Cancel"
payment["transactionId"] = params["SECURETOKENID"]
payment["transactionData"] = params
payment = save_payment(payment)
```

```
        c. Set redirect
```

```ruby Redirect
if payment["transactionData"]["redirectURL"]
  {
    "action" => "redirect",
    "redirect_to" => payment["transactionData"]["redirectURL"]
  }
else
  {
        "action"=> "return",
        "code"=> "200",
    	"data"=> "Payment cancelled"
    }
end
```

3. Save algorithm

## Using the SDK

1. Go to laserfiche.velosimo.com and login
2. Open your forms Process Designer Diagram and show details
3. Select start in the diagram and the On Event Completion tab

![1920](https://files.readme.io/5ab25b3-ss9.png "ss9.png")

4. Set redirect to website and the URL to the initialize\_payment algorithm on the Velosimo site.

Example - <https://dev.velosimo.io/app/payment_adapter/laserfiche/example/initialize_payment>

payment\_adapter = Velosimo tenant\
example = Velosimo namespace\
initialize\_payment = Algorithm

![574](https://files.readme.io/4006e1d-ss10.png "ss10.png")

5. Submit a new form. Once submitted you will be redirected to an environment where you can test each algorithm created.

![924](https://files.readme.io/0eacda9-ss11.png "ss11.png")

```
        a. Click Simulate Approved Postback
```

![1692](https://files.readme.io/1386d6c-Screen_Shot_2022-04-27_at_2.03.26_PM.png "Screen Shot 2022-04-27 at 2.03.26 PM.png")