Вход Регистрация
Файл: monst/Xsolla.php
Строк: 124
<?
//error_reporting(-1);
//ini_set('display_errors', 1);

$HOME $_SERVER['DOCUMENT_ROOT'];
include 
$HOME '/core/in/conf.php';

include 
$HOME '/core/in/fnct.php';
include 
$HOME '/core/in/db.php';

include 
$HOME '/Xsolla/xsolla-autoloader.php';

use 
XsollaSDKWebhookWebhookServer;
use 
XsollaSDKWebhookMessageMessage;
use 
XsollaSDKExceptionWebhookXsollaWebhookException;
use 
XsollaSDKExceptionWebhookInvalidUserException;

$callback = function (Message $message) {
    global 
$HOME$_users$_payment$_not;

    switch (
$message->getNotificationType()) {
        case 
Message::USER_VALIDATION:
            
/** @var XsollaSDKWebhookMessageUserValidationMessage $message */
            // TODO if user not found, you should throw XsollaSDKExceptionWebhookInvalidUserException

            
$name $message -> getUserId();
            
$name null_html($name);

            
$user $_users -> findOne(
                [
                    
'name' => $name
                
]
            );

            if ( !
$user ) {
                throw new 
InvalidUserException('User ID not found');
            }
            break;
        case 
Message::PAYMENT:
            
/** @var XsollaSDKWebhookMessagePaymentMessage $message */
            // TODO if the payment delivery fails for some reason, you should throw XsollaSDKExceptionWebhookXsollaWebhookException

            
$name $message -> getUserId();
            
$name null_html($name);

            
$user $_users -> findOne(
                [
                    
'name' => $name
                
]
            );

            if ( !
$user ) {
                exit;
            }

            
$data $message -> toArray();

            
$xsolla_id $data['transaction']['id'];
            
$gold $data['purchase']['virtual_currency']['quantity'];

            
$gold $gold 2;

//            $sum = $data['payment_details']['payment']['amount'];
//            $currency = $data['payment_details']['payment']['currency'];
//            $payout = $data['payment_details']['payout']['amount'];
//            $method = $data['transaction']['payment_method'];
//            $ip = $data['user']['ip'];
//            $phone = $data['user']['phone'];
//            $email = $data['user']['email'];
//            $country = $data['user']['country'];

            
$payment_id new_id('_payment');

            
$_payment -> insert(
                [
                    
'id'=> $payment_id,
                    
'time' => time(),
                    
'user_id' => $user['id'],
                    
'xsolla_id' => $xsolla_id,
                    
'data' => $data,
                    
'cancel' => 0
                
]
            );

            
$_users -> update(
                [
                    
'id' => $user['id']
                ],

                [
                    
'$set' => [
                        
'gold' => $user['gold'] + $gold
                    
]
                ]
            );


            
// not
            
$not_text '+' $gold ' gold added.<br/>Xsolla';

            
$not_id new_id('_not');
            
$_not -> insert(
                [
                    
'id' => $not_id,
                    
'user_id' => $user['id'],
                    
'time' => time(),
                    
'text' => $not_text
                
]
            );

            
// quest
            
include $HOME '/core/content/daily.php';

            
$daily_db $user['daily'];
            
$daily $DAILY;
            foreach ( 
$daily as $q ) {
                
$daily[$q['id']]['count'] = $daily_db[$q['id']]['count'];
                
$daily[$q['id']]['end'] = $daily_db[$q['id']]['end'];
            }

            foreach ( 
$daily as $q ) {
                if ( 
$q['type'] == 'buy_gold' ) {
                    
$daily_db[$q['id']]['count'] += $gold;

                    
$_users -> update(
                        [
                            
'id' => $user['id']
                        ],

                        [
                            
'$set' => [
                                
'daily' => $daily_db
                            
]
                        ]
                    );

                }
            }

            break;
        case 
Message::REFUND:
            
/** @var XsollaSDKWebhookMessageRefundMessage $message */
            // TODO if you cannot handle the refund, you should throw XsollaSDKExceptionWebhookXsollaWebhookException

                
$name $message -> getUserId();
                
$name null_html($name);

                
$user $_users -> findOne(
                    [
                        
'name' => $name
                    
]
                );

                if ( !
$user ) {
                    exit;
                }

                
$data $message -> toArray();
                
$xsolla_id $data['transaction']['id'];
                
$gold $data['purchase']['virtual_currency']['quantity'];

                
$gold $gold 2;

                
$_payment -> update(
                    [
                        
'xsolla_id'=> $xsolla_id,
                        
'user_id' => $user['id'],
                    ],

                    [
                        
'$set' => [
                            
'cancel' => 1
                        
]
                    ]
                );

                
$_users -> update(
                    [
                        
'id' => $user['id']
                    ],

                    [
                        
'$set' => [
                            
'gold' => $user['gold'] - $gold
                        
]
                    ]
                );

            break;
        default:
            throw new 
XsollaWebhookException('Notification type not implemented');
    }
};

$webhookServer WebhookServer::create($callback$Xsolla['project_key']);
$webhookServer->start();
Онлайн: 0
Реклама