mostly filebased Content Presentation System
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace Controller;
  3. class Checkout {
  4. public $saksnummer;
  5. public $sak;
  6. public $DB = null;
  7. private $hydrated = false;
  8. function __construct($folder = null) {
  9. $f3 = \Base::instance();
  10. $saksnummer = $f3->get('SESSION.saksnummer');
  11. if ($saksnummer) {
  12. $this->saksnummer = $saksnummer;
  13. } else {
  14. $this->saksnummer = md5(sprintf("%s%f",$_SERVER['REMOTE_ADDR'],$_SERVER['REQUEST_TIME_FLOAT']));
  15. $f3->set('SESSION.saksnummer', $this->saksnummer);
  16. }
  17. if (is_object($folder)) {
  18. $folder = $f3->get('POST.datapath');
  19. }
  20. if (!is_string($folder)) {
  21. $folder = $f3->get('SESSION.checkout_folder');
  22. }
  23. if (is_string($folder)) {
  24. $f3->set('SESSION.checkout_folder', $folder);
  25. $this->DB = new \DB\SQL(sprintf("sqlite:%sdatabase.sqlite",$folder));
  26. }
  27. }
  28. function hydrate_framework_variables() {
  29. if($this->hydrated) {
  30. return false;
  31. }
  32. $f3 = \Base::instance();
  33. $sak = new \DB\SQL\Mapper($this->DB,'saklist');
  34. $a = new \DB\SQL\Mapper($this->DB,'addresses');
  35. $contact = new \DB\SQL\Mapper($this->DB,'contacts');
  36. $sak->load(['id=?', $this->saksnummer]);
  37. $f3->mset([
  38. 'full_cart' => $this->html_cart(),
  39. 'contact' => $contact->load(['id=?',$sak->kContact]),
  40. 'shipping' => $a->load(['id=?',$sak->kShipping]),
  41. 'billing' => $sak->kBilling ? $a->load(['id=?',$sak->kBilling]) : $a
  42. ]);
  43. $this->hydrated = true;
  44. }
  45. function overview() {
  46. $f3 = \Base::instance();
  47. $tpl = \Template::instance();
  48. $this->hydrate_framework_variables();
  49. return $tpl->render('checkout_overview.htm',true);
  50. }
  51. function index() {
  52. $f3 = \Base::instance();
  53. $sak = new \DB\SQL\Mapper($this->DB,'saklist');
  54. if ($sak->load(['id=?',$this->saksnummer]) === false) {
  55. $sak->id = $this->saksnummer;
  56. $sak->save();
  57. }
  58. if ($sak->kContact === null) {
  59. $f3->reroute('/checkout/contact');
  60. }
  61. if ($sak->kShipping === null) {
  62. $f3->reroute('/checkout/shipping_address');
  63. }
  64. return $this->overview();
  65. }
  66. function save_address() {
  67. $f3 = \Base::instance();
  68. $a = new \DB\SQL\Mapper($this->DB,'addresses');
  69. $a->name = $f3->get('POST.name');
  70. $a->address1 = $f3->get('POST.address1');
  71. $a->address2 = $f3->get('POST.address2');
  72. $a->zip = $f3->get('POST.zip');
  73. $a->place = $f3->get('POST.town');
  74. $a->country = $f3->get('POST.country');
  75. $a->save();
  76. return $a->id;
  77. }
  78. function save_contact() {
  79. $f3 = \Base::instance();
  80. $a = new \DB\SQL\Mapper($this->DB,'contacts');
  81. //$a->name = $f3->get('POST.name');
  82. $a->email = $f3->get('POST.email');
  83. $a->save();
  84. return $a->id;
  85. }
  86. function place_order() {
  87. // fails silently, producing potentially weird untrackable faults if more than 10000 orders happen on one day
  88. $info = new \DB\SQL\Mapper($this->DB,'info');
  89. for ($i=0;$i<9999;$i++) {
  90. $candidate = strval(sprintf('%s%04d',date('Ymd'),$i));
  91. if (!$info->load(['ordernumber=?',$candidate])) {
  92. $info->ordernumber = $candidate;
  93. $info->save();
  94. break;
  95. }
  96. }
  97. return $info->id;
  98. }
  99. function email_to_merchant() {
  100. $f3 = \Base::instance();
  101. $tpl = \Template::instance();
  102. $c = $f3->get('checkout_data.emailconfig');
  103. $subject = $f3->get('checkout_data.subject');
  104. $smtp = new \SMTP(
  105. $c['host'],
  106. $c['port'],
  107. $c['scheme'],
  108. $c['user'],
  109. $c['pass'],
  110. );
  111. $headers = [
  112. "MIME-Version"=>"1.0",
  113. "Content-type"=>"text/html",
  114. "From" => $c['from']
  115. ];
  116. $smtp->set('To', $c['admin']);
  117. $smtp->set('Subject',$subject);
  118. foreach ($headers as $k=>$v) {
  119. $smtp->set($k,$v);
  120. }
  121. $f3->set('order_summary', $this->overview());
  122. $f3->set('order_summary', $tpl->render('checkout_overview_kunde.htm',true));
  123. $f3->set('UI', $f3->get('UI').";".$f3->get('form_path'));
  124. $email = $tpl->render($f3->get('checkout_data.template'),true);
  125. if ($smtp->send($email)) {
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. }
  131. function email_to_client($recipient) {
  132. $f3 = \Base::instance();
  133. $tpl = \Template::instance();
  134. $c = $f3->get('checkout_data.emailconfig');
  135. $subject = $f3->get('checkout_data.subject');
  136. $smtp = new \SMTP(
  137. $c['host'],
  138. $c['port'],
  139. $c['scheme'],
  140. $c['user'],
  141. $c['pass'],
  142. );
  143. $headers = [
  144. "MIME-Version"=>"1.0",
  145. "Content-type"=>"text/html",
  146. "From" => $c['from']
  147. ];
  148. $smtp->set('To', $recipient);
  149. $smtp->set('Subject',$subject);
  150. foreach ($headers as $k=>$v) {
  151. $smtp->set($k,$v);
  152. }
  153. $f3->set('order_summary', $this->overview());
  154. $f3->set('order_summary', $tpl->render('checkout_overview_kunde.htm',true));
  155. $f3->set('UI', $f3->get('UI').";".$f3->get('form_path'));
  156. $email = $tpl->render($f3->get('checkout_data.template'), true);
  157. if ($smtp->send($email)) {
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. }
  163. /**
  164. * buyer has approved all details and decided which payment method to use
  165. */
  166. function buy() {
  167. // set up environment
  168. $f3 = \Base::instance();
  169. $sak = new \DB\SQL\Mapper($this->DB,'saklist');
  170. $info = new \DB\SQL\Mapper($this->DB,'info');
  171. $contact = new \DB\SQL\Mapper($this->DB,'contacts');
  172. $sak->load(['id=?',$this->saksnummer]);
  173. //$datapath = $f3->get('POST.datapath');
  174. // generate an order number
  175. $sak->kInfo = $this->place_order();
  176. $sak->status = 1; // 1 := ordernumber is generated
  177. $sak->save();
  178. // load data needed o finish transaction
  179. $info->load(['id=?',$sak->kInfo]);
  180. $contact->load(['id=?', $sak->kContact]);
  181. // finish transaction
  182. switch ($f3->get('POST.payment')) {
  183. case 'transfer':
  184. $email = new Email();
  185. $email->load_form_config($f3->get('POST.xss-token'));
  186. //var_dump($f3->get('private'));
  187. if ($this->email_to_merchant() &&
  188. $this->email_to_client($contact->email)) {
  189. $f3->set('SESSION',[]);
  190. $f3->reroute('/checkout/success');
  191. } else {
  192. $f3->reroute('/checkout/failure');
  193. }
  194. break;
  195. case 'paypal':
  196. $f3->set('SESSION.xss-token',$f3->get('POST.xss-token'));
  197. $f3->reroute('/checkout/paypal');
  198. break;
  199. case 'approve':
  200. $email = new Email();
  201. $email->load_form_config($f3->get('SESSION.xss-token'));
  202. if ($this->email_to_merchant() &&
  203. $this->email_to_client($contact->email)) {
  204. $f3->set('SESSION',[]);
  205. header('Content-Type: application/json; charset=utf-8');
  206. echo json_encode(['redirect' => '/checkout/success']);
  207. die;
  208. } else {
  209. header('Content-Type: application/json; charset=utf-8');
  210. echo json_encode(['redirect' => '/checkout/failure']);
  211. die;
  212. }
  213. break;
  214. default:
  215. $f3->set('POST.tesst',"asdasd");
  216. header('Content-Type: application/json; charset=utf-8');
  217. echo json_encode($_POST);
  218. die;
  219. break;
  220. }
  221. }
  222. function api(\Base $f3, $params) {
  223. $sak = new \DB\SQL\Mapper($this->DB,'saklist');
  224. if ($sak->load(['id=?',$this->saksnummer]) !== false) {
  225. switch ($params['method']) {
  226. case "contact":
  227. $sak->kContact = $this->save_contact();
  228. $sak->save();
  229. $f3->reroute('/checkout');
  230. break;
  231. case "shipping_address":
  232. $sak->kShipping = $this->save_address();
  233. $sak->save();
  234. $f3->reroute('/checkout');
  235. break;
  236. case "billing_address":
  237. $sak->kBilling = $this->save_address();
  238. $sak->save();
  239. $f3->reroute('/checkout');
  240. break;
  241. case "buy":
  242. $sak->status = $this->buy();
  243. // $sak->save();
  244. // $f3->reroute('/checkout/success');
  245. break;
  246. case "paypalapprove":
  247. //$f3->set('POST.payment', 'approve');
  248. $sak->status = $this->buy();
  249. break;
  250. default:
  251. header('Content-Type: application/json; charset=utf-8');
  252. echo json_encode(['test' => 2]);
  253. die;
  254. break;
  255. }
  256. } else {
  257. header('Content-Type: application/json; charset=utf-8');
  258. echo json_encode(['error' => "no saksnummer"]);
  259. die;
  260. }
  261. }
  262. function html_cart() {
  263. $t = \Template::instance();
  264. return $t->render("checkout.htm");
  265. }
  266. }