Guest User

Untitled

a guest
Jul 9th, 2018
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. <?php
  2.  
  3. class LastschriftPayment extends Payment {
  4. static $db = array(
  5. 'TxnRef' => 'Text'
  6. );
  7.  
  8. // DPS Informations
  9.  
  10. protected static $privacy_link = '';
  11.  
  12. protected static $logo = '';
  13.  
  14. // URLs
  15.  
  16. protected static $url = '';
  17.  
  18. // Payment Informations
  19.  
  20. protected static $username;
  21.  
  22. protected static $password;
  23.  
  24. static function set_account($username, $password) {
  25. self::$username = $username;
  26. self::$password = $password;
  27. }
  28.  
  29. protected static $cvn_mode = true;
  30.  
  31. static function unset_cvn_mode() {
  32. self::$cvn_mode = false;
  33. }
  34.  
  35. protected static $credit_cards = array(
  36.  
  37. );
  38.  
  39. static function remove_credit_card($creditCard) {
  40. unset(self::$credit_cards[$creditCard]);
  41. }
  42.  
  43. function getPaymentFormFields() {
  44.  
  45. $privacyLink = '<a href="' . self::$privacy_link . '" target="_blank" title="Read DPS\'s privacy policy">' . $logo . '</a><br/>';
  46. $paymentsList = '';
  47. foreach(self::$credit_cards as $name => $image) $paymentsList .= '<img src="' . $image . '" alt="' . $name . '"/>';
  48. $fields = new FieldSet(
  49. new LiteralField('DPSInfo', $privacyLink),
  50. new LiteralField('DPSPaymentsList', $paymentsList),
  51. new TextField('DPS_CreditCardHolderName', 'Kontoinhaber :'),
  52. new TextField('DPS_CreditCardExpiry', 'Kontonummer:')
  53. );
  54. if(self::$cvn_mode) $fields->push(new TextField('DPS_CreditCardCVN', 'Bankleitzahl:'));
  55. return $fields;
  56. }
  57.  
  58. /**
  59. * Returns the required fields to add to the order form, when using this payment method.
  60. */
  61. function getPaymentFormRequirements() {
  62. $jsCode = <<<JS
  63. require('DPS_CreditCardHolderName');
  64. require('DPS_CreditCardExpiry');
  65. JS;
  66. $phpCode = '
  67. $this->requireField("DPS_CreditCardHolderName", $data);
  68. $this->requireField("DPS_CreditCardExpiry", $data);
  69. ';
  70. return array('js' => $jsCode, 'php' => $phpCode);
  71. }
  72.  
  73. function processPayment($data, $form) {
  74.  
  75. // 1) Main Settings
  76.  
  77. $inputs['PostUsername'] = self::$username;
  78. $inputs['PostPassword'] = self::$password;
  79.  
  80. // 2) Payment Informations
  81.  
  82. $inputs['Amount'] = $this->Amount;
  83. $inputs['InputCurrency'] = $this->Currency;
  84. $inputs['TxnId'] = $this->ID;
  85. $inputs['TxnType'] = 'Purchase';
  86.  
  87. // 3) Credit Card Informations
  88.  
  89. $inputs['CardHolderName'] = $data['DPS_Kontoinhaber'];
  90. $inputs['DateExpiry'] = $data['DPS_CreditCardExpiry'];
  91. if(self::$cvn_mode) $inputs['Cvc2'] = $data['DPS_CreditCardCVN'] ? $data['DPS_CreditCardCVN'] : '';
  92.  
  93. // 4) DPS Transaction Sending
  94.  
  95. $responseFields = $this->doPayment($inputs);
  96.  
  97. // 5) DPS Response Management
  98.  
  99. if($responseFields['SUCCESS']) {
  100. $this->Status = 'Success';
  101. $result = new Payment_Success();
  102. }
  103. else {
  104. $this->Status = 'Failure';
  105. $result = new Payment_Failure();
  106. }
  107.  
  108. if($transactionRef = $responseFields['DPSTXNREF']) $this->TxnRef = $transactionRef;
  109.  
  110. if($helpText = $responseFields['HELPTEXT']) $this->Message = $helpText;
  111. else if($responseText = $responseFields['RESPONSETEXT']) $this->Message = $responseText;
  112.  
  113. $this->write();
  114. return $result;
  115. }
  116.  
  117. function doPayment(array $inputs) {
  118.  
  119. // 1) Transaction Creation
  120. $transaction = "<Txn>";
  121. foreach($inputs as $name => $value) {
  122. if($name == "Amount") {
  123. $value = number_format($value, 2, '.', '');
  124. }
  125. $transaction .= "<$name>$value</$name>";
  126. }
  127. $transaction .= "</Txn>";
  128.  
  129. // 2) CURL Creation
  130.  
  131. $clientURL = curl_init();
  132. curl_setopt($clientURL, CURLOPT_URL, self::$url);
  133. curl_setopt($clientURL, CURLOPT_POST, 1);
  134. curl_setopt($clientURL, CURLOPT_POSTFIELDS, $transaction);
  135. curl_setopt($clientURL, CURLOPT_RETURNTRANSFER, 1);
  136. curl_setopt($clientURL, CURLOPT_SSLVERSION, 3);
  137.  
  138. // 3) CURL Execution
  139.  
  140. $resultXml = curl_exec($clientURL);
  141.  
  142. // 4) CURL Closing
  143.  
  144. curl_close ($clientURL);
  145.  
  146. // 5) XML Parser Creation
  147.  
  148. $xmlParser = xml_parser_create();
  149. $values = null;
  150. $indexes = null;
  151.  
  152.  
  153. xml_parse_into_struct($xmlParser, $resultXml, $values, $indexes);
  154. xml_parser_free($xmlParser);
  155.  
  156. // 6) XML Result Parsed In A PHP Array
  157.  
  158. $resultPhp = array();
  159. $level = array();
  160. foreach($values as $xmlElement) {
  161. if($xmlElement['type'] == 'open') {
  162. if(array_key_exists('attributes', $xmlElement)) list($level[$xmlElement['level']], $extra) = array_values($xmlElement['attributes']);
  163. else $level[$xmlElement['level']] = $xmlElement['tag'];
  164. }
  165. else if ($xmlElement['type'] == 'complete') {
  166. $startLevel = 1;
  167. $phpArray = '$resultPhp';
  168. while($startLevel < $xmlElement['level']) $phpArray .= '[$level['. $startLevel++ .']]';
  169. $phpArray .= '[$xmlElement[\'tag\']] = array_key_exists(\'value\', $xmlElement)? $xmlElement[\'value\'] : null;';
  170. eval($phpArray);
  171. }
  172. }
  173.  
  174. $result = $resultPhp['TXN'];
  175.  
  176. return $result;
  177. }
  178. }
  179.  
  180. ?>
Add Comment
Please, Sign In to add comment