PATH:
home
/
letacommog
/
laindinois
/
OLD
/
wp-content
/
plugins
/
dwt-listing-rest-api
/
classes
/
payments
<?php add_action( 'rest_api_init', 'dwt_listing_payments', 0 ); function dwt_listing_payments() { register_rest_route( 'downtown/app', '/payment/', array( 'methods' => WP_REST_Server::EDITABLE, 'callback' => 'dwt_listing_payment_process', 'permission_callback' => function () { return dwt_listing_basic_auth(); }, ) ); } // Get Listing Form if (!function_exists('dwt_listing_payment_process')) { function dwt_listing_payment_process($request) { global $dwt_listing_api; $user = wp_get_current_user(); $user_id = $user->ID; $new_pack = array(); if ( !is_user_logged_in() ) { return rest_ensure_response(array( 'success' => false, 'data' => '', 'message' => __("You need to logged in.", "dwt-listing-api"))); } $json_data = $request->get_json_params(); $token = (isset($json_data['token'])) ? trim($json_data['token']) : ''; if(!empty($json_data['package_id']) && !empty($json_data['package_type'])) { $product_id = $json_data['package_id']; $package_type = $json_data['package_type']; //for free package if($package_type == 'free') { update_user_meta( $user_id, 'd_user_package_id',$product_id); update_user_meta($user_id, 'd_is_free_pgk',$product_id); dwt_listing_store_user_package($user_id,$product_id); $new_pack['package'] = dwt_listing_check_package_history_app('',$user_id); return rest_ensure_response(array( 'success' => true, 'data' => $new_pack, 'message' => __("Package Activated Successfully.", "dwt-listing-api"))); } else { //paid packages if (false === get_post_status($product_id) ) { return rest_ensure_response(array( 'success' => false, 'data' => '', 'message' => __("Package does not exist.", "dwt-listing-api"))); } else { $args = array(); $product_type = wc_get_product($product_id); if($product_type->get_type() == 'dwt_listing_pkgs') { $source = $json_data['source']; $price = $json_data['price']; $currency = $json_data['currency']; if(!empty($source)) { if($source == 'stripe') { if(isset($dwt_listing_api['stripe_sec_key']) && $dwt_listing_api['stripe_sec_key'] !="" && $token !="") { if(!empty($price) && !empty($currency)) { $stripe_sdk = dwt_listing_API_PLUGIN_FRAMEWORK_PATH . 'inc/stripe-php/init.php'; require_once( $stripe_sdk ); $curl = new \Stripe\HttpClient\CurlClient(array(CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2)); \Stripe\ApiRequestor::setHttpClient($curl); \Stripe\Stripe::setApiKey($dwt_listing_api['stripe_sec_key']); $success = false; $args = array(); try { $final_price = $price * 100; $args = array('source' => $token, 'amount' => $final_price, 'currency' => $currency ); $charge = \Stripe\Charge::create($args); if($charge) { $success = true; dwt_listing_app_orders($product_id , "stripe"); $message = __("You order has been placed.", "dwt-listing-api"); $args['package'] = dwt_listing_check_package_history_app('',$user_id); } } catch(Exception $e) { $message = $e->getMessage(); } return rest_ensure_response(array( 'success' => $success, 'data' => $args, 'message' => $message)); } else { return rest_ensure_response(array( 'success' => false, 'data' => '', 'message' => __("Package price is required.", "dwt-listing-api"))); } } else { return rest_ensure_response(array( 'success' => false, 'data' => '', 'message' => __("Stripe secret or token is required.", "dwt-listing-api"))); } } else if($source == 'paypal') { if(!empty($dwt_listing_api['pay_clientid']) && !empty($dwt_listing_api['pay_secret']) && $token !="") { if(!empty($token)) { $pappal_sdk = dwt_listing_API_PLUGIN_FRAMEWORK_PATH . 'inc/paypal/autoload.php'; require_once($pappal_sdk); $apiContext = new \PayPal\Rest\ApiContext( new \PayPal\Auth\OAuthTokenCredential( $dwt_listing_api['pay_clientid'], // ClientID $dwt_listing_api['pay_secret'] // ClientSecret ) ); $payment = new \PayPal\Api\Payment(); try { $payment::get($token, $apiContext); dwt_listing_app_orders($product_id , "paypal"); $args['package'] = dwt_listing_check_package_history_app('',$user_id); return rest_ensure_response(array( 'success' => true, 'data' =>$args, 'message' => __("You order has been placed.", "dwt-listing-api"))); } catch (\PayPal\Exception\PayPalConnectionException $ex) { return rest_ensure_response(array( 'success' => false, 'data' => $ex->getData(), 'message' => __("Bug Bug Bug.", "dwt-listing-api"))); } } else { return rest_ensure_response(array( 'success' => false, 'data' => '', 'message' => __("Invalid Payment Token.", "dwt-listing-api"))); } } else { return rest_ensure_response(array( 'success' => false, 'data' => '', 'message' => __("Client credentials are missing.", "dwt-listing-api"))); } } else if($source == 'in_app') { dwt_listing_app_orders($product_id , "in_app"); $args['package'] = dwt_listing_check_package_history_app('',$user_id); return rest_ensure_response(array( 'success' => true, 'data' =>$args, 'message' => __("You order has been placed.", "dwt-listing-api"))); } else { return rest_ensure_response(array( 'success' => false, 'data' => '', 'message' => __("Something went wrong please try later.", "dwt-listing-api"))); } } else { return rest_ensure_response(array( 'success' => false, 'data' => '', 'message' => __("Payment source is required.", "dwt-listing-api"))); } } else { return rest_ensure_response(array('success' => false, "message" => esc_html__('Only packages are allowed.', 'dwt-listing-api'))); } } } } else { return rest_ensure_response(array( 'success' => false, 'data' => '', 'message' => __("Package ID is required.", "dwt-listing-api"))); } } } //Store package to woocomerce order if( !function_exists("dwt_listing_app_orders")) { function dwt_listing_app_orders($product_id = '',$payment_method = '') { global $woocommerce; $user = wp_get_current_user(); $user_id = $user->ID; $address = array( 'first_name' => $user->display_name, 'last_name' => '', 'company' => '', 'email' => $user->user_email, 'phone' => get_user_meta($user_id, 'd_user_contact', true ), 'address_1' => get_user_meta($user_id, 'd_user_location', true ), 'address_2' => get_user_meta( $user_id, 'shipping_address_2', true ), 'city' => get_user_meta( $user_id, 'shipping_city', true ), 'state' => get_user_meta( $user_id, 'shipping_state', true ), 'postcode' => get_user_meta( $user_id, 'shipping_postcode', true ), 'country' => get_user_meta( $user_id, 'shipping_country', true ), ); // Now we create the order $order = wc_create_order(array('customer_id'=>$user_id)); $order->add_product(get_product($product_id), 1); // Use the product IDs to add // Set addresses $order->set_address( $address, 'billing' ); $order->set_address( $address, 'shipping' ); // Set payment gateway $payment_gateways = WC()->payment_gateways->payment_gateways(); $order->set_payment_method( $payment_gateways[$payment_method] ); $order->set_payment_method_title($payment_method); // Calculate totals $order->calculate_totals(); //check order approval global $dwt_listing_options; if(isset($dwt_listing_options['wo_pack_approve']) && $dwt_listing_options['wo_pack_approve'] == 1) { $order->update_status( 'completed', 'Order created from Apps - ', TRUE); } else { $order->update_status( 'processing', 'Order created from Apps - ', TRUE); } $count = wc_update_new_customer_past_orders( $user_id ); update_user_meta( $user_id, '_wc_linked_order_count', $count ); return $order; } }
[+]
..
[-] payments.php
[edit]