All posts tagged paypal

logo_paypal

There is a bug in Paypal Standard for Magento, when a customer has finished the order process and validate his payment in Paypal, the customer doesn’t receive any email including his invoice. He receives only a confirmation payment of Paypal and a message after the process to tell him that the order is going to be proceeded.

To resolve this problem, two possibilities:

  • Overload the class in your own module : Mage_Paypal_Model_Standard (app/code/core/Mage/Paypal/Model/) at the line 418 after $invoice->register()->pay();, add the following line: $invoice->sendEmail();
  • Create an event with the help of a class Observer.php which trigger after the event sales_order_invoice_pay, which in Paypal standard start at line 418, after the treatment $invoice->register()->pay(). To do it, you have to create a module (there is lots of tutorials on Google to know how to do it) with the following files (replace Name by the name of your module):
    • /app/code/local/Name/Sales/etc/config.xml
    • /app/code/local/Name/Sales/Model/Observer.php
    • /app/etc/modules/Name_All.xm
Here the content:
/app/code/local/Name/Sales/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Name_Sales>
            <version>0.1.0</version>
        </Name_Sales>
    </modules>
    <global>
        <models>
            <sales>
                <rewrite>
                    <observer>Name_Sales_Model_Observer</observer>
                </rewrite>
            </sales>
        </models>
        <events>
            <sales_order_invoice_pay>
                <observers>
                    <sales_notification_observer>
                        <type>singleton</type>
                        <class>sales/observer</class>
                        <method>sendInvoiceEmail</method>
                    </sales_notification_observer>
                </observers>
            </sales_order_invoice_pay>
        </events>
    </global>
</config>

/app/code/local/Name/Sales/Model/Observer.php

<?php
/**
 * Event sales_order_invoice_pay
 *
 */
require_once 'Mage/Sales/Model/Observer.php';
class Name_Sales_Model_Observer extends Mage_Sales_Model_Observer {
 
    public function sendInvoiceEmail($observer) {
            $invoice = $observer->getEvent ()->getInvoice ();
 
            switch ($invoice->getState ()) {
                case Mage_Sales_Model_Order_Invoice::STATE_PAID :
 
		try {
			// Save first time the invoice to get an id and send the invoice to the customer with the correct id
			$invoice->save ();
			Mage::log ('Email sent to the customer by the Name/sales/observer - Invoice number ' . $invoice->getIncrementId(), Zend_Log::DEBUG );
			$invoice->sendEmail ();
			$invoice->setEmailSent ( true );
			// Save a second time to save the EmailSent value
			$invoice->save ();
		} catch ( Exception $e ) {
			$session = Mage::getSingleton ( 'core/session' );
			$exception = new Exception ( Mage::helper('Sales')->__('Error during the email sending.'), 0 );
			$session->addException ( $exception, Mage::helper('Sales')->__('Error to send invoice email. Please, contact the website administrator.') );
		}
            }
        return $this;
    }
}

/app/etc/modules/Name_All.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Name_Sales>
            <active>true</active>
            <codePool>local</codePool>
        </Name_Sales>
    </modules>
</config>

In Paypal Express, the interesting line is in the controller /app/code/core/Mage/Paypal/controllers/expressController.php, in the method updateorderAction(){} the code is near of $invoice->register()->capture(); add $invoice->sendEmail(); or use this observer (untested).

For more information about the events I invite you to visit the Magento website which make good explanation: Customizing Magento using Event-Observer Method

I was astonished is to see that you cannot deactivate the payment method Paypal when the total amount is null. You will guess that is useless to have this payment method activated when nothing need to be paid. This scenario can arrived in certain circumstances otherwise Varien would not provide the payment method “No payment information is required” for Magento. I propose to you to change this behaviour by adding only three files in your installation.

Here how to proceed:

  1. Create a module Paypal in Magento in the following folder for example: /app/code/local/Yourname/Paypal/
  2. Create a folder “etc” in the previous folder created: /app/code/local/Yourname/Paypal/etc/
  3. Create a file config.xml with the content below (replace yourname by the wished name – name of the society or else, no underscore or special characters)
  4. Create a file system.xml with the content below
  5. Activate your new module by creating the file /app/etc/modules/Yourname_All.xml with the informations below by not forgetting to replace Yourname.

This code is for Magento versions 1.5.1

Please, read until the bottom. You will find later below the code for Magento 1.4.x
- File etc/config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Yourname_Paypal>
            <version>0.1.0</version>
        </Yourname_Paypal>
    </modules>
   
   <global>
        <models>
            <paypal>
                <rewrite>
                    <standard>Yourname_Paypal_Model_Overwrite_Standard</standard>
                </rewrite>
            </paypal>
        </models>
    </global>
   
       <default>
        <paypal>
            <wps>
                <min_order_total></min_order_total>
                <max_order_total></max_order_total>
            </wps>
        </paypal>
    </default>
</config>

- File etc/system.xml

<?xml version="1.0"?>
<config>
   <sections>
        <paypal translate="label" module="paypal">
            <groups>
                <wps translate="label comment">
                    <fields>
                        <min_order_total translate="label">
                            <label>Minimum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>50</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </min_order_total>
                        <max_order_total translate="label">
                            <label>Maximum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>60</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </max_order_total>
                    </fields>
                </wps>
            </groups>
        </paypal>
    </sections>
</config>

- file Model/Overwrite/Standard.php

<?php

class Yourname_Paypal_Model_Overwrite_Standard extends Mage_Paypal_Model_Standard
{
    /**
     * Check whether payment method can be used
     * @param Mage_Sales_Model_Quote
     * @return bool
     */
    public function isAvailable($quote = null)
    {
    	/**
         * Checking for min/max order total for assigned payment method
         */
        $total = $quote->getBaseGrandTotal();
        $minTotal = Mage::getStoreConfig('paypal/wps/min_order_total', ($quote ? $quote->getStoreId() : null));
        $maxTotal = Mage::getStoreConfig('paypal/wps/max_order_total', ($quote ? $quote->getStoreId() : null));

        $priceAuth = true;
        if((!empty($minTotal) && ($total < $minTotal)) || (!empty($maxTotal) && ($total > $maxTotal))) {
            $priceAuth = false;
        }
        
        if ($this->getConfig()->isMethodAvailable() && parent::isAvailable($quote) && $priceAuth) {
            return true;
        }
        return false;
    }
}

This code is for Magento versions < 1.4.x

- File config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Yourname_Paypal>
            <version>0.1.0</version>
        </Yourname_Paypal>
    </modules>

    <default>
        <payment>
            <paypal_express>
                <min_order_total>0</min_order_total>
                <max_order_total>0</max_order_total>
            </paypal_express>
            <paypal_direct>
                <min_order_total>0</min_order_total>
                <max_order_total>0</max_order_total>
            </paypal_direct>
            <paypal_standard>
                <min_order_total>0</min_order_total>
                <max_order_total>0</max_order_total>
            </paypal_standard>
        </payment>
    </default>
</config>

- File system.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <sections>
        <payment>
            <groups>
                <paypal_direct>
                    <fields>
                        <min_order_total translate="label">
                            <label>Minimum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>98</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </min_order_total>
                        <max_order_total translate="label">
                            <label>Maximum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>99</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </max_order_total>
                    </fields>
                </paypal_direct>
                <paypal_express>
                    <fields>
                        <min_order_total translate="label">
                            <label>Minimum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>98</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </min_order_total>
                        <max_order_total translate="label">
                            <label>Maximum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>99</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </max_order_total>
                    </fields>
                </paypal_express>
                <paypal_standard>
                    <fields>
                        <min_order_total translate="label">
                            <label>Minimum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>98</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </min_order_total>
                        <max_order_total translate="label">
                            <label>Maximum Order Total</label>
                            <frontend_type>text</frontend_type>
                            <sort_order>99</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>0</show_in_store>
                        </max_order_total>
                    </fields>
                </paypal_standard>
            </groups>
        </payment>
    </sections>
</config>

Code available for all Magento versions

- File app/etc/modules/Yourname_Paypal.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Yourname_Paypal>
            <active>true</active>
            <codePool>local</codePool>
        </Yourname_Paypal>
    </modules>
</config>

To finish you only need to go to your backend, Configuration > Payment method > Paypal (Express, standard or direct) and provide a minimum amount or even a maximum to display this payment method or not.