├── LICENSE.md ├── README.md ├── api ├── blesta_api.php └── blesta_response.php └── components └── gateways ├── merchant └── merchant_demo_cc │ ├── language │ └── en_us │ │ └── merchant_demo_cc.php │ ├── merchant_demo_cc.php │ └── views │ └── default │ ├── images │ └── logo.png │ └── settings.pdt └── nonmerchant └── nonmerchant_demo ├── language └── en_us │ └── nonmerchant_demo.php ├── nonmerchant_demo.php └── views └── default ├── images └── logo.png ├── process.pdt └── settings.pdt /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License # 2 | 3 | Copyright (c) 2010-2013 [Phillips Data, Inc.](https://github.com/phillipsdata) 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to deal in the Software without 8 | restriction, including without limitation the rights to use, 9 | copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the 11 | Software is furnished to do so, subject to the following 12 | conditions: 13 | 14 | The above copyright notice and this permission notice shall be 15 | included in all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 19 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 21 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 22 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 23 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 | OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Blesta SDK # 2 | 3 | This development kit includes the following: 4 | 5 | * An API processor to make interfacing with the Blesta API super simple 6 | * Sample Merchant and Non-merchant Gateway implementations 7 | 8 | ## Requirements ## 9 | 10 | * PHP 5.2.0 or greater 11 | * Blesta 3.0.0 or greater 12 | 13 | ## Using the API ## 14 | 15 | Documentation on the API can be found in the [API](http://docs.blesta.com/display/dev/API) section of the [Developer Manual](http://docs.blesta.com/display/dev/) 16 | 17 | ```php 18 | get("users", "get", array('user_id' => 1)); 28 | 29 | print_r($response->response()); 30 | print_r($response->errors()); 31 | 32 | ?> 33 | ``` 34 | 35 | Plugin models are accessible as well: 36 | 37 | ```php 38 | post("support_manager.support_manager_tickets", "close", array('ticket_id' => 1)); 48 | 49 | print_r($response->response()); 50 | print_r($response->errors()); 51 | 52 | ?> 53 | ``` 54 | 55 | ## Working with Gateways ## 56 | 57 | Documentation on gateways can be found in the [Payment Gateways](http://docs.blesta.com/display/dev/Payment+Gateways) section of the [Developer Manual](http://docs.blesta.com/display/dev/) 58 | 59 | Included in this SKD are two example gateways: 60 | 61 | * Merchant Credit Card Gateway _/components/gateways/merchant/merchant_demo_cc/_ 62 | * Non-merchant Gateway _/components/gateways/nonmerchant/nonmerchant_demo/_ -------------------------------------------------------------------------------- /api/blesta_api.php: -------------------------------------------------------------------------------- 1 | url = $url; 39 | $this->user = $user; 40 | $this->key = $key; 41 | } 42 | 43 | /** 44 | * Submit an API request via GET 45 | * 46 | * @param string $model The model to request (e.g. users) 47 | * @param string $method The method to request (e.g. add) 48 | * @param array $args An array of arguments to pass to the method 49 | * @return BlestaResponse The response object 50 | */ 51 | public function get($model, $method, array $args = array()) { 52 | return $this->submit($model, $method, $args, "GET"); 53 | } 54 | 55 | /** 56 | * Submit an API request via POST 57 | * 58 | * @param string $model The model to request (e.g. users) 59 | * @param string $method The method to request (e.g. add) 60 | * @param array $args An array of arguments to pass to the method 61 | * @return BlestaResponse The response object 62 | */ 63 | public function post($model, $method, array $args = array()) { 64 | return $this->submit($model, $method, $args, "POST"); 65 | } 66 | 67 | /** 68 | * Submit an API request via PUT 69 | * 70 | * @param string $model The model to request (e.g. users) 71 | * @param string $method The method to request (e.g. add) 72 | * @param array $args An array of arguments to pass to the method 73 | * @return BlestaResponse The response object 74 | */ 75 | public function put($model, $method, array $args = array()) { 76 | return $this->submit($model, $method, $args, "PUT"); 77 | } 78 | 79 | /** 80 | * Submit an API request via DELETE 81 | * 82 | * @param string $model The model to request (e.g. users) 83 | * @param string $method The method to request (e.g. add) 84 | * @param array $args An array of arguments to pass to the method 85 | * @return BlestaResponse The response object 86 | */ 87 | public function delete($model, $method, array $args = array()) { 88 | return $this->submit($model, $method, $args, "DELETE"); 89 | } 90 | 91 | /** 92 | * Submits a request to the API 93 | * 94 | * @param string $uri The URI to submit to 95 | * @param array $args An array of key/value pair arguments to submit to the given API command 96 | * @return BlestaResponse The response object 97 | */ 98 | private function submit($model, $method, array $args = array(), $action = "POST") { 99 | 100 | $url = $this->url . $model . "/" . $method . "." . self::$format; 101 | 102 | $this->last_request = array( 103 | 'url' => $url, 104 | 'args' => $args 105 | ); 106 | 107 | if ($action == "GET") { 108 | $url .= "?" . http_build_query($args); 109 | $args = null; 110 | } 111 | 112 | $ch = curl_init(); 113 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 114 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $action); 115 | curl_setopt($ch, CURLOPT_URL, $url); 116 | if ($args) { 117 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($args)); 118 | } 119 | 120 | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 121 | curl_setopt($ch, CURLOPT_USERPWD, $this->user . ":" . $this->key); 122 | //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 123 | //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 124 | $response = curl_exec($ch); 125 | $response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 126 | curl_close($ch); 127 | 128 | return new BlestaResponse($response, $response_code); 129 | } 130 | 131 | /** 132 | * Returns the details of the last request made 133 | * 134 | * @return array An array containg: 135 | * - url The URL of the last request 136 | * - args The paramters passed to the URL 137 | */ 138 | public function lastRequest() { 139 | return $this->last_request; 140 | } 141 | } 142 | ?> -------------------------------------------------------------------------------- /api/blesta_response.php: -------------------------------------------------------------------------------- 1 | raw = $response; 27 | $this->response_code = $response_code; 28 | } 29 | 30 | /** 31 | * Returns the response from the request 32 | * 33 | * @return mixed A stdClass object representing the response returned from the request, null if no response returned 34 | */ 35 | public function response() { 36 | $response = $this->formatResponse(); 37 | if (isset($response->response)) 38 | return $response->response; 39 | return null; 40 | } 41 | 42 | /** 43 | * Returns the HTTP response code 44 | * 45 | * @return int The HTTP response code for the request 46 | */ 47 | public function responseCode() { 48 | return $this->response_code; 49 | } 50 | 51 | /** 52 | * Returns the raw response 53 | * 54 | * @return string The raw response 55 | */ 56 | public function raw() { 57 | return $this->raw; 58 | } 59 | 60 | /** 61 | * Returns all errors contained in the response 62 | * 63 | * @return stdClass A stdClass object representing the errors in the response, false if invalid response 64 | */ 65 | public function errors() { 66 | if ($this->response_code != 200) { 67 | $response = $this->formatResponse(); 68 | 69 | if (isset($response->errors)) 70 | return $response->errors; 71 | else { 72 | $error = new stdClass(); 73 | $error->error = $response; 74 | return $error; 75 | } 76 | } 77 | return false; 78 | } 79 | 80 | /** 81 | * Formats the raw response into a stdClass object 82 | * 83 | * @return stdClass A stdClass object representing the resposne 84 | */ 85 | private function formatResponse() { 86 | return json_decode($this->raw); 87 | } 88 | } 89 | ?> -------------------------------------------------------------------------------- /components/gateways/merchant/merchant_demo_cc/language/en_us/merchant_demo_cc.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /components/gateways/merchant/merchant_demo_cc/merchant_demo_cc.php: -------------------------------------------------------------------------------- 1 | "Phillips Data, Inc.", 'url' => "http://www.blesta.com")); 20 | /** 21 | * @var array An array of meta data for this gateway 22 | */ 23 | private $meta; 24 | 25 | /** 26 | * Construct a new merchant gateway 27 | */ 28 | public function __construct() { 29 | // Load components required by this module 30 | Loader::loadComponents($this, array("Input")); 31 | 32 | // Load the language required by this module 33 | Language::loadLang("merchant_demo_cc", null, dirname(__FILE__) . DS . "language" . DS); 34 | } 35 | 36 | /** 37 | * Returns the name of this gateway 38 | * 39 | * @return string The common name of this gateway 40 | */ 41 | public function getName() { 42 | return Language::_("MerchantDemoCc.name", true); 43 | } 44 | 45 | /** 46 | * Returns the version of this gateway 47 | * 48 | * @return string The current version of this gateway 49 | */ 50 | public function getVersion() { 51 | return self::$version; 52 | } 53 | 54 | /** 55 | * Returns the name and URL for the authors of this gateway 56 | * 57 | * @return array The name and URL of the authors of this gateway 58 | */ 59 | public function getAuthors() { 60 | return self::$authors; 61 | } 62 | 63 | /** 64 | * Return all currencies supported by this gateway 65 | * 66 | * @return array A numerically indexed array containing all currency codes (ISO 4217 format) this gateway supports 67 | */ 68 | public function getCurrencies() { 69 | return array("USD", "GBP", "EUR"); 70 | } 71 | 72 | /** 73 | * Sets the currency code to be used for all subsequent payments 74 | * 75 | * @param string $currency The ISO 4217 currency code to be used for subsequent payments 76 | */ 77 | public function setCurrency($currency) { 78 | $this->currency = $currency; 79 | } 80 | 81 | /** 82 | * Create and return the view content required to modify the settings of this gateway 83 | * 84 | * @param array $meta An array of meta (settings) data belonging to this gateway 85 | * @return string HTML content containing the fields to update the meta data for this gateway 86 | */ 87 | public function getSettings(array $meta=null) { 88 | $this->view = $this->makeView("settings", "default", str_replace(ROOTWEBDIR, "", dirname(__FILE__) . DS)); 89 | 90 | // Load the helpers required for this view 91 | Loader::loadHelpers($this, array("Form", "Html")); 92 | 93 | $this->view->set("meta", $meta); 94 | 95 | return $this->view->fetch(); 96 | } 97 | 98 | /** 99 | * Validates the given meta (settings) data to be updated for this gateway 100 | * 101 | * @param array $meta An array of meta (settings) data to be updated for this gateway 102 | * @return array The meta data to be updated in the database for this gateway, or reset into the form on failure 103 | */ 104 | public function editSettings(array $meta) { 105 | // Verify meta data is valid 106 | $rules = array( 107 | 'key'=>array( 108 | 'valid'=>array( 109 | 'rule'=>array("betweenLength", 16, 16), 110 | 'message'=>Language::_("MerchantDemoCc.!error.key.valid", true) 111 | ) 112 | ) 113 | 114 | # 115 | # TODO: Do error checking on any other fields that require it 116 | # 117 | 118 | ); 119 | 120 | $this->Input->setRules($rules); 121 | 122 | // Validate the given meta data to ensure it meets the requirements 123 | $this->Input->validates($meta); 124 | // Return the meta data, no changes required regardless of success or failure for this gateway 125 | return $meta; 126 | } 127 | 128 | /** 129 | * Returns an array of all fields to encrypt when storing in the database 130 | * 131 | * @return array An array of the field names to encrypt when storing in the database 132 | */ 133 | public function encryptableFields() { 134 | return array("key"); 135 | } 136 | 137 | /** 138 | * Sets the meta data for this particular gateway 139 | * 140 | * @param array $meta An array of meta data to set for this gateway 141 | */ 142 | public function setMeta(array $meta=null) { 143 | $this->meta = $meta; 144 | } 145 | 146 | /** 147 | * Used to determine whether this gateway can be configured for autodebiting accounts 148 | * 149 | * @return boolean True if the customer must be present (e.g. in the case of credit card customer must enter security code), false otherwise 150 | */ 151 | public function requiresCustomerPresent() { 152 | return false; 153 | } 154 | 155 | 156 | /** 157 | * Charge a credit card 158 | * 159 | * @param array $card_info An array of credit card info including: 160 | * - first_name The first name on the card 161 | * - last_name The last name on the card 162 | * - card_number The card number 163 | * - card_exp The card expiration date in yyyymm format 164 | * - card_security_code The 3 or 4 digit security code of the card (if available) 165 | * - type The credit card type 166 | * - address1 The address 1 line of the card holder 167 | * - address2 The address 2 line of the card holder 168 | * - city The city of the card holder 169 | * - state An array of state info including: 170 | * - code The 2 or 3-character state code 171 | * - name The local name of the state 172 | * - country An array of country info including: 173 | * - alpha2 The 2-character country code 174 | * - alpha3 The 3-character country code 175 | * - name The english name of the country 176 | * - alt_name The local name of the country 177 | * - zip The zip/postal code of the card holder 178 | * @param float $amount The amount to charge this card 179 | * @param array $invoice_amounts An array of invoices, each containing: 180 | * - id The ID of the invoice being processed 181 | * - amount The amount being processed for this invoice (which is included in $amount) 182 | * @return array An array of transaction data including: 183 | * - status The status of the transaction (approved, declined, void, pending, error, refunded, returned) 184 | * - reference_id The reference ID for gateway-only use with this transaction (optional) 185 | * - transaction_id The ID returned by the remote gateway to identify this transaction 186 | * - message The message to be displayed in the interface in addition to the standard message for this transaction status (optional) 187 | */ 188 | public function processCc(array $card_info, $amount, array $invoice_amounts=null) { 189 | 190 | # 191 | # TODO: Submit the request to the remote gateway processor, log the 192 | # submission and response, then return the transaction data 193 | # 194 | 195 | return array( 196 | 'status' => "approved", 197 | 'reference_id' => null, 198 | 'transaction_id' => substr(md5(microtime()), -16) 199 | ); 200 | } 201 | 202 | /** 203 | * Authorize a credit card 204 | * 205 | * @param array $card_info An array of credit card info including: 206 | * - first_name The first name on the card 207 | * - last_name The last name on the card 208 | * - card_number The card number 209 | * - card_exp The card expiration date in yyyymm format 210 | * - card_security_code The 3 or 4 digit security code of the card (if available) 211 | * - type The credit card type 212 | * - address1 The address 1 line of the card holder 213 | * - address2 The address 2 line of the card holder 214 | * - city The city of the card holder 215 | * - state An array of state info including: 216 | * - code The 2 or 3-character state code 217 | * - name The local name of the country 218 | * - country An array of country info including: 219 | * - alpha2 The 2-character country code 220 | * - alpha3 The 3-cahracter country code 221 | * - name The english name of the country 222 | * - alt_name The local name of the country 223 | * - zip The zip/postal code of the card holder 224 | * @param float $amount The amount to charge this card 225 | * @param array $invoice_amounts An array of invoices, each containing: 226 | * - id The ID of the invoice being processed 227 | * - amount The amount being processed for this invoice (which is included in $amount) 228 | * @return array An array of transaction data including: 229 | * - status The status of the transaction (approved, declined, void, pending, error, refunded, returned) 230 | * - reference_id The reference ID for gateway-only use with this transaction (optional) 231 | * - transaction_id The ID returned by the remote gateway to identify this transaction 232 | * - message The message to be displayed in the interface in addition to the standard message for this transaction status (optional) 233 | */ 234 | public function authorizeCc(array $card_info, $amount, array $invoice_amounts=null) { 235 | // Gateway does not support this action 236 | $this->Input->setErrors($this->getCommonError("unsupported")); 237 | } 238 | 239 | /** 240 | * Capture the funds of a previously authorized credit card 241 | * 242 | * @param string $reference_id The reference ID for the previously authorized transaction 243 | * @param string $transaction_id The transaction ID for the previously authorized transaction 244 | * @param float $amount The amount to capture on this card 245 | * @param array $invoice_amounts An array of invoices, each containing: 246 | * - id The ID of the invoice being processed 247 | * - amount The amount being processed for this invoice (which is included in $amount) 248 | * @return array An array of transaction data including: 249 | * - status The status of the transaction (approved, declined, void, pending, error, refunded, returned) 250 | * - reference_id The reference ID for gateway-only use with this transaction (optional) 251 | * - transaction_id The ID returned by the remote gateway to identify this transaction 252 | * - message The message to be displayed in the interface in addition to the standard message for this transaction status (optional) 253 | */ 254 | public function captureCc($reference_id, $transaction_id, $amount, array $invoice_amounts=null) { 255 | // Gateway does not support this action 256 | $this->Input->setErrors($this->getCommonError("unsupported")); 257 | } 258 | 259 | /** 260 | * Void a credit card charge 261 | * 262 | * @param string $reference_id The reference ID for the previously authorized transaction 263 | * @param string $transaction_id The transaction ID for the previously authorized transaction 264 | * @return array An array of transaction data including: 265 | * - status The status of the transaction (approved, declined, void, pending, error, refunded, returned) 266 | * - reference_id The reference ID for gateway-only use with this transaction (optional) 267 | * - transaction_id The ID returned by the remote gateway to identify this transaction 268 | * - message The message to be displayed in the interface in addition to the standard message for this transaction status (optional) 269 | */ 270 | public function voidCc($reference_id, $transaction_id) { 271 | // Gateway does not support this action 272 | $this->Input->setErrors($this->getCommonError("unsupported")); 273 | } 274 | 275 | /** 276 | * Refund a credit card charge 277 | * 278 | * @param string $reference_id The reference ID for the previously authorized transaction 279 | * @param string $transaction_id The transaction ID for the previously authorized transaction 280 | * @param float $amount The amount to refund this card 281 | * @return array An array of transaction data including: 282 | * - status The status of the transaction (approved, declined, void, pending, error, refunded, returned) 283 | * - reference_id The reference ID for gateway-only use with this transaction (optional) 284 | * - transaction_id The ID returned by the remote gateway to identify this transaction 285 | * - message The message to be displayed in the interface in addition to the standard message for this transaction status (optional) 286 | */ 287 | public function refundCc($reference_id, $transaction_id, $amount) { 288 | // Gateway does not support this action 289 | $this->Input->setErrors($this->getCommonError("unsupported")); 290 | } 291 | } 292 | ?> -------------------------------------------------------------------------------- /components/gateways/merchant/merchant_demo_cc/views/default/images/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phillipsdata/blesta_sdk/052760da4d9468c2d49edf1482ec97d35c85f162/components/gateways/merchant/merchant_demo_cc/views/default/images/logo.png -------------------------------------------------------------------------------- /components/gateways/merchant/merchant_demo_cc/views/default/settings.pdt: -------------------------------------------------------------------------------- 1 | 2 |