|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?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)) {
- $folder = $f3->get('SESSION.checkout_folder');
- }
- if (is_string($folder)) {
- $f3->set('SESSION.checkout_folder', $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() {
- // fails silently, producing potentially weird untrackable faults if more than 10000 orders happen on one day
- $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;
- }
- }
-
- /**
- * buyer has approved all details and decided which payment method to use
- */
- function buy() {
- // set up environment
- $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');
-
- // generate an order number
- $sak->kInfo = $this->place_order();
- $sak->status = 1; // 1 := ordernumber is generated
- $sak->save();
-
- // load data needed o finish transaction
- $info->load(['id=?',$sak->kInfo]);
- $contact->load(['id=?', $sak->kContact]);
-
-
- // finish transaction
- 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':
- $f3->set('SESSION.xss-token',$f3->get('POST.xss-token'));
- $f3->reroute('/checkout/paypal');
- break;
- case 'approve':
- $email = new Email();
- $email->load_form_config($f3->get('SESSION.xss-token'));
-
- if ($this->email_to_merchant() &&
- $this->email_to_client($contact->email)) {
- $f3->set('SESSION',[]);
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode(['redirect' => '/checkout/success']);
- die;
- } else {
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode(['redirect' => '/checkout/failure']);
- die;
- }
- break;
- default:
- $f3->set('POST.tesst',"asdasd");
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode($_POST);
- die;
- 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');
- break;
- case "paypalapprove":
- //$f3->set('POST.payment', 'approve');
- $sak->status = $this->buy();
- break;
- default:
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode(['test' => 2]);
- die;
- break;
- }
- } else {
- header('Content-Type: application/json; charset=utf-8');
- echo json_encode(['error' => "no saksnummer"]);
- die;
- }
- }
-
- function html_cart() {
- $t = \Template::instance();
- return $t->render("checkout.htm");
- }
-
- }
|