Example #1
0
 /**
  * Successful Payment!
  **/
 function successful_request($posted)
 {
     // Custom holds post ID
     if (!empty($posted['custom']) && !empty($posted['invoice'])) {
         $order = new woocommerce_order((int) $posted['custom']);
         if ($order->order_key !== $posted['invoice']) {
             exit;
         }
         // Sandbox fix
         if ($posted['test_ipn'] == 1 && $posted['payment_status'] == 'Pending') {
             $posted['payment_status'] = 'completed';
         }
         // We are here so lets check status and do actions
         switch (strtolower($posted['payment_status'])) {
             case 'completed':
                 // Check order not already completed
                 if ($order->status == 'completed') {
                     exit;
                 }
                 // Check valid txn_type
                 $accepted_types = array('cart', 'instant', 'express_checkout', 'web_accept', 'masspay', 'send_money');
                 if (!in_array(strtolower($posted['txn_type']), $accepted_types)) {
                     exit;
                 }
                 // Payment completed
                 $order->add_order_note(__('IPN payment completed', 'woothemes'));
                 $order->payment_complete();
                 // Store PP Details
                 update_post_meta((int) $posted['custom'], 'Payer PayPal address', $posted['payer_email']);
                 update_post_meta((int) $posted['custom'], 'Transaction ID', $posted['txn_id']);
                 update_post_meta((int) $posted['custom'], 'Payer first name', $posted['first_name']);
                 update_post_meta((int) $posted['custom'], 'Payer last name', $posted['last_name']);
                 update_post_meta((int) $posted['custom'], 'Payment type', $posted['payment_type']);
                 break;
             case 'denied':
             case 'expired':
             case 'failed':
             case 'voided':
                 // Order failed
                 $order->update_status('failed', sprintf(__('Payment %s via IPN.', 'woothemes'), strtolower($posted['payment_status'])));
                 break;
             case "refunded":
             case "reversed":
             case "chargeback":
                 // Mark order as refunded
                 $order->update_status('refunded', sprintf(__('Payment %s via IPN.', 'woothemes'), strtolower($posted['payment_status'])));
                 $message = woocommerce_mail_template(__('Order refunded/reversed', 'woothemes'), sprintf(__('Order #%s has been marked as refunded - PayPal reason code: %s', 'woothemes'), $order->id, $posted['reason_code']));
                 // Send the mail
                 woocommerce_mail(get_option('woocommerce_new_order_email_recipient'), sprintf(__('Payment for order #%s refunded/reversed', 'woothemes'), $order->id), $message);
                 break;
             default:
                 // No action
                 break;
         }
         exit;
     }
 }
 /**
  * Check for valid paytm server callback // response processing //
  **/
 function check_paytm_response()
 {
     global $woocommerce;
     if (isset($_POST['ORDERID']) && isset($_POST['RESPCODE'])) {
         $order_sent = $_POST['ORDERID'];
         $responseDescription = $_POST['RESPMSG'];
         if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) {
             $order = new WC_Order($_POST['ORDERID']);
         } else {
             $order = new woocommerce_order($_POST['ORDERID']);
         }
         if ($this->log == "yes") {
             error_log("Response Code = " . $_POST['RESPCODE']);
         }
         $redirect_url = $this->redirect_page_id == "" || $this->redirect_page_id == 0 ? get_site_url() . "/" : get_permalink($this->redirect_page_id);
         $this->msg['class'] = 'error';
         $this->msg['message'] = "Thank you for shopping with us. However, the transaction has been Failed For Reason  : " . $responseDescription;
         if ($_POST['RESPCODE'] == 01) {
             // success
             $order_amount = $order->order_total;
             if ($_POST['TXNAMOUNT'] == $order_amount) {
                 if ($this->log == "yes") {
                     error_log("amount matched");
                 }
                 // code by paytm team
                 $order_sent = $_POST['ORDERID'];
                 $res_code = $_POST['RESPCODE'];
                 $responseDescription = $_POST['RESPMSG'];
                 $checksum_recv = $_POST['CHECKSUMHASH'];
                 $paramList = $_POST;
                 $order_amount = $_POST['TXNAMOUNT'];
                 //  code by paytm team
                 $all = $paramList;
                 if ($this->log == "yes") {
                     error_log("received parameters = " . $all);
                 }
                 $bool = "FALSE";
                 $bool = verifychecksum_e($paramList, $this->secret_key, $checksum_recv);
                 //$newcheck = Checksum::calculateChecksum($this->secret_key, $all);
                 if ($this->log == "yes") {
                     error_log("calculated checksum = " . $newch . " and checksum received = " . $_POST['CHECKSUMHASH']);
                 }
                 if ($bool == "TRUE") {
                     if ($order->status !== 'completed') {
                         error_log("SUCCESS");
                         $this->msg['message'] = "Thank you for your order . Your transaction has been successful.";
                         $this->msg['class'] = 'success';
                         if ($order->status == 'processing') {
                         } else {
                             $order->payment_complete();
                             $order->add_order_note('Mobile Wallet payment successful');
                             $order->add_order_note($this->msg['message']);
                             $woocommerce->cart->empty_cart();
                         }
                     }
                 } else {
                     // server to server failed while call//
                     //error_log("api process failed");
                     $this->msg['class'] = 'error';
                     $this->msg['message'] = "Severe Error Occur.";
                     $order->update_status('failed');
                     $order->add_order_note('Failed');
                     $order->add_order_note($this->msg['message']);
                 }
             } else {
                 // Order mismatch occur //
                 //error_log("order mismatch");
                 $this->msg['class'] = 'error';
                 $this->msg['message'] = "Order Mismatch Occur";
                 $order->update_status('failed');
                 $order->add_order_note('Failed');
                 $order->add_order_note($this->msg['message']);
             }
         } else {
             $order->update_status('failed');
             $order->add_order_note('Failed');
             $order->add_order_note($responseDescription);
             $order->add_order_note($this->msg['message']);
         }
         add_action('the_content', array(&$this, 'paytmShowMessage'));
         $redirect_url = $this->redirect_page_id == "" || $this->redirect_page_id == 0 ? get_site_url() . "/" : get_permalink($this->redirect_page_id);
         //For wooCoomerce 2.0
         $redirect_url = add_query_arg(array('msg' => urlencode($this->msg['message']), 'type' => $this->msg['class']), $redirect_url);
         wp_redirect($redirect_url);
         exit;
     }
 }
Example #3
0
/**
 * Outputs the pay page - payment gateways can hook in here to show payment forms etc
 **/
function woocommerce_pay()
{
    global $woocommerce, $order;
    if (isset($_GET['pay_for_order']) && isset($_GET['order']) && isset($_GET['order_id'])) {
        // Pay for existing order
        $order_key = urldecode($_GET['order']);
        $order_id = (int) $_GET['order_id'];
        $order = new woocommerce_order($order_id);
        if ($order->id == $order_id && $order->order_key == $order_key && in_array($order->status, array('pending', 'failed'))) {
            // Set customer location to order location
            if ($order->billing_country) {
                $woocommerce->customer->set_country($order->billing_country);
            }
            if ($order->billing_state) {
                $woocommerce->customer->set_state($order->billing_state);
            }
            if ($order->billing_postcode) {
                $woocommerce->customer->set_postcode($order->billing_postcode);
            }
            // Pay form was posted - process payment
            if (isset($_POST['pay']) && $woocommerce->verify_nonce('pay')) {
                // Update payment method
                if ($order->order_total > 0) {
                    $payment_method = woocommerce_clean($_POST['payment_method']);
                    $available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
                    // Update meta
                    update_post_meta($order_id, '_payment_method', $payment_method);
                    if (isset($available_gateways) && isset($available_gateways[$payment_method])) {
                        $payment_method_title = $available_gateways[$payment_method]->title;
                    }
                    update_post_meta($order_id, '_payment_method_title', $payment_method_title);
                    $result = $available_gateways[$payment_method]->process_payment($order_id);
                    // Redirect to success/confirmation/payment page
                    if ($result['result'] == 'success') {
                        wp_redirect($result['redirect']);
                        exit;
                    }
                } else {
                    // No payment was required for order
                    $order->payment_complete();
                    wp_safe_redirect(get_permalink(get_option('woocommerce_thanks_page_id')));
                    exit;
                }
            }
            // Show form
            woocommerce_get_template('checkout/pay_for_order.php');
        } elseif (!in_array($order->status, array('pending', 'failed'))) {
            $woocommerce->add_error(__('Your order has already been paid for. Please contact us if you need assistance.', 'woothemes'));
            $woocommerce->show_messages();
        } else {
            $woocommerce->add_error(__('Invalid order.', 'woothemes'));
            $woocommerce->show_messages();
        }
    } else {
        // Pay for order after checkout step
        if (isset($_GET['order'])) {
            $order_id = $_GET['order'];
        } else {
            $order_id = 0;
        }
        if (isset($_GET['key'])) {
            $order_key = $_GET['key'];
        } else {
            $order_key = '';
        }
        if ($order_id > 0) {
            $order = new woocommerce_order($order_id);
            if ($order->order_key == $order_key && in_array($order->status, array('pending', 'failed'))) {
                ?>
				<ul class="order_details">
					<li class="order">
						<?php 
                _e('Order:', 'woothemes');
                ?>
						<strong># <?php 
                echo $order->id;
                ?>
</strong>
					</li>
					<li class="date">
						<?php 
                _e('Date:', 'woothemes');
                ?>
						<strong><?php 
                echo date(get_option('date_format'), strtotime($order->order_date));
                ?>
</strong>
					</li>
					<li class="total">
						<?php 
                _e('Total:', 'woothemes');
                ?>
						<strong><?php 
                echo woocommerce_price($order->order_total);
                ?>
</strong>
					</li>
					<li class="method">
						<?php 
                _e('Payment method:', 'woothemes');
                ?>
						<strong><?php 
                echo $order->payment_method_title;
                ?>
</strong>
					</li>
				</ul>
				
				<?php 
                do_action('woocommerce_receipt_' . $order->payment_method, $order_id);
                ?>
				
				<div class="clear"></div>
				<?php 
            } else {
                wp_safe_redirect(get_permalink(get_option('woocommerce_myaccount_page_id')));
                exit;
            }
        } else {
            wp_safe_redirect(get_permalink(get_option('woocommerce_myaccount_page_id')));
            exit;
        }
    }
}