410 lines
No EOL
17 KiB
PHP
Executable file
410 lines
No EOL
17 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 App\User;
|
|
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;
|
|
$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(Util::getAuthUser()){
|
|
$shopping_user->abo_options = Util::getUserHistoryValue('abo_options');
|
|
$shopping_user->save();
|
|
}
|
|
|
|
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);
|
|
Util::setUserHistoryValue(['status'=>2, 'shopping_order_id'=>$shopping_order->id]);
|
|
|
|
//check credit Card
|
|
if(Input::get('payment_method')){
|
|
|
|
$ret = [];
|
|
//need precheck the card
|
|
if(Input::get('payment_method') === 'cc'){
|
|
$pay = new PayoneController();
|
|
$ret['cc'] = $pay->checkCreditCard($data);
|
|
if($ret['cc']['status'] === 'ERROR' || $ret['cc']['status'] === 'INVALID'){
|
|
/* PaymentTransaction::create([
|
|
'shopping_payment_id' => //is no shopping_payment_id at this moment,
|
|
'request' => 'creditcardcheck,
|
|
'errorcode' => $ret['cc']['errorcode'],
|
|
'errormessage' => $ret['cc']['errormessage'],
|
|
'customermessage' => $ret['cc']['customermessage'],
|
|
'status' => $response['status'],
|
|
]);*/
|
|
\Session::flash('cc-error', 1);
|
|
\Session::flash('errormessage', $ret['cc']['errormessage']);
|
|
\Session::flash('customermessage', $ret['cc']['customermessage']);
|
|
return redirect(route('checkout.checkout_card'))->withInput(Input::all());
|
|
}
|
|
if($ret['cc']['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
|
|
* */
|
|
}
|
|
}
|
|
|
|
//need precheck the card
|
|
if(Input::get('payment_method') === 'elv' && is_null(Input::get('mandate_identification'))){
|
|
$pay = new PayoneController();
|
|
$amount = (int) (float) Yard::instance('shopping')->totalWithShipping(2, '.', ',') *100;
|
|
$ret['elv'] = $pay->checkBankAccount($data, $amount, 'EUR', $shopping_user);
|
|
|
|
if($ret['elv']['status'] === 'ERROR' || $ret['elv']['status'] === 'INVALID'){
|
|
/* PaymentTransaction::create([
|
|
'shopping_payment_id' => //is no shopping_payment_id at this moment,
|
|
'request' => 'creditcardcheck,
|
|
'errorcode' => $ret['cc']['errorcode'],
|
|
'errormessage' => $ret['cc']['errormessage'],
|
|
'customermessage' => $ret['cc']['customermessage'],
|
|
'status' => $response['status'],
|
|
]);*/
|
|
\Session::flash('elv-error', 1);
|
|
\Session::flash('errormessage', $ret['elv']['errormessage']);
|
|
\Session::flash('customermessage', $ret['elv']['customermessage']);
|
|
return redirect(route('checkout.checkout_card'))->withInput(Input::all());
|
|
}
|
|
if($ret['elv']['status'] === 'APPROVED' && $ret['elv']['mandate_status'] !== "active"){
|
|
\Session::flash('elv-managemandate', 1);
|
|
\Session::flash('elv-mandate_identification', $ret['elv']['mandate_identification']);
|
|
\Session::flash('elv-mandate_text', $ret['elv']['mandate_text']);
|
|
\Session::flash('elv-creditor_identifier', $ret['elv']['creditor_identifier']);
|
|
return redirect(route('checkout.checkout_card'))->withInput(Input::all());
|
|
|
|
/*
|
|
* 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
|
|
* */
|
|
}
|
|
$ret['elv']['bankaccountholder'] = $data['elv_bankaccountholder'];
|
|
|
|
}
|
|
if(Input::get('payment_method') === 'elv' && Input::get('mandate_identification')) {
|
|
$ret['elv']['mandate_identification'] = Input::get('mandate_identification');
|
|
$ret['elv']['creditor_identifier'] = Input::get('creditor_identifier');
|
|
$ret['elv']['iban'] = $data['elv_iban'];
|
|
$ret['elv']['bic'] = $data['elv_bic'];
|
|
$ret['elv']['bankaccountholder'] = $data['elv_bankaccountholder'];
|
|
//check abo and save the mandate
|
|
$this->storeUserPaymentsData($shopping_user, $ret);
|
|
|
|
}
|
|
//other
|
|
$pay = new PayoneController();
|
|
$pay->init($shopping_user, $shopping_order);
|
|
$amount = (int) (float) Yard::instance('shopping')->totalWithShipping(2, '.', ',') *100;
|
|
$reference = $pay->setPrePayment(Input::get('payment_method'), $amount, 'EUR', $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
|
|
Util::setUserHistoryValue(['status'=>21]);
|
|
|
|
\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"){
|
|
Util::setUserHistoryValue(['status'=>22]);
|
|
\Session::flash('checkout-error', 'Der Zahlungsvorgang wurde abgebrochen, die Bestellung konnte nicht ausgeführt werden.');
|
|
return redirect(route('checkout.checkout_card'));
|
|
|
|
}
|
|
if($status === "error"){
|
|
Util::setUserHistoryValue(['status'=>23]);
|
|
\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 storeUserPaymentsData($shopping_user, $ret){
|
|
if($shopping_user->auth_user_id){
|
|
$user = User::find($shopping_user->auth_user_id);
|
|
if($user && $user->account && $shopping_user->abo_options){
|
|
if(isset($ret['elv']) && is_array($ret['elv'])){
|
|
$user->account->payment_data = $ret['elv'];
|
|
$user->account->save();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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,
|
|
'payment_for' => Util::getUserPaymentFor(),
|
|
'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);
|
|
}
|
|
|
|
} |