Ejemplo n.º 1
0
 function wpestate_ajax_paypal_pack_generation()
 {
     $allowed_html = array();
     $packName = wp_kses($_POST['packName'], $allowed_html);
     $pack_id = $_POST['packId'];
     if (!is_numeric($pack_id)) {
         exit;
     }
     $is_pack = get_posts('post_type=membership_package&p=' . $pack_id);
     if (!empty($is_pack)) {
         global $current_user;
         get_currentuserinfo();
         $pack_price = get_post_meta($pack_id, 'pack_price', true);
         $submission_curency_status = esc_html(get_option('wp_estate_submission_curency', ''));
         $paypal_status = esc_html(get_option('wp_estate_paypal_api', ''));
         $host = 'https://api.sandbox.paypal.com';
         if ($paypal_status == 'live') {
             $host = 'https://api.paypal.com';
         }
         $url = $host . '/v1/oauth2/token';
         $postArgs = 'grant_type=client_credentials';
         $token = wpestate_get_access_token($url, $postArgs);
         $url = $host . '/v1/payments/payment';
         $dash_profile_link = wpestate_get_dashboard_profile_link();
         $payment = array('intent' => 'sale', "redirect_urls" => array("return_url" => $dash_profile_link, "cancel_url" => $dash_profile_link), 'payer' => array("payment_method" => "paypal"));
         $payment['transactions'][0] = array('amount' => array('total' => $pack_price, 'currency' => $submission_curency_status, 'details' => array('subtotal' => $pack_price, 'tax' => '0.00', 'shipping' => '0.00')), 'description' => $packName . ' ' . esc_html__('membership payment on ', 'wpestate') . esc_html(home_url()));
         //
         // prepare individual items
         $payment['transactions'][0]['item_list']['items'][] = array('quantity' => '1', 'name' => esc_html__('Membership Payment', 'wpestate'), 'price' => $pack_price, 'currency' => $submission_curency_status, 'sku' => $packName . ' ' . esc_html__('Membership Payment', 'wpestate'));
         $json = json_encode($payment);
         $json_resp = wpestate_make_post_call($url, $json, $token);
         foreach ($json_resp['links'] as $link) {
             if ($link['rel'] == 'execute') {
                 $payment_execute_url = $link['href'];
                 $payment_execute_method = $link['method'];
             } else {
                 if ($link['rel'] == 'approval_url') {
                     $payment_approval_url = $link['href'];
                     $payment_approval_method = $link['method'];
                 }
             }
         }
         $executor['paypal_execute'] = $payment_execute_url;
         $executor['paypal_token'] = $token;
         $executor['pack_id'] = $pack_id;
         $save_data[$current_user->ID] = $executor;
         update_option('paypal_pack_transfer', $save_data);
         print $payment_approval_url;
     }
     die;
 }
 $token_recursive = wp_kses($_GET['token'], $allowed_html);
 // get transfer data
 $save_data = get_option('paypal_pack_transfer');
 $payment_execute_url = $save_data[$current_user->ID]['paypal_execute'];
 $token = $save_data[$current_user->ID]['paypal_token'];
 $pack_id = $save_data[$current_user->ID]['pack_id'];
 $recursive = 0;
 if (isset($save_data[$current_user->ID]['recursive'])) {
     $recursive = $save_data[$current_user->ID]['recursive'];
 }
 if ($recursive != 1) {
     if (isset($_GET['PayerID'])) {
         $payerId = wp_kses($_GET['PayerID'], $allowed_html);
         $payment_execute = array('payer_id' => $payerId);
         $json = json_encode($payment_execute);
         $json_resp = wpestate_make_post_call($payment_execute_url, $json, $token);
         $save_data[$current_user->ID] = array();
         update_option('paypal_pack_transfer', $save_data);
         if ($json_resp['state'] == 'approved') {
             if (wpestate_check_downgrade_situation($current_user->ID, $pack_id)) {
                 wpestate_downgrade_to_pack($current_user->ID, $pack_id);
                 wpestate_upgrade_user_membership($current_user->ID, $pack_id, 1, '');
             } else {
                 wpestate_upgrade_user_membership($current_user->ID, $pack_id, 1, '');
             }
             wp_redirect($dash_profile_link);
         }
     }
     //end if Get
 } else {
     require get_template_directory() . '/libs/resources/paypalfunctions.php';