Guest User

Untitled

a guest
Nov 25th, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @author Endre F. Kiss
  5. * @copyright 2015
  6. */
  7.  
  8. class Payment {
  9. private $FirstName;
  10. private $LastName;
  11. private $Currency;
  12. private $PaymentAmount;
  13. private $CreditCardNumber;
  14. private $CreditCardCCV;
  15. private $CreditCardExpirationMonth;
  16. private $CreditCardExpirationYear;
  17. private $Fields;
  18. private $url;
  19.  
  20. public function __construct() {
  21. $this->Fields = Array(
  22. 'FirstName'=>'First Name',
  23. 'LastName'=>'Last Name',
  24. 'Currency'=>'Currency',
  25. 'PaymentAmount'=>'Payment Amount',
  26. 'CreditCardNumber'=>'Credit CardNumber',
  27. 'CreditCardCCV'=>'Credit Card CCV',
  28. 'CreditCardExpirationMonth'=>'Credit Card Expiration Month',
  29. 'CreditCardExpirationYear'=>'Credit Card Expiration Year'
  30. );
  31. }
  32.  
  33. private function makePostData() {
  34. $sendArray = Array();
  35. foreach($this->Fields AS $key=>$value) {
  36. $sendArray[$key] = $this->$key;
  37. }
  38. return $sendArray;
  39. }
  40.  
  41. public function setUrl($url) {
  42. $this->url = $url;
  43. }
  44.  
  45. private function showAnswer($answer) {
  46. $items = "";
  47. foreach ($answer AS $key=>$value) {
  48. $items .= $this->template("result_item", Array("FIELD"=>$key, "VALUE"=>$value));
  49. }
  50. $content = $this->template("result", Array("RESULTS"=>$items));
  51. echo $this->template("index", Array("CONTENT"=>$content));
  52. }
  53.  
  54. private function doPay() {
  55. $answer = $this->sendData();
  56. if (is_object($answer) && count($answer)>0) {
  57. $this->showAnswer($answer);
  58. } else {
  59. $this->payError();
  60. }
  61. }
  62.  
  63. private function payError() {
  64. $content = $this->template("error", Array());
  65. echo $this->template("index", Array("CONTENT"=>$content));
  66. }
  67.  
  68. private function sendData() {
  69.  
  70. $postdata = $this->makePostData();
  71.  
  72. $send = curl_init($this->url);
  73. curl_setopt($send, CURLOPT_USERAGENT, "Credit Card Checkout client");
  74. curl_setopt($send, CURLOPT_ENCODING, "UTF-8" );
  75. curl_setopt($send, CURLOPT_RETURNTRANSFER ,1);
  76. curl_setopt($send, CURLOPT_POSTFIELDS, http_build_query($postdata));
  77. $ret = json_decode(curl_exec($send));
  78.  
  79. $this->log($postdata, $ret);
  80.  
  81. return $ret;
  82. }
  83.  
  84. private function log($postdata, $ret) {
  85. $file = fopen("log/log.txt", "a");
  86. fwrite($file, "Time: ".date("Y-m-d H:i:s")."\n");
  87. fwrite($file, "Postdata: ".json_encode($postdata)."\n");
  88. fwrite($file, "Result: ".json_encode($ret)."\n\n");
  89. fclose($file);
  90. }
  91.  
  92. public function startPay() {
  93. $errors = Array();
  94. $do = false;
  95. if (isset($_POST['gopay'])) {
  96. foreach($this->Fields AS $key=>$value) {
  97. if (strlen($_POST[$key])==0) {
  98. $errors[] = $key;
  99. } else {
  100. $this->$key = $_POST[$key];
  101. }
  102. }
  103. if (count($errors)==0) {
  104. $this->doPay();
  105. $do = true;
  106. }
  107. }
  108. if (!$do) $this->showForm($errors);
  109. }
  110.  
  111. private function showForm($errors) {
  112. $fields = "";
  113. foreach($this->Fields AS $key=>$value) {
  114. if (in_array($key, $errors)) {
  115. $error = $this->template("field_error", Array("FIELD"=>$value));
  116. } else {
  117. $error = "";
  118. }
  119. $fields .= $this->template("form_field", Array("FIELD"=>$key,"NAME"=>$value,"VALUE"=>$this->$key,"ERROR"=>$error));
  120. }
  121. $form = $this->template("form", Array("FIELDS"=>$fields));
  122. echo $this->template("index", Array("CONTENT"=>$form));
  123. }
  124.  
  125. private function template($file,$replace) {
  126.  
  127. $tpl = "";
  128. $fileName = "template/".$file.".html";
  129. if (file_exists($fileName)) {
  130.  
  131. $tpl = file_get_contents($fileName);
  132.  
  133. foreach ($replace as $key=>$val) {
  134. if (is_array($val)) $val = current($val);
  135. $tpl = str_replace("{".$key."}",$val,$tpl);
  136. }
  137. } else $tpl = '<div>UNDEFINED TEMPLATE: '.$file.'</div>';
  138.  
  139. return $tpl;
  140. }
  141.  
  142. }
  143.  
  144. ?>
Advertisement
Add Comment
Please, Sign In to add comment