|
- <?php
-
- namespace Controller;
-
- class Checkout {
-
- public $saksnummer;
- public $sak;
- public $DB = null;
- private $hydrated = false;
-
- function __construct($folder = null) {
- $f3 = \Base::instance();
- $saksnummer = $f3->get('SESSION.saksnummer');
- if ($saksnummer) {
- $this->saksnummer = $saksnummer;
- } else {
- $this->saksnummer = md5(sprintf("%s%f",$_SERVER['REMOTE_ADDR'],$_SERVER['REQUEST_TIME_FLOAT']));
- $f3->set('SESSION.saksnummer', $this->saksnummer);
- }
-
- if (is_object($folder)) {
- $folder = $f3->get('POST.datapath');
- }
- if (is_string($folder)) {
- $this->DB = new \DB\SQL(sprintf("sqlite:%sdatabase.sqlite",$folder));
- }
-
-
- }
-
- function hydrate_framework_variables() {
- if($this->hydrated) {
- return false;
- }
- $f3 = \Base::instance();
- $sak = new \DB\SQL\Mapper($this->DB,'saklist');
- $a = new \DB\SQL\Mapper($this->DB,'addresses');
- $contact = new \DB\SQL\Mapper($this->DB,'contacts');
-
- $sak->load(['id=?', $this->saksnummer]);
-
- $f3->mset([
- 'full_cart' => $this->html_cart(),
- 'contact' => $contact->load(['id=?',$sak->kContact]),
- 'shipping' => $a->load(['id=?',$sak->kShipping]),
- 'billing' => $sak->kBilling ? $a->load(['id=?',$sak->kBilling]) : $a
- ]);
- $this->hydrated = true;
-
- }
-
- function overview() {
- $f3 = \Base::instance();
- $tpl = \Template::instance();
-
- $this->hydrate_framework_variables();
-
-
- return $tpl->render('checkout_overview.htm',true);
- }
-
-
-
- function index() {
- $f3 = \Base::instance();
-
- $sak = new \DB\SQL\Mapper($this->DB,'saklist');
-
-
- if ($sak->load(['id=?',$this->saksnummer]) === false) {
- $sak->id = $this->saksnummer;
- $sak->save();
- }
-
- if ($sak->kContact === null) {
- $f3->reroute('/checkout/contact');
- }
- if ($sak->kShipping === null) {
- $f3->reroute('/checkout/shipping_address');
- }
-
- return $this->overview();
- }
-
- function save_address() {
- $f3 = \Base::instance();
- $a = new \DB\SQL\Mapper($this->DB,'addresses');
- $a->name = $f3->get('POST.name');
- $a->address1 = $f3->get('POST.address1');
- $a->address2 = $f3->get('POST.address2');
- $a->zip = $f3->get('POST.zip');
- $a->place = $f3->get('POST.town');
- $a->country = $f3->get('POST.country');
- $a->save();
- return $a->id;
- }
-
- function save_contact() {
- $f3 = \Base::instance();
- $a = new \DB\SQL\Mapper($this->DB,'contacts');
- //$a->name = $f3->get('POST.name');
- $a->email = $f3->get('POST.email');
- $a->save();
- return $a->id;
- }
-
- function place_order() {
- $info = new \DB\SQL\Mapper($this->DB,'info');
- for ($i=0;$i<9999;$i++) {
- $candidate = strval(sprintf('%s%04d',date('Ymd'),$i));
- if (!$info->load(['ordernumber=?',$candidate])) {
- $info->ordernumber = $candidate;
- $info->save();
- break;
- }
- }
- return $info->id;
- }
-
- function email_to_merchant() {
- $f3 = \Base::instance();
- $tpl = \Template::instance();
-
-
- $c = $f3->get('checkout_data.emailconfig');
- $subject = $f3->get('checkout_data.subject');
- $smtp = new \SMTP(
- $c['host'],
- $c['port'],
- $c['scheme'],
- $c['user'],
- $c['pass'],
- );
-
- $headers = [
- "MIME-Version"=>"1.0",
- "Content-type"=>"text/html",
- "From" => $c['from']
- ];
-
- $smtp->set('To', $c['admin']);
- $smtp->set('Subject',$subject);
- foreach ($headers as $k=>$v) {
- $smtp->set($k,$v);
- }
-
- $f3->set('order_summary', $this->overview());
- $f3->set('order_summary', $tpl->render('checkout_overview_kunde.htm',true));
- $f3->set('UI', $f3->get('UI').";".$f3->get('form_path'));
- $email = $tpl->render($f3->get('checkout_data.template'),true);
-
- if ($smtp->send($email)) {
- return true;
-
- } else {
- return false;
-
- }
- }
-
- function email_to_client($recipient) {
- $f3 = \Base::instance();
- $tpl = \Template::instance();
-
- $c = $f3->get('checkout_data.emailconfig');
- $subject = $f3->get('checkout_data.subject');
- $smtp = new \SMTP(
- $c['host'],
- $c['port'],
- $c['scheme'],
- $c['user'],
- $c['pass'],
- );
-
- $headers = [
- "MIME-Version"=>"1.0",
- "Content-type"=>"text/html",
- "From" => $c['from']
- ];
-
- $smtp->set('To', $recipient);
- $smtp->set('Subject',$subject);
- foreach ($headers as $k=>$v) {
- $smtp->set($k,$v);
- }
- $f3->set('order_summary', $this->overview());
- $f3->set('order_summary', $tpl->render('checkout_overview_kunde.htm',true));
-
- $f3->set('UI', $f3->get('UI').";".$f3->get('form_path'));
- $email = $tpl->render($f3->get('checkout_data.template'), true);
-
- if ($smtp->send($email)) {
- return true;
- } else {
- return false;
- }
- }
-
- function buy() {
- $f3 = \Base::instance();
- $sak = new \DB\SQL\Mapper($this->DB,'saklist');
- $info = new \DB\SQL\Mapper($this->DB,'info');
- $contact = new \DB\SQL\Mapper($this->DB,'contacts');
- $sak->load(['id=?',$this->saksnummer]);
- $datapath = $f3->get('POST.datapath');
-
- $sak->kInfo = $this->place_order();
- $sak->status = 1; // 1 := ordernumber is generated
- $sak->save();
-
- $info->load(['id=?',$sak->kInfo]);
- $contact->load(['id=?', $sak->kContact]);
-
- switch ($f3->get('POST.payment')) {
- case 'transfer':
- $email = new Email();
- $email->load_form_config($f3->get('POST.xss-token'));
-
- //var_dump($f3->get('private'));
- if ($this->email_to_merchant() &&
- $this->email_to_client($contact->email)) {
- $f3->set('SESSION',[]);
- $f3->reroute('/checkout/success');
- } else {
- $f3->reroute('/checkout/failure');
- }
- break;
- case 'paypal':
- break;
- }
- }
-
- function api(\Base $f3, $params) {
-
- $sak = new \DB\SQL\Mapper($this->DB,'saklist');
- if ($sak->load(['id=?',$this->saksnummer]) !== false) {
- switch ($params['method']) {
- case "contact":
- $sak->kContact = $this->save_contact();
- $sak->save();
- $f3->reroute('/checkout');
- break;
- case "shipping_address":
- $sak->kShipping = $this->save_address();
- $sak->save();
- $f3->reroute('/checkout');
- break;
- case "billing_address":
- $sak->kBilling = $this->save_address();
- $sak->save();
- $f3->reroute('/checkout');
- break;
- case "buy":
- $sak->status = $this->buy();
- // $sak->save();
- // $f3->reroute('/checkout/success');
- default:
- break;
- }
- }
- }
-
- function html_cart() {
- $t = \Template::instance();
- return $t->render("checkout.htm");
- }
-
- }
|