Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author Endre F. Kiss
- * @copyright 2015
- */
- class Payment {
- private $FirstName;
- private $LastName;
- private $Currency;
- private $PaymentAmount;
- private $CreditCardNumber;
- private $CreditCardCCV;
- private $CreditCardExpirationMonth;
- private $CreditCardExpirationYear;
- private $Fields;
- private $url;
- public function __construct() {
- $this->Fields = Array(
- 'FirstName'=>'First Name',
- 'LastName'=>'Last Name',
- 'Currency'=>'Currency',
- 'PaymentAmount'=>'Payment Amount',
- 'CreditCardNumber'=>'Credit CardNumber',
- 'CreditCardCCV'=>'Credit Card CCV',
- 'CreditCardExpirationMonth'=>'Credit Card Expiration Month',
- 'CreditCardExpirationYear'=>'Credit Card Expiration Year'
- );
- }
- private function makePostData() {
- $sendArray = Array();
- foreach($this->Fields AS $key=>$value) {
- $sendArray[$key] = $this->$key;
- }
- return $sendArray;
- }
- public function setUrl($url) {
- $this->url = $url;
- }
- private function showAnswer($answer) {
- $items = "";
- foreach ($answer AS $key=>$value) {
- $items .= $this->template("result_item", Array("FIELD"=>$key, "VALUE"=>$value));
- }
- $content = $this->template("result", Array("RESULTS"=>$items));
- echo $this->template("index", Array("CONTENT"=>$content));
- }
- private function doPay() {
- $answer = $this->sendData();
- if (is_object($answer) && count($answer)>0) {
- $this->showAnswer($answer);
- } else {
- $this->payError();
- }
- }
- private function payError() {
- $content = $this->template("error", Array());
- echo $this->template("index", Array("CONTENT"=>$content));
- }
- private function sendData() {
- $postdata = $this->makePostData();
- $send = curl_init($this->url);
- curl_setopt($send, CURLOPT_USERAGENT, "Credit Card Checkout client");
- curl_setopt($send, CURLOPT_ENCODING, "UTF-8" );
- curl_setopt($send, CURLOPT_RETURNTRANSFER ,1);
- curl_setopt($send, CURLOPT_POSTFIELDS, http_build_query($postdata));
- $ret = json_decode(curl_exec($send));
- $this->log($postdata, $ret);
- return $ret;
- }
- private function log($postdata, $ret) {
- $file = fopen("log/log.txt", "a");
- fwrite($file, "Time: ".date("Y-m-d H:i:s")."\n");
- fwrite($file, "Postdata: ".json_encode($postdata)."\n");
- fwrite($file, "Result: ".json_encode($ret)."\n\n");
- fclose($file);
- }
- public function startPay() {
- $errors = Array();
- $do = false;
- if (isset($_POST['gopay'])) {
- foreach($this->Fields AS $key=>$value) {
- if (strlen($_POST[$key])==0) {
- $errors[] = $key;
- } else {
- $this->$key = $_POST[$key];
- }
- }
- if (count($errors)==0) {
- $this->doPay();
- $do = true;
- }
- }
- if (!$do) $this->showForm($errors);
- }
- private function showForm($errors) {
- $fields = "";
- foreach($this->Fields AS $key=>$value) {
- if (in_array($key, $errors)) {
- $error = $this->template("field_error", Array("FIELD"=>$value));
- } else {
- $error = "";
- }
- $fields .= $this->template("form_field", Array("FIELD"=>$key,"NAME"=>$value,"VALUE"=>$this->$key,"ERROR"=>$error));
- }
- $form = $this->template("form", Array("FIELDS"=>$fields));
- echo $this->template("index", Array("CONTENT"=>$form));
- }
- private function template($file,$replace) {
- $tpl = "";
- $fileName = "template/".$file.".html";
- if (file_exists($fileName)) {
- $tpl = file_get_contents($fileName);
- foreach ($replace as $key=>$val) {
- if (is_array($val)) $val = current($val);
- $tpl = str_replace("{".$key."}",$val,$tpl);
- }
- } else $tpl = '<div>UNDEFINED TEMPLATE: '.$file.'</div>';
- return $tpl;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment