GatewayInfo::is_manual()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 11
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * Provides information about gateways.
5
 */
6
7
use Omnipay\Common\GatewayFactory;
8
9
class GatewayInfo{
10
11
	/**
12
	 * Get the available configured payment types, optionally with i18n readable names.
13
	 * @param bool $nice make the array values i18n readable.
14
	 * @return array map of gateway short name to translated long name.
15
	 */
16
	public static function get_supported_gateways($nice = true) {
17
		$allowed = Config::inst()->forClass('Payment')->allowed_gateways;
18
		if (!is_array($allowed)) {
19
			//include the manual payment type by default, if no gateways are configured
20
			$allowed = array("Manual");
21
		}
22
		$allowed = array_combine($allowed, $allowed);
23
		if ($nice) {
24
			$allowed = array_map('GatewayInfo::nice_title', $allowed);
25
		}
26
27
		return $allowed;
28
	}
29
30
	public static function nice_title($name) {
31
		$gateway = null;
32
		try {
33
			$factory = new GatewayFactory();
34
			$gateway = $factory->create($name);
35
		} catch (Exception $e) {
36
			/** do nothing */
37
		}
38
		return _t(
39
			"Payment.".$name,
40
			$gateway ? $gateway->getName() : $name
41
		);
42
	}
43
44
	/**
45
	 * Find out if the given gateway is supported.
46
	 * @param  string  $gateway gateway name to check
47
	 * @return boolean
48
	 */
49
	public static function is_supported($gateway) {
50
		$gateways = self::get_supported_gateways(false);
51
		return isset($gateways[$gateway]);
52
	}
53
54
	/**
55
	 * Checks if the given gateway name is an off-site gateway.
56
	 * 
57
	 * @param  string  $gateway gateway name
58
	 * @throws RuntimeException
59
	 * @return boolean the gateway offsite or not
60
	 */
61
	public static function is_offsite($gateway) {
62
		$factory = new GatewayFactory;
63
		$gateway = $factory->create($gateway);
64
		return (
65
			($gateway->supportsCompletePurchase() || $gateway->supportsCompleteAuthorize())
66
			// Some offsite gateways don't separate between authorize and complete requests,
67
			// so we need a different way to determine they're off site in the first place
68
			// without kicking off a purchase request within omnipay.
69
			|| (method_exists($gateway, 'isOffsite') && $gateway->isOffsite())
70
		);
71
	}
72
73
	/**
74
	 * Check for special 'manual' payment type.
75
	 * @param  string  $gateway
76
	 * @return boolean
77
	 */
78
    public static function is_manual($gateway) {
79
        $manualGateways = Config::inst()->forClass('Payment')->manual_gateways;
80
81
        // if not defined in config, set default manula gateway to 'Manual'
82
        if (!$manualGateways)
83
        {
84
            $manualGateways = array('Manual');
85
        }
86
87
        return in_array($gateway,$manualGateways);
88
    }
89
90
	/**
91
	 * Get the required parameters for a given gateway
92
	 * @param string $gateway gateway name
93
	 * @return array required parameters
94
	 */
95
	public static function required_fields($gateway) {
96
		$parameters = Config::inst()->forClass('Payment')->parameters;
97
		$fields = array();
98
		if(isset($parameters[$gateway]['required_fields']) &&
99
			is_array($parameters[$gateway]['required_fields'])){
100
				$fields = $parameters[$gateway]['required_fields'];
101
		}
102
		//always require the following for on-site gateways (and not manual)
103
		if (!self::is_offsite($gateway) && !self::is_manual($gateway)) {
104
			$fields = array_merge(
105
				$fields,
106
				array('name','number','expiryMonth','expiryYear','cvv')
107
			);
108
		}
109
		return $fields;
110
	}
111
112
}
113