mivita/app/Http/Controllers/Web/CheckoutController.php
2019-11-23 15:02:41 +01:00

346 lines
No EOL
13 KiB
PHP
Executable file

<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Pay\PayoneController;
use App\Models\PaymentTransaction;
use App\Models\ShoppingOrder;
use App\Models\ShoppingOrderItem;
use App\Models\ShoppingPayment;
use App\Models\ShoppingUser;
use Illuminate\Session\SessionManager;
use Illuminate\Support\Collection;
use Validator;
use App\Services\Util;
use Yard;
use Input;
class CheckoutController extends Controller
{
private $session;
private $instance;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct(SessionManager $session)
{
$this->session = $session;
$this->instance = sprintf('%s.%s', 'cart', 'payments');
}
public function checkout(){
// $user_shop = Util::getUserShop();
if(Input::get('selected_country')){
Yard::instance('shopping')->setShippingCountryWithPrice(Input::get('selected_country'));
}else{
// $ShippingCountry = ShippingCountry::where('country_id', 1)->first();
// $selected_country = $ShippingCountry->id;
}
if(!$this->getPayments('shopping_user_id') && Util::getAuthUser()){
$user = Util::getAuthUser();
$user->email;
$account = $user->account;
$shopping_user = new ShoppingUser();
$shopping_user->auth_user_id = $user->id;
$shopping_user->billing_salutation = $user->account->salutation;
$shopping_user->billing_company = $user->account->company;
$shopping_user->billing_firstname = $user->account->first_name;
$shopping_user->billing_lastname = $user->account->last_name;
$shopping_user->billing_address = $user->account->address;
$shopping_user->billing_address_2 = $user->account->address_2;
$shopping_user->billing_zipcode = $user->account->zipcode;
$shopping_user->billing_city = $user->account->city;
$shopping_user->billing_country_id = $user->account->country_id;
$shopping_user->billing_phone = $user->account->phone;
$shopping_user->billing_email = $user->email;
$shopping_user->accepted_data_checkbox = 1;
$shopping_user->same_as_billing = $user->account->same_as_billing;
$shopping_user->shipping_salutation = $user->account->shipping_salutation;
$shopping_user->shipping_company = $user->account->shipping_company;
$shopping_user->shipping_firstname = $user->account->shipping_firstname;
$shopping_user->shipping_lastname = $user->account->shipping_lastname;
$shopping_user->shipping_address = $user->account->shipping_address;
$shopping_user->shipping_address_2 = $user->account->shipping_address_2;
$shopping_user->shipping_zipcode = $user->account->shipping_zipcode;
$shopping_user->shipping_city = $user->account->shipping_city;
$shopping_user->shipping_country_id = $user->account->shipping_country_id;
$shopping_user->shipping_phone = $user->account->shipping_phone;
$shopping_user->save();
$this->putPayments('shopping_user_id', $shopping_user->id);
}else{
$shopping_user = ShoppingUser::findOrNew($this->getPayments('shopping_user_id'));
}
if($shopping_user->same_as_billing === NULL){
$shopping_user->same_as_billing = true;
}
$data = [
'user_shop' => Util::getUserShop(),
'shopping_user' => $shopping_user,
];
return view('web.templates.checkout', $data);
}
public function checkoutFinal(){
$rules = array(
'billing_salutation' => 'required',
'billing_firstname'=>'required',
'billing_lastname'=>'required',
'billing_email'=>'required|email',
'billing_address'=>'required',
'billing_zipcode'=>'required',
'billing_city' => 'required',
'accepted_data_checkbox' => 'accepted',
);
if(!Input::get('same_as_billing')){
$rules = array_merge($rules, [
'shipping_firstname'=>'required',
'shipping_lastname'=>'required',
'shipping_address'=>'required',
'shipping_zipcode'=>'required',
'shipping_city' => 'required',
'shipping_salutation' => 'required'
]);
}
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return back()->withErrors($validator)->withErrors($validator)->withInput(Input::all());
}
$data = Input::all();
//make User
$shopping_user = $this->makeShoppingUser($data);
//make Order and Items
$shopping_order = $this->makeShoppingOrder($shopping_user);
//check credit Card
if(Input::get('payment_method')){
$cc_ret = [];
//need precheck the card
if(Input::get('payment_method') == 'cc'){
$pay = new PayoneController();
$cc_ret = $pay->checkCreditCard($data);
if($cc_ret['status'] == 'ERROR' || $cc_ret['status'] == 'INVALID'){
/* PaymentTransaction::create([
'shopping_payment_id' => //is no shopping_payment_id at this moment,
'request' => 'creditcardcheck,
'errorcode' => $cc_ret['errorcode'],
'errormessage' => $cc_ret['errormessage'],
'customermessage' => $cc_ret['customermessage'],
'status' => $response['status'],
]);*/
\Session::flash('cc-error', 1);
\Session::flash('errormessage', $cc_ret['errormessage']);
\Session::flash('customermessage', $cc_ret['customermessage']);
return redirect(route('checkout.checkout_card'))->withInput(Input::all());
exit;
}
if($cc_ret['status'] == 'VALID'){
/*
* array(4) { ["status"]=> string(5) "VALID" ["pseudocardpan"]=> string(19) "9410010000169020567" ["cardtype"]=> string(1) "V" ["truncatedcardpan"]=> string(16) "411111XXXXXX1111" }
* application through http post
* get this to the prepayment
* */
}
}
//other
$pay = new PayoneController();
$pay->init($shopping_user, $shopping_order);
$amount = intval(floatval(Yard::instance('shopping')->totalWithShipping(2, '.', ',')) *100);
$reference = $pay->setPrePayment(Input::get('payment_method'), $amount, 'EUR', $cc_ret);
$this->putPayments('payment_reference', $reference);
$pay->setPersonalData();
return $pay->ResponseData();
}
return redirect()->back();
}
public function transactionStatus($status, $reference){
$shopping_order_id = $this->getPayments('shopping_order_id');
$ShoppingPayment = ShoppingPayment::where('shopping_order_id', $shopping_order_id)->where('reference', $reference)->first();
if(!$ShoppingPayment){
//TODO log this
\Session::flash('checkout-error', 'Der Zahlungsvorgang konnte nicht abgeschlossen werden, die Zahlung wurde nicht gefunden: '.$reference);
return redirect(route('checkout.checkout_card'));
}
$ShoppingPayment->status = $status;
$ShoppingPayment->save();
if($status == "success"){
Yard::instance('shopping')->destroy();
$this->destroy();
$payt = $ShoppingPayment->payment_transactions->last();
$data = [
'user_shop' => Util::getUserShop(),
'order_reference' => $reference,
'pay_trans' => $payt,
];
return view('web.templates.checkout-final', $data);
}
if($status == "cancel"){
\Session::flash('checkout-error', 'Der Zahlungsvorgang wurde abgebrochen, die Bestellung konnte nicht ausgeführt werden.');
return redirect(route('checkout.checkout_card'));
}
if($status == "error"){
\Session::flash('checkout-error', 'Der Zahlungsvorgang wurde abgebrochen, die Bestellung konnte nicht ausgeführt werden.');
return redirect(route('checkout.checkout_card'));
}
}
public function transactionApproved($transactionId, $reference) {
$payt = PaymentTransaction::findOrFail($transactionId);
if($payt->shopping_payment->reference != $reference){
abort(404);
}
Yard::instance('shopping')->destroy();
$this->destroy();
//vor
$data = [
'user_shop' => Util::getUserShop(),
'order_reference' => $payt->shopping_payment->reference,
'pay_trans' => $payt,
];
return view('web.templates.checkout-final', $data);
}
private function makeShoppingUser($data){
$data['same_as_billing'] = isset($data['same_as_billing']) ? true : false;
$data['accepted_data_checkbox'] = isset($data['accepted_data_checkbox']) ? true : false;
$shopping_user = false;
if($this->getPayments('shopping_user_id')){
$shopping_user = ShoppingUser::find($this->getPayments('shopping_user_id'));
if($shopping_user){
$shopping_user->fill($data);
$shopping_user->save();
}
}
if(!$shopping_user){
$shopping_user = ShoppingUser::create($data);
}
$this->putPayments('shopping_user_id', $shopping_user->id);
return $shopping_user;
}
private function makeShoppingOrder($shopping_user){
$user_shop = Util::getUserShop();
$data = [
'shopping_user_id' => $shopping_user->id,
'auth_user_id' => $shopping_user->auth_user_id,
'country_id' => Yard::instance('shopping')->getShippingCountryId(),
'user_shop_id' => $user_shop->id,
'total' => Yard::instance('shopping')->total(2, '.', ','),
'shipping' => Yard::instance('shopping')->shipping(2, '.', ','),
'subtotal' => Yard::instance('shopping')->subtotalWithShipping(2, '.', ','),
'tax_rate' => Yard::getTaxRate(),
'tax' => Yard::instance('shopping')->subtotalWithShipping(2, '.', ','),
'total_shipping' => Yard::instance('shopping')->totalWithShipping(2, '.', ','),
'weight' => Yard::instance('shopping')->weight(),
];
$shopping_order= false;
if($this->getPayments('shopping_order_id')){
$shopping_order = ShoppingOrder::find($this->getPayments('shopping_order_id'));
if($shopping_order){
$shopping_order->fill($data);
$shopping_order->save();
}
}
if(!$shopping_order){
$shopping_order = ShoppingOrder::create($data);
}
$this->putPayments('shopping_order_id', $shopping_order->id);
$items = Yard::instance('shopping')->content();
$shopping_order->shopping_order_items()->each(function($model) use ($items, $shopping_order) {
foreach ($items as $item) {
if ($model->row_id === $item->rowId) {
$model->fill([
'shopping_order_id' => $shopping_order->id,
'row_id' => $item->rowId,
'product_id' => $item->id,
'qty' => $item->qty,
'price' => $item->price,
'slug' => $item->options->slug,
])->save();
return false;
}
}
return $model->delete();
});
foreach ($items as $item) {
if (!ShoppingOrderItem::where('shopping_order_id', $shopping_order->id)->where('row_id', $item->rowId)->count())
ShoppingOrderItem::create([
'shopping_order_id' => $shopping_order->id,
'row_id' => $item->rowId,
'product_id' => $item->id,
'qty' => $item->qty,
'price' => $item->price,
'slug' => $item->options->slug
]);
};
return $shopping_order;
}
private function putPayments($key, $value){
$content = $this->getContent();
$content->put($key, $value);
$this->session->put($this->instance, $content);
}
private function getPayments($key){
$content = $this->getContent();
if ($content->has($key)){
return $content->get($key);
}
return false;
}
private function getContent()
{
if (is_null($this->session->get($this->instance))) {
return new Collection([]);
}
return $this->session->get($this->instance);
}
public function destroy()
{
$this->session->remove($this->instance);
}
}