Cash on Delivery

Hi,
I have notice that in marketpress there isn't a Cash on Delivery method of Payment.
I search on the forum but I don't find what I need.
First of all, I need on my website 3 payment: Manual, PayPal and Cash on Delivery.
So for the Manual Payment and PayPal I don't have problem, but for the Cash on Delivery I create a new method of payment.
I copy the manual-payments.php file into plugins-gateway folder and change the name in cod-cashondelivery.php, also change the all the name inside (was "manual-payments":wink:.
But it's not all... I also need to add an extra cost if the customers choose the Cash on Delivery.
I create a input box where the admin can insert the ammount value of the extra cost, but now there is the problem: how I can add the value to the total ammount of the order?
This is the extra field that I add into cod-cashondelivery.php:

<tr>
					<th scope=&quot;row&quot;><label for=&quot;cash-on-delivery-cost&quot;><?php _e('Cost Value', 'mp') ?></label></th>
				<td>
				<span class=&quot;description&quot;><?php _e('Enter the cost of the COD Cash on Delivery', 'mp') ?></span>
		        	<p>

		        	<?php echo $mp->format_currency(); ?><input type=&quot;text&quot; name=&quot;mp[gateways][cash-on-delivery][cost]&quot; value=&quot;<?php echo esc_attr($settings['gateways']['cash-on-delivery']['cost']); ?>&quot; size=&quot;5&quot; maxlength=&quot;10&quot; />

		        	</p>
		   		</td>

	        	</tr>

If you need I can send the file that I have created.
Thank you in advance.
Best regards

  • imagenow

    Hi Timothy,
    I want to fix the problem that I have with the additional cost, but I have no problem to upload the file to the forums.
    So, I have update the code, but now I need to create the "if" for the Cash on Delivery adding cost:
    on the tempate-functions.php after the 355 line:

    //tax line
        if ( $tax_price = array_sum($tax_prices) ) {
          $content .=  '<tr>';
          $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __('Taxes:', 'mp') . '</td>';
          $content .=  '  <td class="mp_cart_col_tax">' . $mp->format_currency('', $tax_price) . '</td>';
          $content .=  '</tr>';
          $total = $total + $tax_price;
        }

    I need to add the "if" condition fot the adding price:

    //cash on delivery line
        $cod_price = $settings['gateways']['cash-on-delivery']['cost'];
    
    	if ( ) {
          $content .=  '<tr>';
          $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __('COD:', 'mp') . '</td>';
          $content .=  '  <td class="mp_cart_col_tax">' . $mp->format_currency('', $cod_price) . '</td>';
          $content .=  '</tr>';
          $total = $total + $cod_price;
        }

    At now, the code works and add the additional value that the admin set on the administration side, but I miss the final step to only show this option only when the customers choose the Cash on Delivery method of Payment.

    I add the php file of the Cash on Delivery if someone want try to help me.
    Thank you in advance.

  • imagenow

    I can't upload, so this is my Cash on Delivery php code, copy and paste it into a blank .php file:

    <?php
    /*
    MarketPress COD Cash on Delivery Plugin
    */
    class MP_Gateway_CashonDelivery extends MP_Gateway_API {
    
      //private gateway slug. Lowercase alpha (a-z) and dashes (-) only please!
      var $plugin_name = 'cash-on-delivery';
    
      //name of your gateway, for the admin side.
      var $admin_name = '';
    
      //public name of your gateway, for lists and such.
      var $public_name = '';
    
      //url for an image for your checkout method. Displayed on method form
      var $method_img_url = '';
    
      //url for an submit button image for your checkout method. Displayed on checkout form if set
      var $method_button_img_url = '';
    
      //whether or not ssl is needed for checkout page
      var $force_ssl = false;
    
      //always contains the url to send payment notifications to if needed by your gateway. Populated by the parent class
      var $ipn_url;
    
    	//whether if this is the only enabled gateway it can skip the payment_form step
      var $skip_form = false;
    
      /****** Below are the public methods you may overwrite via a plugin ******/
    
      /**
       * Runs when your class is instantiated. Use to setup your plugin instead of __construct()
       */
      function on_creation() {
    		global $mp;
    		$settings = get_option('mp_settings');
    
    		//set names here to be able to translate
    		$this->admin_name = __('Cash on Delivery', 'mp');
    		$this->public_name = (isset($settings['gateways']['cash-on-delivery']['name'])) ? $settings['gateways']['cash-on-delivery']['name'] : __('Cash on Delivery', 'mp');
    
        $this->method_img_url = $mp->plugin_url . 'images/manual-payment.png';
    	}
    
      /**
       * Return fields you need to add to the payment screen, like your credit card info fields
       *
       * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
       * @param array $shipping_info. Contains shipping info and email in case you need it
       */
      function payment_form($cart, $shipping_info) {
        global $mp;
        $settings = get_option('mp_settings');
        return $settings['gateways']['cash-on-delivery']['instructions'];
      }
    
      /**
       * Use this to process any fields you added. Use the $_POST global,
       *  and be sure to save it to both the $_SESSION and usermeta if logged in.
       *  DO NOT save credit card details to usermeta as it's not PCI compliant.
       *  Call $mp->cart_checkout_error($msg, $context); to handle errors. If no errors
       *  it will redirect to the next step.
       *
       * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
       * @param array $shipping_info. Contains shipping info and email in case you need it
       */
    	function process_payment_form($cart, $shipping_info) {
    		global $mp;
      }
    
      /**
       * Return the chosen payment details here for final confirmation. You probably don't need
       *  to post anything in the form as it should be in your $_SESSION var already.
       *
       * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
       * @param array $shipping_info. Contains shipping info and email in case you need it
       */
    	function confirm_payment_form($cart, $shipping_info) {
      	global $mp;
      }
    
      /**
       * Use this to do the final payment. Create the order then process the payment. If
       *  you know the payment is successful right away go ahead and change the order status
       *  as well.
       *  Call $mp->cart_checkout_error($msg, $context); to handle errors. If no errors
       *  it will redirect to the next step.
       *
       * @param array $cart. Contains the cart contents for the current blog, global cart if $mp->global_cart is true
       * @param array $shipping_info. Contains shipping info and email in case you need it
       */
    	function process_payment($cart, $shipping_info) {
    	  global $mp;
        $settings = get_option('mp_settings');
    	  $timestamp = time();
    
        $totals = array();
        foreach ($cart as $product_id => $variations) {
    			foreach ($variations as $data) {
          	$totals[] = $mp->before_tax_price($data['price'], $product_id) * $data['quantity'];
          }
        }
        $total = array_sum($totals);
    
    	  if ( $coupon = $mp->coupon_value($mp->get_coupon_code(), $total) ) {
    	    $total = $coupon['new_total'];
    	  }
    
    	  //shipping line
    
    	  if ( ($shipping_price = $mp->shipping_price()) !== false ) {
    	    $total = $total + $shipping_price;
    	  }
    
    	  //tax line
    	  if ( ($tax_price = $mp->tax_price()) !== false ) {
    	    $total = $total + $tax_price;
    	  }
    
    	  //cash on delivery line
    	  if ( ($cod_price = $mp->cod_price()) !== false ) {
    	    $total = $total + $cod_price;
    	  }
    
    		$order_id = $mp->generate_order_id();
    
    	//Cash on Delivery COST
    	$cod_price = $settings['gateways']['cash-on-delivery']['cost'];
    
        $payment_info['gateway_public_name'] = $this->public_name;
        $payment_info['gateway_private_name'] = $this->admin_name;
        $payment_info['status'][$timestamp] = __('Invoiced', 'mp');
        $payment_info['total'] = $total;
        $payment_info['currency'] = $settings['currency'];
    	  $payment_info['method'] = __('Manual/Invoice', 'mp');
    	  //$payment_info['transaction_id'] = $order_id;
    
        //create our order now
        $result = $mp->create_order($order_id, $cart, $shipping_info, $payment_info, false);
      }
    
      /**
       * Runs before page load incase you need to run any scripts before loading the success message page
       */
    	function order_confirmation($order) {
      }
    	/**
       * Filters the order confirmation email message body. You may want to append something to
       *  the message. Optional
       *
       * Don't forget to return!
       */
    	function order_confirmation_email($msg, $order) {
        global $mp;
    		$settings = get_option('mp_settings');
    
    	  if (isset($settings['gateways']['cash-on-delivery']['email']))
    		  $msg = $mp->filter_email($order, $settings['gateways']['cash-on-delivery']['email']);
    		else
    		  $msg = $settings['email']['new_order_txt'];
    
        return $msg;
      }
    
      /**
       * Return any html you want to show on the confirmation screen after checkout. This
       *  should be a payment details box and message.
       *
       * Don't forget to return!
       */
    	function order_confirmation_msg($content, $order) {
        global $mp;
        $settings = get_option('mp_settings');
    
        return $content . str_replace( 'TOTAL', $mp->format_currency($order->mp_payment_info['currency'], $order->mp_payment_info['total']), $settings['gateways']['cash-on-delivery']['confirmation'] );
      }
    
    	/**
       * Echo a settings meta box with whatever settings you need for you gateway.
       *  Form field names should be prefixed with mp[gateways][plugin_name], like "mp[gateways][plugin_name][mysetting]".
       *  You can access saved settings via $settings array.
       */
    	function gateway_settings_box($settings) {
        global $mp;
        $settings = get_option('mp_settings');
    		if (!isset($settings['gateways']['cash-on-delivery']['name']))
    		  $settings['gateways']['cash-on-delivery']['name'] = __('Manual Payment', 'mp');
    
    		if (!isset($settings['gateways']['cash-on-delivery']['email']))
    		  $settings['gateways']['cash-on-delivery']['email'] = $settings['email']['new_order_txt'];
    
        ?>
        <div id="mp_manual_payments" class="postbox mp-pages-msgs">
        	<h3 class='handle'><span><?php _e('Cash on Delivery Settings', 'mp'); ?></span></h3>
          <div class="inside">
    	      <span class="description"><?php _e('Record payments manually, such as by Cash, Check, or EFT.', 'mp') ?></span>
    	      <table class="form-table">
    
    		      <tr>
    						<th scope="row"><label for="cash-on-delivery-name"><?php _e('Method Name', 'mp') ?></label></th>
    						<td>
    		  				<span class="description"><?php _e('Enter a public name for this payment method that is displayed to users - No HTML', 'mp') ?></span>
    		          <p>
    		          <input value="<?php echo esc_attr($settings['gateways']['cash-on-delivery']['name']); ?>" style="width: 100%;" name="mp[gateways][cash-on-delivery][name]" id="cash-on-delivery-name" type="text" />
    		          </p>
    		        </td>
    
    	          </tr>
    
    		    	<tr>
    					<th scope="row"><label for="cash-on-delivery-cost"><?php _e('Cost Value', 'mp') ?></label></th>
    				<td>
    				<span class="description"><?php _e('Enter the cost of the COD Cash on Delivery', 'mp') ?></span>
    		        	<p>
    
    		        	<?php echo $mp->format_currency(); ?><input type="text" name="mp[gateways][cash-on-delivery][cost]" value="<?php echo esc_attr($settings['gateways']['cash-on-delivery']['cost']); ?>" size="5" maxlength="10" />
    		        	</p>
    		   		</td>
    
    	        	</tr>
    		      <tr>
    		        <th scope="row"><label for="cash-on-delivery-instructions"><?php _e('User Instructions', 'mp') ?></label></th>
    		        <td>
    		        <span class="description"><?php _e('These are the manual payment instructions to display on the payments screen - HTML allowed', 'mp') ?></span>
    	          <p>
    							<?php wp_editor( $settings['gateways']['cash-on-delivery']['instructions'], 'cashondeliveryinstructions', array('textarea_name'=>'mp[gateways][cash-on-delivery][instructions]') ); ?>
    						</p>
    	        	</td>
    	        </tr>
    	        <tr>
    		        <th scope="row"><label for="cash-on-delivery-confirmation"><?php _e('Confirmation User Instructions', 'mp') ?></label></th>
    		        <td>
    		        <span class="description"><?php _e('These are the manual payment instructions to display on the order confirmation screen. TOTAL will be replaced with the order total. - HTML allowed', 'mp') ?></span>
    	          <?php wp_editor( $settings['gateways']['cash-on-delivery']['confirmation'], 'cashondeliveryconfirmation', array('textarea_name'=>'mp[gateways][cash-on-delivery][confirmation]') ); ?>
    	        	</td>
    	        </tr>
    	        <tr>
    		        <th scope="row"><label for="cash-on-delivery-email"><?php _e('Order Confirmation Email', 'mp') ?></label></th>
    		        <td>
    		        <span class="description"><?php _e('This is the email text to send to those who have made manual payment checkouts. You should include your manual payment instructions here. It overrides the default order checkout email. These codes will be replaced with order details: CUSTOMERNAME, ORDERID, ORDERINFO, SHIPPINGINFO, PAYMENTINFO, TOTAL, TRACKINGURL. No HTML allowed.', 'mp') ?></span>
    	          <p>
    	            <textarea id="cash-on-delivery-email" name="mp[gateways][cash-on-delivery][email]" class="mp_emails_txt"><?php echo esc_textarea($settings['gateways']['cash-on-delivery']['email']); ?></textarea>
    	          </p>
    	        	</td>
    	        </tr>
          	</table>
          </div>
        </div>
        <?php
      }
    
      /**
       * Filters posted data from your settings form. Do anything you need to the $settings['gateways']['plugin_name']
       *  array. Don't forget to return!
       */
    	function process_gateway_settings($settings) {
    
    		//strip slashes
        $settings['gateways']['cash-on-delivery'] = array_map('stripslashes', $settings['gateways']['cash-on-delivery']);
    
    		//no html in public name
      	$settings['gateways']['cash-on-delivery']['name'] = wp_filter_nohtml_kses($settings['gateways']['cash-on-delivery']['name']);
    
    		//filter html if needed
    		if (!current_user_can('unfiltered_html')) {
    			$settings['gateways']['cash-on-delivery']['instructions'] = filter_post_kses($settings['gateways']['cash-on-delivery']['instructions']);
    			$settings['gateways']['cash-on-delivery']['confirmation'] = filter_post_kses($settings['gateways']['cash-on-delivery']['confirmation']);
    		}
    
    		//no html in email
      	$settings['gateways']['cash-on-delivery']['email'] = wp_filter_nohtml_kses($settings['gateways']['cash-on-delivery']['email']);	
    
        return $settings;
      }
    
    	/**
       * Use to handle any payment returns to the ipn_url. Do not display anything here. If you encounter errors
       *  return the proper headers. Exits after.
       */
    	function process_ipn_return() {
    
      }
    }
    
    mp_register_gateway_plugin( 'MP_Gateway_CashonDelivery', 'cash-on-delivery', __('Cash on Delivery', 'mp') );
    ?>
  • imagenow

    IMPORTANT:
    The code is not finish, I have problem with the Confirm Order button, the page return blank.
    Anyone can help me?
    I fix the problem about adding cost:
    On template-functions.php after this code:

    //tax line
        if ( $tax_price = array_sum($tax_prices) ) {
          $content .=  '<tr>';
          $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __('Taxes:', 'mp') . '</td>';
          $content .=  '  <td class="mp_cart_col_tax">' . $mp->format_currency('', $tax_price) . '</td>';
          $content .=  '</tr>';
          $total = $total + $tax_price;
        }

    Add this:

    //Cash on Delivery
        $cod_price = $settings['gateways']['cash-on-delivery']['cost'];
    
        if ( $_SESSION['mp_payment_method'] == 'cash-on-delivery' ) {
          $content .=  '<tr>';
          $content .=  '  <td class="mp_cart_subtotal_lbl" colspan="3">' . __('Spese aggiuntive per Contrassegno:', 'mp') . '</td>';
          $content .=  '  <td class="mp_cart_col_tax">' . $mp->format_currency('', $cod_price) . '</td>';
          $content .=  '</tr>';
          $total = $total + $cod_price;
        }
  • imagenow

    Hi Aaron,
    the error message is that I don't see the page: when I choose the Cash on Delivery method and confirm the payment, after that the page return to blank.
    I don't know why, but I think I need to add the code for the COD method into the mail send file, but I don't know how the other methods of payment works.
    If I know it I can copy the code and change the parameters for the COD.
    There's only one step for the goal.
    Thank you in advance for your precious help.
    Greetings

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.