mivita/app/Http/Controllers/Api/PayoneController.php
2020-08-07 16:02:03 +02:00

214 lines
No EOL
8.5 KiB
PHP
Executable file

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Mail\MailCheckout;
use App\Models\PaymentTransaction;
use App\Models\ShoppingOrder;
use App\Models\ShoppingPayment;
use App\Services\Shop;
use App\Services\Util;
use App\User;
use Illuminate\Support\Facades\Mail;
class PayoneController extends Controller
{
public function __construct()
{
}
public function paymentStatus(){
$data = \Request::all();
// test para
/* $data = [
'key' => '698fb2555f8b2efc74f60b2121421f45',
'txaction' => 'paid',
'clearingtype' => 'wlt',
'userid' => '158723953',
'txid' => '321623031',
'price' => '89.00',
'param' => '1', //$this->shopping_order->id,
'reference' => '15c83aee2766c3',
];
*/
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));
echo "PaymentStatus: parameter incomplete:";
var_dump($data);
die();
}
if($data['key'] != config('payone.defaults.key')) {
\Log::channel('payone')->error('PaymentStatus: Key error: '.json_encode($data));
echo "PaymentStatus: Key error:";
var_dump($data);
die();
}
$shopping_order = ShoppingOrder::find($data['param']);
if(!$shopping_order){
\Log::channel('payone')->error('PaymentStatus: ShoppingOrder not found: '.json_encode($data));
echo "PaymentStatus: ShoppingOrder not found:";
var_dump($data);
die();
}
$shopping_payment = ShoppingPayment::where('reference', $data['reference'])->first();
if(!$shopping_payment){
\Log::channel('payone')->error('PaymentStatus: ShoppingPayment not found: '.json_encode($data));
echo "PaymentStatus: ShoppingPayment not found:";
var_dump($data);
die();
}
if($shopping_payment->shopping_order_id != $shopping_order->id){
\Log::channel('payone')->error('PaymentStatus: ShoppingPayment no realation ShoppingOrder: '.json_encode($data));
echo "PaymentStatus: ShoppingPayment no realation ShoppingOrder:";
var_dump($data);
die();
}
if($data['key'] != config('payone.defaults.key')) {
\Log::channel('payone')->error('PaymentStatus: Key error: '.json_encode($data));
echo "PaymentStatus: ShoppingPayment no realation ShoppingOrder:";
var_dump($data);
die();
}
$price = number_format((round($data['price'],2) * 100), 0, '.', '');
$price_amount = number_format($shopping_payment->amount, 0, '.', '');
if($price_amount != $price){
$data['shopping_payment-amount'] = $price_amount;
$data['price-amount'] = $price;
\Log::channel('payone')->error('PaymentStatus: Price error: '.json_encode($data));
echo "PaymentStatus: Price error:";
var_dump($data);
die();
}
/* TODO -- need this?
if($shopping_payment->txaction == $data['txaction']){
\Log::channel('payone')->error('PaymentStatus: same txaction error: '.json_encode($data));
echo "PaymentStatus: same txaction:";
var_dump($data);
die();
}
*/
//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' => Util::utf8ize($data),
'mode' => $data['mode'],
]);
$shopping_order->txaction = $data['txaction'];
$shopping_order->save();
$shopping_payment->txaction = $data['txaction'];
$shopping_payment->save();
$send_link = false;
if($data['txaction'] === 'failed'){
$shopping_order->setUserHistoryValue(['status' => 6]);
}
if($data['txaction'] === 'appointed'){
$shopping_order->setUserHistoryValue(['status' => 7]);
Shop::userOrders();
}
if($data['txaction'] === 'paid'){
$shopping_order->setUserHistoryValue(['status' => 8]);
Shop::userOrders();
$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);
$user->save();
$send_link = true;
//new date
$date = \Carbon::now()->modify('1 year');
if($user->payment_account && $user->daysActiveAccount()>0){
$date = \Carbon::parse($user->payment_account)->modify('1 year');
}
foreach ($shopping_order_item->product->action as $do){
if($shopping_order_item->product->getActionName($do) === 'payment_for_account'){
$user->payment_order_id = $shopping_order_item->product->id; //34
$user->payment_account = $date;
$user->wizard = 100;
$shopping_order->setUserHistoryValue(['status' => 9]);
}
if($shopping_order_item->product->getActionName($do) === 'payment_for_shop'){
$user->payment_order_id = $shopping_order_item->product->id; //35
$user->payment_shop = $date;
$user->wizard = 100;
$shopping_order->setUserHistoryValue(['status' => 9]);
}
if($shopping_order_item->product->getActionName($do) === 'payment_for_shop_upgrade'){
if($shopping_order_item->product->upgrade_to_id){
$user->payment_order_id = $shopping_order_item->product->upgrade_to_id;
}
$user->payment_shop = $user->payment_account; //same Date, is upgrade
$shopping_order->setUserHistoryValue(['status' => 9]);
}
if($shopping_order_item->product->getActionName($do) === 'payment_for_lead_upgrade'){
if($shopping_order_item->product->upgrade_to_id){
$user->m_level = $shopping_order_item->product->upgrade_to_id;
}
}
$user->save();
}
}
}
}
}
}
$bcc = [];
$billing_email = $shopping_order->shopping_user->billing_email;
if(!$billing_email){
if($data['mode'] === 'test'){
$billing_email = config('app.checkout_test_mail');
}else{
$billing_email = config('app.checkout_mail');
}
}
if($data['mode'] === 'test'){
$bcc[] = config('app.checkout_test_mail');
}else{
$bcc[] = config('app.checkout_mail');
}
if(!$shopping_order->shopping_user->is_like && $shopping_order->shopping_user->member){
$bcc[] = $shopping_order->shopping_user->member->email;
}
Mail::to($billing_email)->bcc($bcc)->send(new MailCheckout($data['txaction'], $shopping_order, $shopping_payment, $send_link, $data['mode']));
print("TSOK");
exit;
}
}