function update_order_status($method_id, $status) { $orders = $this->get_order_id($method_id); if ($orders) { foreach ($orders as $order) { //really only one learn_press_update_order_status($order->ID, $status); return true; } } return false; }
/** * @param null $args * * @return mixed */ function learn_press_add_order($args = null) { //$method, $method_id, $status = 'Pending', $customer_id = false, $transaction_object = false, $args = array() $default_args = array('method' => '', 'method_id' => '', 'status' => '', 'user_id' => null, 'order_id' => 0, 'parent' => 0, 'transaction_object' => false); $args = wp_parse_args($args, $default_args); $order_data = array(); if ($args['order_id'] > 0 && get_post($args['order_id'])) { $updating = true; $order_data['ID'] = $args['order_id']; } else { $updating = false; $order_data['post_type'] = LP()->order_post_type; $order_data['post_status'] = !empty($args['status']) ? 'publish' : 'lpr-draft'; $order_data['ping_status'] = 'closed'; $order_data['post_author'] = ($order_owner_id = learn_press_cart_order_instructor()) ? $order_owner_id : 1; // always is administrator $order_data['post_parent'] = absint($args['parent']); } $order_title = array(); if ($args['method']) { $order_title[] = $args['method']; } if ($args['method_id']) { $order_title[] = $args['method_id']; } $order_title[] = date_i18n('Y-m-d-H:i:s'); $order_data['post_title'] = join('-', $order_title); if (empty($args['user_id'])) { $user = learn_press_get_current_user(); $args['user_id'] = $user->ID; } if (!$args['transaction_object']) { $args['transaction_object'] = learn_press_generate_transaction_object(); } if (!$updating) { if ($transaction_id = wp_insert_post($order_data)) { update_post_meta($transaction_id, '_learn_press_transaction_method', $args['method']); //update_post_meta( $transaction_id, '_learn_press_transaction_status', $status ); update_post_meta($transaction_id, '_learn_press_customer_id', $args['user_id']); update_post_meta($transaction_id, '_learn_press_customer_ip', learn_press_get_ip()); update_post_meta($transaction_id, '_learn_press_order_items', $args['transaction_object']); add_user_meta($args['user_id'], '_lpr_order_id', $transaction_id); } } else { $transaction_id = wp_update_post($order_data); } if ($transaction_id) { if (!empty($args['status'])) { learn_press_update_order_status($transaction_id, $args['status']); } update_post_meta($transaction_id, '_learn_press_transaction_method_id', $args['method_id']); if ($args['transaction_object']) { update_post_meta($transaction_id, '_learn_press_order_items', $args['transaction_object']); } if (!empty($args['status'])) { if ($updating) { return apply_filters('learn_press_update_transaction_success', $transaction_id, $args); } else { return apply_filters('learn_press_add_transaction_success', $transaction_id, $args); } } return $transaction_id; } return false; //do_action( 'learn_press_add_transaction_fail', $args );// $method, $method_id, $status, $customer_id, $transaction_object, $args ); }
3. Check that $_POST['receiver_email'] is your Primary PayPal email 4. Check that $_POST['payment_amount'] and $_POST['payment_currency'] are correct Since implementations on this varies, I will leave these checks out of this example and just send an email using the getTextReport() method to get all of the details about the IPN. */ if (strtoupper($_POST['payment_status']) == 'COMPLETED') { global $wpdb; $course_id = $_POST['item_number']; $params = get_option('_lpr_general_settings', array()); $course_price = get_post_meta($course_id, '_lpr_course_price', true); if ($course_price == $_POST['mc_gross'] && $params['currency'] == $_POST['mc_currency']) { $user_id = $_POST['custom']; learn_press_update_user_course($user_id, $course_id); } $purchased_items = array(); $post_password = substr('lpr_order_' . $_POST['txn_id'], 0, 20); $order_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts}\n\t\t\t\tWHERE post_password=%s", $post_password)); if ($order_id) { learn_press_update_order_status($order_id, 2); } } } else { /* An Invalid IPN *may* be caused by a fraudulent transaction attempt. It's a good idea to have a developer or sys admin manually investigate any invalid IPN. */ }
/** * Action when user press the "Take this course" button * * @param $course_id * @param string $payment_method */ function learn_press_take_course($course_id, $payment_method = '') { $user = learn_press_get_current_user(); $can_take_course = apply_filters('learn_press_before_take_course', true, $user->ID, $course_id, $payment_method); if ($can_take_course) { if (learn_press_is_free_course($course_id)) { if ($order_id = learn_press_add_transaction(array('method' => 'free', 'method_id' => '', 'status' => '', 'user_id' => $user->ID, 'transaction_object' => learn_press_generate_transaction_object()))) { learn_press_update_order_status($order_id, 'Completed'); learn_press_add_message('message', __('Congratulations! You have enrolled this course')); $json = array('result' => 'success', 'redirect' => ($confirm_page_id = learn_press_get_page_id('taken_course_confirm')) && get_post($confirm_page_id) ? learn_press_get_order_confirm_url($order_id) : get_permalink($course_id)); learn_press_send_json($json); } } else { if (has_filter('learn_press_take_course_' . $payment_method)) { $order = null; $result = apply_filters('learn_press_take_course_' . $payment_method, $order); $result = apply_filters('learn_press_payment_result', $result, $order); if (is_ajax()) { learn_press_send_json($result); exit; } else { wp_redirect($result['redirect']); exit; } } else { wp_die(__('Invalid payment method.', 'learn_press')); } } } else { learn_press_add_message('error', __('Sorry! You can not enroll to this course')); $json = array('result' => 'error', 'redirect' => get_permalink($course_id)); echo '<!--LPR_START-->' . json_encode($json) . '<!--LPR_END-->'; } }
function update_status() { $order_id = !empty($_REQUEST['order_id']) ? $_REQUEST['order_id'] : 0; $status = !empty($_REQUEST['status']) ? $_REQUEST['status'] : 'Pending'; learn_press_update_order_status($order_id, $status); wp_send_json(array('status' => $status, 'class' => sanitize_title($status))); }