Module: Safecharge
- Defined in:
- lib/safecharge.rb,
lib/safecharge/dmn.rb,
lib/safecharge/request.rb,
lib/safecharge/version.rb,
lib/safecharge/response.rb,
lib/safecharge/constants.rb,
lib/safecharge/sg_request.rb,
lib/safecharge/wc_request.rb
Overview
The Safecharge PPP system provides a simple means for merchants to integrate credit card payments into their site, without worrying about having to capture their customers’ credit card details.
It works like this:
Step 1) Your website provides a way for customers to choose the items they wish to buy,
say via a shopping basket, or similar.
Collate an array of items with the following information
items = [
{
'name' => 'bat',
'number' => 'sku54321',
'amount' => 25,
'quantity' => 1
},
{
'name' => 'ball',
'number' => 'sku12345',
'amount' => 15,
'quantity' => 2
}
]
and insert that items array into an array of params like so
params = {
'total_amount' => 55,
'currency' => 'USD',
'items' => items
}
Note you must supply the following environment variables for this API to work.
SAFECHARGE_SECRET_KEY, SAFECHARGE_MERCHANT_ID, SAFECHARGE_MERCHANT_SITE_ID
These will have been provided to you by Safecharge.
Step 2) You offer a ‘checkout’ button that links to the following url.
url = Safecharge.request_url(params)
Step 3) The Safecharge system will redirect the user to the Public Payment Page
and there they will enter in their credit card and other payment details as needed.
When the user confirms their payment, the Safecharge system authenticates it and
redirects the user to either a 'success', 'failure' or 'back' page. The back page
is used if the user suspends the payment processing by clicking on their browser's
back button.
Whichever page is returned, it will incude parameters from the server
which can be decoded into a valid Response object by your server.
response = Safecharge.parse_response
TODO: finish these docs
Defined Under Namespace
Classes: Constants, DMN, InternalException, Request, Response, SafechargeError, SgRequest, ValidationException, WcRequest
Constant Summary collapse
- VERSION =
"0.1.7"
Class Method Summary collapse
-
.request_url(params = {}, mode = 'test') ⇒ Object
module level method to get the redirection URL given some params.
-
.sg_request_url(params = {}, mode = 'test') ⇒ Object
module level method to get the redirection URL given some params.
-
.wc_request_url(params = {}, mode = 'test') ⇒ Object
module level method to get the redirection URL given some params.
Class Method Details
.request_url(params = {}, mode = 'test') ⇒ Object
module level method to get the redirection URL given some params. you must explicitly set the mode to ‘live’ to generate the production URL.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/safecharge.rb', line 81 def self.request_url(params = {}, mode = 'test') result = nil url = '' case mode when 'test' url = Safecharge::Constants::SERVER_TEST when 'live' url = Safecharge::Constants::SERVER_LIVE else raise ArgumentError, "Invalid request mode #{mode}" end begin request = Safecharge::Request.new(url, params) return request.full_url rescue InternalException => e puts "Caught Internal Exception: #{e.}" puts e.backtrace raise RuntimeError, "Internal server error. Please try again later." rescue ValidationException => e puts "Caught Validation Exception: #{e.}" puts e.backtrace raise RuntimeError, "Validation error: #{e.} Fix your data and retry." rescue SafechargeError => e puts "Caught General Safecharge Error: #{e.}" puts e.backtrace raise RuntimeError, "Undocumented Internal error: #{e.}" end end |
.sg_request_url(params = {}, mode = 'test') ⇒ Object
module level method to get the redirection URL given some params. you must explicitly set the mode to ‘live’ to generate the production URL.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/safecharge.rb', line 151 def self.sg_request_url(params = {}, mode = 'test') result = nil url = '' case mode when 'test' url = Safecharge::Constants::SG_SERVER_TEST when 'live' url = Safecharge::Constants::SG_SERVER_LIVE else raise ArgumentError, "Invalid request mode #{mode}" end begin request = Safecharge::SgRequest.new(url, params) return request.full_url rescue InternalException => e puts "Caught Internal Exception: #{e.}" puts e.backtrace raise RuntimeError, "Internal server error. Please try again later." rescue ValidationException => e puts "Caught Validation Exception: #{e.}" puts e.backtrace raise RuntimeError, "Validation error: #{e.} Fix your data and retry." rescue SafechargeError => e puts "Caught General Safecharge Error: #{e.}" puts e.backtrace raise RuntimeError, "Undocumented Internal error: #{e.}" end end |
.wc_request_url(params = {}, mode = 'test') ⇒ Object
module level method to get the redirection URL given some params. you must explicitly set the mode to ‘live’ to generate the production URL.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/safecharge.rb', line 116 def self.wc_request_url(params = {}, mode = 'test') result = nil url = '' case mode when 'test' url = Safecharge::Constants::SERVER_TEST when 'live' url = Safecharge::Constants::SERVER_LIVE else raise ArgumentError, "Invalid request mode #{mode}" end begin request = Safecharge::WcRequest.new(url, params) return request.full_url rescue InternalException => e puts "Caught Internal Exception: #{e.}" puts e.backtrace raise RuntimeError, "Internal server error. Please try again later." rescue ValidationException => e puts "Caught Validation Exception: #{e.}" puts e.backtrace raise RuntimeError, "Validation error: #{e.} Fix your data and retry." rescue SafechargeError => e puts "Caught General Safecharge Error: #{e.}" puts e.backtrace raise RuntimeError, "Undocumented Internal error: #{e.}" end end |