GFPaystationPayment

The Gravity Forms Paystation (3 party hosted) GFPaystationPayment class.

Defined (1)

The class is defined in the following location(s).

/class.GFPaystationPayment.php  
  1. class GFPaystationPayment { 
  2.  
  3. public $sslVerifyPeer; 
  4. public $paystationId; // Paystation account id form the main settings or that chosen by user input of the form. The PlaystationPlugin class will pass in the correct thing. 
  5. public $gatewayId; // The Gateway id. 
  6. public $testMode; // Set when in test mode. 
  7. public $securityHash; // Set to string of letters and numbers, used to help verify that postback is from paystation. 
  8. public $merchantSession; // Unique identification code for each transaction. 
  9. public $amount; // When set in this class it should be converted to cents (so a whole number / integer). 
  10. public $currency; // This is set from the gravity forms currency thing. 
  11. public $merchantReference; // The merchant reference, typlically the customer's email address. 
  12. public $customerDetails; // Optional 255 chars of customer detail information. 
  13. public $orderDetails; // Optional 255 chars of order detail informaiton. 
  14.  
  15.  
  16. // ==================================================================================================================================== 
  17. /** 
  18. * populate members with defaults, and set account and environment information 
  19. * @param string $paystationId The paystation account id. 
  20. * @param string $gatewayId The gateway id. 
  21. * @param string $testMode Either Y or N to indicate if should run in test mode. 
  22. */ 
  23. // ==================================================================================================================================== 
  24. public function __construct($paystationId, $gatewayId, $testMode, $securityHash) { 
  25.  
  26. $this->sslVerifyPeer = true; 
  27. $this->paystationId = $paystationId; 
  28. $this->gatewayId = $gatewayId; 
  29. $this->testMode = $testMode; 
  30. $this->securityHash = $securityHash; 
  31.  
  32. // ==================================================================================================================================== 
  33. /** 
  34. * Process a payment against Paystation 3rd part hotsed gateway after validating. 
  35. */ 
  36. // ==================================================================================================================================== 
  37. public function processPayment() { 
  38.  
  39. $this->validate(); 
  40. return $this->sendPaymentRequest(GFPAYSTATION_API_URL); 
  41.  
  42. // ==================================================================================================================================== 
  43. /** 
  44. * Validate the data members to ensure that sufficient and valid information has been given 
  45. * @throws GFPaystationException 
  46. */ 
  47. // ==================================================================================================================================== 
  48. protected function validate() { 
  49. $errmsg = ''; 
  50.  
  51. // According to our spec the following need to be set (i.e. required). 
  52. // - pstn_pi : Paystation Id. 
  53. // - pstn_gi : Gateway Id. 
  54. // - pstn_ms : Merchant session. 
  55. // - pstn_am : Amount - integer only. 
  56. // - pstn_nr : t ot T - hard coded when payment is done. 
  57. if (strlen($this->paystationId) === 0) 
  58. $errmsg .= "paystationId cannot be empty.\n"; 
  59.  
  60. if (strlen($this->gatewayId) === 0) 
  61. $errmsg .= "gatewayId cannot be empty.\n"; 
  62.  
  63. if (strlen($this->merchantSession) === 0) 
  64. $errmsg .= "merchantSession cannot be empty.\n"; 
  65.  
  66. // The gateway requires that the amount is in cents, the amount will have been converted 
  67. // by this point to that so double-check is numeric and is integer. 
  68. if (!is_numeric($this->amount) || $this->amount <= 0) 
  69. $errmsg .= "amount must be given as a number in dollars only.\n"; 
  70. else if (!is_int($this->amount)) 
  71. $errmsg .= "amount must be an integer (cents).\n"; 
  72.  
  73. // If error then throw it so is displayed to the user by code up the line. 
  74. if (strlen($errmsg) > 0) { 
  75. throw new GFPaystationException($errmsg); 
  76. else { 
  77. // No errors so the payment will go ahead, we need though to ensure that the parameters which will be sent 
  78. // to the paystation gateway are not longer than what is accepted just to ensure that no issues. 
  79.  
  80. // The rules are... 
  81. // - Merchant session must not be more than 50 chars. 
  82. // - The merchant reference must not be more than 64 chars. 
  83. // - The customerDetails, which is optional, cannot be more than 255. 
  84. // - The orderDetails, which is optional, cannot be more than 255. 
  85. if (strlen($this->merchantSession) > 50) 
  86. $this->merchantSession = substr($this->merchantSession, 0, 50); 
  87.  
  88. if (strlen($this->merchantReference) > 64) 
  89. $this->merchantReference = substr($this->merchantReference, 0, 64); 
  90.  
  91. if (strlen($this->customerDetails) > 255) 
  92. $this->customerDetails = substr($this->customerDetails, 0, 255); 
  93.  
  94. if (strlen($this->orderDetails) > 255) 
  95. $this->orderDetails = substr($this->orderDetails, 0, 255); 
  96.  
  97. // ---------------------------------- 
  98. // Also ensure that only allowed characters are included in the fields which can be populated from user input, these are... 
  99. // Only the merchantReference, customerDetails, and orderDetails need the replacment. The biggest issue is quote characters. 
  100. // 0123456789abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWYXZ@_ -.#:;*(), +[]/| 
  101. $this->merchantReference = preg_replace('/[^a-z0-9@\_\ \-\.\, \(\)\[\]\:\;\#\+\/\|]*/i', '', $this->merchantReference); 
  102. $this->customerDetails = preg_replace('/[^a-z0-9@\_\ \-\.\, \(\)\[\]\:\;\#\+\/\|]*/i', '', $this->customerDetails); 
  103. $this->orderDetails = preg_replace('/[^a-z0-9@\_\ \-\.\, \(\)\[\]\:\;\#\+\/\|]*/i', '', $this->orderDetails); 
  104.  
  105. // ==================================================================================================================================== 
  106. /** 
  107. * This function sends the payment request by posting the information. 
  108. * @param paystationUrl is the url to the paystation gateway. 
  109. * @return XMLstring or false if failure. 
  110. */ 
  111. // ==================================================================================================================================== 
  112. protected function sendPaymentRequest($paystationUrl) { 
  113.  
  114. // Put the parameters for the post in to an associative array. 
  115. // First the required things. 
  116. $paramsArray = array('paystation' => '_empty',  
  117. 'pstn_pi' => $this->paystationId,  
  118. 'pstn_gi' => $this->gatewayId,  
  119. 'pstn_ms' => $this->merchantSession,  
  120. 'pstn_mr' => $this->merchantReference,  
  121. 'pstn_am' => $this->amount,  
  122. 'pstn_nr' => 't',  
  123. 'pstn_cu' => $this->currency 
  124. ); 
  125.  
  126. // Now add the optional things. 
  127. if ($this->testMode == 'Y') { 
  128. $paramsArray['pstn_tm'] = 't'; 
  129.  
  130. if ($this->customerDetails) { 
  131. $paramsArray['pstn_mc'] = $this->customerDetails; 
  132.  
  133. if ($this->orderDetails) { 
  134. $paramsArray['pstn_mo'] = $this->orderDetails; 
  135.  
  136. // Generate url encoded query string from the array. 
  137. $formattedData = http_build_query($paramsArray); 
  138.  
  139. // Put together context for the POST request using the encoded params. 
  140. $contextOptions = array ( 
  141. 'http' => array ( 
  142. 'method' => 'POST',  
  143. 'header' => "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($formattedData) . "\r\n",  
  144. 'content' => $formattedData 
  145. ); 
  146.  
  147. // Create a stream context, open for reading the response, then place the contents in to the response XML variable. 
  148. $ctx = stream_context_create($contextOptions); 
  149. $fp = @fopen($paystationUrl, 'r', false, $ctx); 
  150. $responseXML = @stream_get_contents($fp); 
  151.  
  152. // Create new response object, and get it to load its properies from the response XML. 
  153. $response = new GFPaystationPaymentResponse(); 
  154. $response->loadResponseXML($responseXML); 
  155.  
  156. // Return the PaystationPaymentResponse object. 
  157. return $response;