PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/core/MultiSafepay/MultiSafepayTransaction.class.php

https://bitbucket.org/multimedium/bob361
PHP | 425 lines | 116 code | 40 blank | 269 comment | 7 complexity | 0e71083ff04d99f1d3cee54dcb3fab50 MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /**
  3. * M_MultiSafepayTransaction
  4. *
  5. * Contains general information about the payment transaction. Typically,
  6. * one {@link M_MultiSafepay} should contain a single
  7. * {@link M_MultiSafepayTransaction}, providing it with all the necessary
  8. * base information, like the amount the client has to pay, the currency used
  9. * and the transaction's ID.
  10. *
  11. * @package Core
  12. */
  13. class M_MultiSafepayTransaction extends M_Object {
  14. /* -- CONSTANTS -- */
  15. /**
  16. * Currency: Euro
  17. *
  18. * @var string
  19. */
  20. const CURRENCY_EUR = 'EUR';
  21. /**
  22. * Currency: US Dollar
  23. *
  24. * @var string
  25. */
  26. const CURRENCY_USD = 'USD';
  27. /**
  28. * Currency: Great British Pounds
  29. *
  30. * @var string
  31. */
  32. const CURRENCY_GBP = 'GBP';
  33. /* -- PROPERTIES -- */
  34. /**
  35. * Id
  36. *
  37. * The unique id of the transaction
  38. *
  39. * NOTE: this property is mandatory for correct transaction functionality
  40. *
  41. * @see M_MultiSafepayTransaction::setId()
  42. * @see M_MultiSafepayTransaction::getId()
  43. * @access private
  44. * @var string
  45. */
  46. private $_id;
  47. /**
  48. * Currency
  49. *
  50. * The active currency for the transaction
  51. *
  52. * NOTE: this property is mandatory for correct transaction functionality
  53. *
  54. * @see M_MultiSafepayTransaction::setCurrency()
  55. * @see M_MultiSafepayTransaction::getCurrency()
  56. * @access private
  57. * @var string
  58. */
  59. private $_currency;
  60. /**
  61. * Amount
  62. *
  63. * The total amount of the current currency the customer has to pay
  64. * with the transaction
  65. *
  66. * NOTE: this property is mandatory for correct transaction functionality
  67. *
  68. * @see M_MultiSafepayTransaction::setAmount()
  69. * @see M_MultiSafepayTransaction::getAmount()
  70. * @access private
  71. * @var float
  72. */
  73. private $_amount;
  74. /**
  75. * Description
  76. *
  77. * A short description of the transaction, e.g. "Order #xxxxxx"
  78. *
  79. * NOTE: this property is mandatory for correct transaction functionality
  80. *
  81. * @see M_MultiSafepayTransaction::setDescription()
  82. * @see M_MultiSafepayTransaction::getDescription()
  83. * @access private
  84. * @var string
  85. */
  86. private $_description;
  87. /**
  88. * Items
  89. *
  90. * Holds the collection of {@link M_MultiSafepayTransactionItem} objects
  91. * that are part of this transaction
  92. *
  93. * @see M_MultiSafepayTransaction::addItem()
  94. * @see M_MultiSafepayTransaction::getItems()
  95. * @access private
  96. * @var array
  97. */
  98. private $_items = array();
  99. /* -- CONSTRUCTOR -- */
  100. /**
  101. * Constructor
  102. *
  103. * @access public
  104. * @return M_MultiSafepayTransaction
  105. */
  106. public function __construct() {
  107. // Default the currency to EUR
  108. $this->setCurrency(self::CURRENCY_EUR);
  109. }
  110. /* -- SETTERS -- */
  111. /**
  112. * Set Id
  113. *
  114. * Allows for setting the unique id of this transaction. Typically, this
  115. * is a string also used to uniquely identify the order within the
  116. * webshop
  117. *
  118. * NOTE: setting this property is mandatory for correct transaction
  119. * functionality
  120. *
  121. * @access public
  122. * @param string $id
  123. * @return M_MultiSafepayTransaction
  124. * Returns itself, for a fluent programming interface
  125. */
  126. public function setId($id) {
  127. $this->_id = (string) $id;
  128. return $this;
  129. }
  130. /**
  131. * Set Currency
  132. *
  133. * Allows for setting the currency type that is to be used for the
  134. * transaction. The supported currency types are defined as constants
  135. * within this class. For example:
  136. *
  137. * - {@link M_MultiSafepayTransaction::CURRENCY_EUR}
  138. * - {@link M_MultiSafepayTransaction::CURRENCY_USD}
  139. * - {@link M_MultiSafepayTransaction::CURRENCY_GBP}
  140. *
  141. * NOTE: setting this property is mandatory for correct transaction
  142. * functionality
  143. *
  144. * @access public
  145. * @param string $currency
  146. * @return M_MultiSafepayTransaction
  147. * Returns itself, for a fluent programming interface
  148. */
  149. public function setCurrency($currency) {
  150. // Cast to string before proceeding
  151. $currency = (string) $currency;
  152. // Make sure the currency type is supported
  153. if(! in_array($currency, array(self::CURRENCY_EUR, self::CURRENCY_USD, self::CURRENCY_GBP))) {
  154. throw new M_Exception(sprintf(
  155. 'Unsupported currency type %s given in %s::%s()',
  156. $currency,
  157. __CLASS__,
  158. __FUNCTION__
  159. ));
  160. }
  161. // Set the property
  162. $this->_currency = $currency;
  163. // And return self
  164. return $this;
  165. }
  166. /**
  167. * Set Amount
  168. *
  169. * Allows for setting the total amount the customer has to pay, specified
  170. * in the currently active currency. This amount is specified as a float
  171. * including decimals, e.g. €25,99 will be set as follows:
  172. *
  173. * <code>
  174. * $transaction = new M_MultiSafepayTransaction();
  175. * $transaction->setAmount(25.99);
  176. * </code>
  177. *
  178. * We clarify this here specifically, because in MultiSafepay itself,
  179. * the amount has to be declared in amount of cents, e.g. 2599 in the
  180. * above example. Converting the decimal number to a full integer
  181. * happens internally here though, so you do not have to specify the
  182. * amount in cents.
  183. *
  184. * NOTE: setting this property is mandatory for correct transaction
  185. * functionality
  186. *
  187. * @access public
  188. * @param float $amount
  189. * @return M_MultiSafepayTransaction
  190. * Returns itself, for a fluent programming interface
  191. */
  192. public function setAmount($amount) {
  193. $this->_amount = (float) $amount;
  194. return $this;
  195. }
  196. /**
  197. * Set Description
  198. *
  199. * Allows for setting a short description for the transaction. Usually
  200. * this description contains something like "Order #xxxxxx"
  201. *
  202. * NOTE: setting this property is mandatory for correct transaction
  203. * functionality
  204. *
  205. * @access public
  206. * @param string $description
  207. * @return M_MultiSafepayTransaction
  208. * Returns itself, for a fluent programming interface
  209. */
  210. public function setDescription($description) {
  211. $this->_description = (string) $description;
  212. return $this;
  213. }
  214. /**
  215. * Add Item
  216. *
  217. * Allows for adding a single {@link M_MultiSafepayTransactionItem} object
  218. * to the transaction. These items are added to the output, and displayed
  219. * on MultiSafepay payment page.
  220. *
  221. * @access public
  222. * @param M_MultiSafepayTransactionItem $item
  223. * @return M_MultiSafepayTransaction
  224. * Returns itself, for a fluent programming interface
  225. */
  226. public function addItem(M_MultiSafepayTransactionItem $item) {
  227. $this->_items[] = $item;
  228. return $this;
  229. }
  230. /* -- GETTERS -- */
  231. /**
  232. * Get Id
  233. *
  234. * @see M_MultiSafepayTransaction::setId()
  235. * @access public
  236. * @return string
  237. */
  238. public function getId() {
  239. return $this->_id;
  240. }
  241. /**
  242. * Get Currency
  243. *
  244. * @see M_MultiSafepayTransaction::setCurrency()
  245. * @access public
  246. * @return string
  247. */
  248. public function getCurrency() {
  249. return $this->_currency;
  250. }
  251. /**
  252. * Get Amount
  253. *
  254. * @see M_MultiSafepayTransaction::setAmount()
  255. * @access public
  256. * @return float
  257. */
  258. public function getAmount() {
  259. return $this->_amount;
  260. }
  261. /**
  262. * Get Amount For Processing
  263. *
  264. * In the XML sent to MultiSafepay, the amount for payment should be
  265. * specified in amount of cents, e.g. €25,99 needs to be specified as
  266. * an integer with value 2599. This value is also needed to create
  267. * the signature needed to make the call, typically created in
  268. * {@link M_MultiSafepay::getSignature()}. As a result, this method
  269. * will provide the set amount in the said format.
  270. *
  271. * @access public
  272. * @return int
  273. */
  274. public function getAmountForProcessing() {
  275. // Make sure the amount is set
  276. if(is_null($this->getAmount())) {
  277. throw new M_Exception(spintf(
  278. 'Cannot proceed in %s::%s(): no amount has been specified yet',
  279. __CLASS__,
  280. __FUNCTION__
  281. ));
  282. }
  283. // Return the correct amount for processing
  284. // IMPORTANT: note that we first case to a string, to avoid float
  285. // to int casting issues:
  286. // http://php.net/manual/en/function.intval.php (post Ben L)
  287. return (int) (string) ($this->getAmount() * 100);
  288. }
  289. /**
  290. * Get Description
  291. *
  292. * @see M_MultiSafepayTransaction::setDescription()
  293. * @access public
  294. * @return string
  295. */
  296. public function getDescription() {
  297. return $this->_description;
  298. }
  299. /**
  300. * Get Items
  301. *
  302. * Will provide the collection of {@link M_MultiSafepayTransactionItem}
  303. * objects that are part of this transaction
  304. *
  305. * @see M_MultiSafepayTransaction::addItem()
  306. * @access public
  307. * @return M_ArrayIterator
  308. */
  309. public function getItems() {
  310. return new M_ArrayIterator($this->_items);
  311. }
  312. /**
  313. * Get Xml
  314. *
  315. * Will return this {@link M_MultiSafepayTransaction} in XML format, which
  316. * is typically used to make the XML request in {@link M_MultiSafepay}
  317. *
  318. * @access public
  319. * @return string
  320. */
  321. public function getXml() {
  322. // Before proceeding, make sure the mandatory properties are provided:
  323. if(! $this->getId()) {
  324. throw new M_Exception(sprintf(
  325. 'Cannot proceed in %s::%s(): no transaction id has been set',
  326. __CLASS__,
  327. __FUNCTION__
  328. ));
  329. }
  330. if(! $this->getCurrency()) {
  331. throw new M_Exception(sprintf(
  332. 'Cannot proceed in %s::%s(): no active currency has been defined',
  333. __CLASS__,
  334. __FUNCTION__
  335. ));
  336. }
  337. if(! $this->getDescription()) {
  338. throw new M_Exception(sprintf(
  339. 'Cannot proceed in %s::%s(): no description has been set',
  340. __CLASS__,
  341. __FUNCTION__
  342. ));
  343. }
  344. // Start the output
  345. $out = '<transaction>';
  346. // Add the properties
  347. $out .= '<id>'. $this->getId() .'</id>';
  348. $out .= '<currency>'. $this->getCurrency() .'</currency>';
  349. $out .= '<amount>'. $this->getAmountForProcessing() .'</amount>';
  350. $out .= '<description><![CDATA['. $this->getDescription() .']]></description>';
  351. $out .= '<var1>' /* not in use currently */ .'</var1>';
  352. $out .= '<var2>' /* not in use currently */ .'</var2>';
  353. $out .= '<var3>' /* not in use currently */ .'</var3>';
  354. // Open the items tag
  355. $out .= '<items><![CDATA[';
  356. // If we received exactly 1 item
  357. if($this->getItems()->count() == 1) {
  358. // Then add that item
  359. $out .= $this->getItems()->first()->toString();
  360. }
  361. // Otherwise, if more than one item is specified
  362. elseif($this->getItems()->count() > 1) {
  363. // Then we have to display them in an unordered list. Open it:
  364. $out .= '<ul>';
  365. // Loop through the items
  366. foreach($this->getItems() as $item) {
  367. /* @var $item M_MultiSafepayTransactionItem */
  368. // And add each to the output
  369. $out .= '<li>'. $item->toString() .'</li>';
  370. }
  371. // Close the unordered list
  372. $out .= '</ul>';
  373. }
  374. // Close the items tag
  375. $out .= ']]></items>';
  376. // Add more properties
  377. $out .= '<manual>false' /* not in use currently */ .'</manual>';
  378. $out .= '<gateway>' /* not in use currently */ .'</gateway>';
  379. $out .= '<daysactive>' /* not in use currently */ .'</daysactive>';
  380. // Close the output
  381. $out .= '</transaction>';
  382. // Return return the output
  383. return $out;
  384. }
  385. }