mivita/app/Http/Controllers/Web/CheckoutController.php
Kevin Adametz 446bc4561b checkout, register, payment,
checkout correction,
register wizard,
payment packege,
2019-03-02 00:08:11 +01:00

470 lines
No EOL
18 KiB
PHP
Executable file

<?php
namespace App\Http\Controllers\Web;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Pay\PayoneController;
use App\Mail\MailCheckout;
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 Illuminate\Support\Facades\Mail;
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);
}
public function paymentStatus(){
$data = \Request::all();
// test para
$data = [
'key' => '698fb2555f8b2efc74f60b2121421f45',
'txaction' => 'paid',
'clearingtype' => 'wlt',
'userid' => '158006846',
'txid' => '320267294',
'price' => '59.00',
'param' => '18', //$this->shopping_order->id,
'reference' => '15c79ba77992e2',
];
if(!isset($data['key']) || !isset($data['param']) || !isset($data['userid']) || !isset($data['txid']) || !isset($data['reference']) || !isset($data['price'])){
\Log::channel('payone')->error('PaymentStatus: parameter incomplete: '.json_encode($data));
abort(404);
}
if($data['key'] != config('payone.defaults.key')) {
\Log::channel('payone')->error('PaymentStatus: Key error: '.json_encode($data));
abort(404);
}
$shopping_order = ShoppingOrder::find($data['param']);
if(!$shopping_order){
\Log::channel('payone')->error('PaymentStatus: ShoppingOrder not found: '.json_encode($data));
abort(404);
}
$shopping_payment = ShoppingPayment::where('reference', $data['reference'])->first();
if(!$shopping_payment){
\Log::channel('payone')->error('PaymentStatus: ShoppingPayment not found: '.json_encode($data));
abort(404);
}
if($shopping_payment->shopping_order_id != $shopping_order->id){
\Log::channel('payone')->error('PaymentStatus: ShoppingPayment no realation ShoppingOrder: '.json_encode($data));
abort(404);
}
if($data['key'] != config('payone.defaults.key')) {
\Log::channel('payone')->error('PaymentStatus: Key error: '.json_encode($data));
abort(404);
}
$price = intval($data['price']*100);
if($shopping_payment->amount != $price){
\Log::channel('payone')->error('PaymentStatus: Price error: '.json_encode($data));
abort(404);
}
//create transaction
PaymentTransaction::create([
'shopping_payment_id' => $shopping_payment->id,
'request' => 'transaction',
'txid' => $data['txid'],
'userid' => $data['userid'],
'status' => 'PAYONE',
'key' => $data['key'],
'txaction' => $data['txaction'],
'transmitted_data' => $data,
]);
$shopping_order->txaction = $data['txaction'];
$shopping_order->save();
$shopping_payment->txaction = $data['txaction'];
$shopping_payment->save();
if($data['txaction'] == 'failed'){
}
if($data['txaction'] == 'paid'){
$shopping_order->paid = true;
$shopping_order->save();
//if product has actions
if($shopping_order->shopping_order_items && $shopping_order->auth_user_id){
foreach($shopping_order->shopping_order_items as $shopping_order_item){
if($shopping_order_item->product){
if($shopping_order_item->product->action){
$user = User::findOrFail($shopping_order->auth_user_id);
foreach ($shopping_order_item->product->action as $do){
if($shopping_order_item->product->getActionName($do) == 'payment_for_account'){
$user->payment_account = date("Y-m-d H:i:s", strtotime("+1 years"));
$user->wizard = 10;
}
if($shopping_order_item->product->getActionName($do) == 'payment_for_shop'){
$user->payment_shop = date("Y-m-d H:i:s", strtotime("+1 years"));
$user->wizard = 10;
}
$user->save();
}
}
}
}
}
}
if($data['txaction'] == 'appointed'){
}
$billing_email = $shopping_order->shopping_user->billing_email;
$user_shop_email = $shopping_order->user_shop->user->email;
if(!$billing_email){
$billing_email = config('app.checkout_mail');
}
$checkout_mail = config('app.checkout_mail');
if($user_shop_email){
Mail::to($billing_email)->bcc([$user_shop_email, $checkout_mail])->send(new MailCheckout($data['txaction'], $shopping_order, $shopping_payment));
}else{
Mail::to($billing_email)->bcc($checkout_mail)->send(new MailCheckout($data['txaction'], $shopping_order, $shopping_payment));
}
die("ok");
}
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;
}
}
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);
}
}