Personal Payment Gateway at Coursepress + Marketpress

Hi,
I'm creating a new payment gateway at my store, and now running some tests. The gateway I used as sample was eway30, then I created my gateway called Braspag.
So, the payment process seems to be ok, but something isn't right (ok, if it was ok I didn't need to contact you).

My main changes are at the process_payment function, where I use my personal payment gateway and on the tests it seems to be ok. When find some error at the payment process, it calls the function:

$mp->cart_checkout_error( __('', 'mp').' ('.$lblError.')' );

and after that: return false

So, It is working fine.

My question is:
If it has some error at the payment, the course was added to the "Dashboard -> My Courses" page.

What function do I need to call to don't add the course when the payment wasn't aproved?

Thanks

  • Michael Bissett

    Hey @Danilo, hope you're doing well today! :slight_smile:

    If I'm reading you right, what you're saying here is:

    If it has an error when a payment is being made, the user gets subscribed to the course anyway.

    What function do I need to call to prevent a signup when the payment wasn't approved?

    Is that what you're saying there? :slight_smile:

    This sounds like something we'll have to call in one of our developers for, but I'd just like to make sure that I'm understanding you correctly first. :slight_smile:

    Please advise,
    Michael

  • Hoang Ngo

    Hi @Danilo,

    I hope you are well today.

    Usually, the payment gateways will have 2 choices, the url for your client return when payment success and the other is when the payment have errors.

    I can not say much for now, since I will need to check your code, can you please send in:

    - Mark to my attention - ATTN: Hoang Ngo
    - Link back to this thread
    - Include admin/network access
    - Include FTP
    - Include any relevant URLS for your site

    On the contact form, select "I have a different question", this ensures it comes through and gets assigned to me.

    Thanks!!.

    Best regards,
    Hoang Ngo

  • Danilo

    Hi Hoang Ngo, good afternoon.
    My code is working at my localhost system. If there is some way to send you the code to copy my workspace, tell me that I'll send.

    When there is some error at the payment at my gateway, or the payment wasn't aproved, the code calls the function "cart_checkout_error" and returns false.

    Tha page shows the error, as it should be normally, but the customer is enrolled at the course, that isn't ok, because the payment has an error.

    So I did a debug at marketpress.php file inside the plugin marketpress and found this curious thing at (in my code) line 3587.

    <?php
    //wrap the action as it may trigger errors as well
    if($this->checkout_error == false) {
    	do_action( 'mp_payment_confirm_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info'] );
    }
    
    //if no errors send to next checkout step
     if ($this->checkout_error == false) {
    	wp_safe_redirect(mp_checkout_step_url('confirmation'));
    	exit;
    }
    ?>

    So I think that one of the ifs above isn't correct. When the payment shows some error, I noted that the system calls the first if, and shows runs this code:
    do_action( 'mp_payment_confirm_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info'] );

    If you want to reply my workspace, you can download the file with all the coursepress and marketpress (including the payment gateways) at this dropbox link: https://www.dropbox.com/s/r9mjqj923lvno6k/plugins.rar?dl=0 (please, report me when download the files so I can delete all of them).

    If u want to test the payment system, U can use this data at card payment system:
    Name: What do you want;
    Número do Cartão: 0000000000000001 (for test payment sucess) or 0000000000000002 (for denied payment);
    Validade and other fields u can fiel as u want.

    Remember the process:
    Buy a course using Braspag gateway, choose "Cartão de Crédito", fill the fields then go to the end of the checkout. I'll see the error when use the 2 at the end of credit card #, and the code calls "mp_checkout_error", but the customer is enrolled at the course.

    When use the final 1 (payment sucess) already goes as I want.

    Thanks.

  • Danilo

    Guys, please help us a bit fast. We need to make this works and I don't know if it is a bug at my gateway or a bug at coursepress.

    Doing a debug at the code, now I have an specific doubt that you can help me.

    At my gateway, u can see the way we work. The validation of the payment occurs when the customer click: "Confirm Payment".

    So, when the user click there, we validate the credit card data in the function:
    function process_payment($cart, $shipping_info) { that calls the function "cart_checkout_error" when there is some error.

    I saw that the function "cart_checkout_error" set $this->checkout_error = true at marketpress.php code.

    I see 2 problems there:
    1. before the code enters in my function "process_payment" at my gateway, the system calls this action:

    //wrap the action as it may trigger errors as well
    	if($this->checkout_error == false) {
    		do_action( 'mp_payment_confirm_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info'] );
    	}

    And this action enroll the student at the course. But, again: I call the function at my gateway in the process_payment method, that is executed AFTER enroll the student, what makes the error appears at the cart but enroll the student at CoursePress.

    2. The another problem I can see is this piece of code in MarketPress:

    //wrap the action as it may trigger errors as well
    	if($this->checkout_error == false) {
    		do_action( 'mp_payment_confirm_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info'] );
    	}
    
    	//if no errors send to next checkout step
    	 if ($this->checkout_error == false) {
    	 	wp_safe_redirect(mp_checkout_step_url('confirmation'));
    		}

    As you can see, that is 2 ifs with the same condition seems to be some error code, because the system will always execute the first then call the action mp_payment_confirm_.

    Please, help us guys.

    Thanks.

  • Hoang Ngo

    Hi @Danilo,

    I hope you are well today.

    Hmm, so the issue now the student get assigned but in some case, payment not success. From what I see, that function is use to check the quantity in carts valid or not, it's not really sure does the payment success or not.

    For safer now, in CoursePress code /coursepress/coursepress.php, line 386, please change the hook from mp_payment_confirm_' . $gateway to mp_new_order, this will make sure the order has been settled before assign the studen. And in the function enroll_on_payment_confirmation, change it into

    function enroll_on_payment_confirmation( $order_id ) {
                global $mp;
                $order = $mp->get_order($order_id);
                $cart = $order->mp_cart_info;
    
                if ( count($cart) > 0 ) {
                    $product_id = array_keys($cart);
                    $product_id = end($product_id);
    
                    $course_id = get_post_meta($product_id, 'cp_course_id', true);
    
                    if ( !empty($course_id) ) {
                        $student = new Student(get_current_user_id());
                        $existing_student = $student->has_access_to_course($course_id);
                        if ( !$existing_student ) {
                            $student->enroll_in_course($course_id);
                        }
                    }
                } else {
                    cp_write_log('Error in cart. This should not happen.');
                }
            }

    Hope this will help, I also flag the developer of MP here too for his valuable inputs.

    I'm sorry for this inconvenience and many thanks for your patience.

    Best regards,
    Hoang Ngo

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.