## $Id$
##
## Copyright (C) 2022 Uli Fouquet & Henrik Bettermann
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
import ConfigParser
import grok
from zope.component import queryUtility
from zope.interface import implementer
from kofacustom.nigeria.paypal.interfaces import IPaypalConfig

VERIFY_URL_PROD = 'https://ipnpb.paypal.com/cgi-bin/webscr'
VERIFY_URL_TEST = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr'
VALID_PAYPAL_EMAILS = [
        "sample_biz@paypal.com",
        ]


def parse_paypal_config(path):
    """Get paypal credentials from config file

    This is called on initialisation when the `kofacustomng:paypalconf`
    directive is set in ZCML configuration.

    Retrieve the respective config with: `getUtility(IPaypalConfig)`
    """
    parser = ConfigParser.SafeConfigParser({'id': None, 'secret': None, 'mode': 'sandbox',})
    with open(path) as fp:
        parser.readfp(fp)
    return {
            'client_id': parser.get('rest-client', 'id', None),
            'client_secret': parser.get('rest-client', 'secret', None),
            'mode': parser.get('rest-client', 'mode', 'sandbox'),
            }
