Completed
Push — 2.0 ( 95bb32...281022 )
by Mark
8s
created

GatewayInfo::isManual()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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
use Omnipay\Common\GatewayFactory;
7
8
class GatewayInfo
9
{
10
    /**
11
     * Config accessor
12
     * @return Config_ForClass
13
     */
14
    public static function config()
15
    {
16
        return Config::inst()->forClass('GatewayInfo');
17
    }
18
19
    /**
20
     * Get the available configured payment types, optionally with i18n readable names.
21
     * @param bool $nice make the array values i18n readable.
22
     * @return array map of gateway short name to translated long name.
23
     */
24
    public static function getSupportedGateways($nice = true)
25
    {
26
        $allowed = Payment::config()->allowed_gateways;
27
        if (!is_array($allowed)) {
28
            //include the manual payment type by default, if no gateways are configured
29
            $allowed = array("Manual");
30
        }
31
        $allowed = array_combine($allowed, $allowed);
32
        if ($nice) {
33
            $allowed = array_map('GatewayInfo::niceTitle', $allowed);
34
        }
35
36
        return $allowed;
37
    }
38
39
    /**
40
     * Get a locale aware title for the given gateway
41
     * @param string $name gateway short name
42
     * @return string nice title for the gateway. Uses translations, if available
43
     */
44
    public static function niceTitle($name)
45
    {
46
        $gateway = null;
47
        try {
48
            $factory = new GatewayFactory();
49
            $gateway = $factory->create($name);
50
        } catch (Exception $e) {
51
            /** do nothing */
52
        }
53
        return _t(
54
            "Payment." . $name,
55
            $gateway ? $gateway->getName() : $name
56
        );
57
    }
58
59
    /**
60
     * Find out if the given gateway is supported.
61
     * @param  string $gateway gateway name to check
62
     * @return boolean
63
     */
64
    public static function isSupported($gateway)
65
    {
66
        $gateways = self::getSupportedGateways(false);
67
        return isset($gateways[$gateway]);
68
    }
69
70
    /**
71
     * Checks if the given gateway name is an off-site gateway.
72
     *
73
     * @param  string $gateway gateway name
74
     * @throws RuntimeException
75
     * @return boolean the gateway offsite or not
76
     */
77
    public static function isOffsite($gateway)
78
    {
79
        $factory = new GatewayFactory();
80
        $gateway = $factory->create($gateway);
81
        // Some offsite gateways don't separate between authorize and complete requests,
82
        // so we need a different way to determine they're off site in the first place
83
        // without kicking off a purchase request within Omnipay.
84
        if (method_exists($gateway, 'isOffsite')) {
85
            return !!$gateway->isOffsite();
0 ignored issues
show
Bug introduced by
The method isOffsite() does not seem to exist on object<Omnipay\Common\GatewayInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
        }
87
88
        return ($gateway->supportsCompletePurchase() || $gateway->supportsCompleteAuthorize());
89
    }
90
91
    /**
92
     * Check for special 'manual' payment type.
93
     * @param  string $gateway
94
     * @return boolean
95
     */
96
    public static function isManual($gateway)
97
    {
98
        $manualGateways = Payment::config()->manual_gateways;
99
100
        // if not defined in config, set default manual gateway to 'Manual'
101
        if (!$manualGateways) {
102
            $manualGateways = array('Manual');
103
        }
104
105
        return in_array($gateway, $manualGateways);
106
    }
107
108
    /**
109
     * Get the required parameters for a given gateway
110
     * @param string $gateway gateway name
111
     * @return array required parameters
112
     */
113
    public static function requiredFields($gateway)
114
    {
115
        $parameters = self::getParameters($gateway);
116
        $fields = array();
117
        if (isset($parameters['required_fields']) && is_array($parameters['required_fields'])) {
118
            $fields = $parameters['required_fields'];
119
        }
120
121
        //always require the following for on-site gateways (and not manual)
122
        if (!self::isOffsite($gateway) && !self::isManual($gateway)) {
123
            $fields = array_merge(
124
                $fields,
125
                array('name', 'number', 'expiryMonth', 'expiryYear', 'cvv')
126
            );
127
        }
128
129
        return $fields;
130
    }
131
132
133
    /**
134
     * Get the gateway config-parameters.
135
     *
136
     * @param string $gateway the gateway name
137
     * @return array|null gateway parameters
138
     */
139
    public static function getParameters($gateway)
140
    {
141
        $params = Payment::config()->parameters;
142
        if(isset($params[$gateway])){
143
            return $params[$gateway];
144
        }
145
        return null;
146
    }
147
148
    // -----------------------------------------------------------------------------------------------------------------
149
    // Deprecated methods.
150
    // TODO: Remove with 3.0
151
    // -----------------------------------------------------------------------------------------------------------------
152
153
    /**
154
     * Get the available configured payment types, optionally with i18n readable names.
155
     * @param bool $nice make the array values i18n readable.
156
     * @return array map of gateway short name to translated long name.
157
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use getSupportedGateways
158
     */
159
    public static function get_supported_gateways($nice = true)
160
    {
161
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use getSupportedGateways');
162
        return self::getSupportedGateways($nice);
163
    }
164
165
    /**
166
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use niceTitle
167
     */
168
    public static function nice_title($name)
169
    {
170
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use niceTitle');
171
        return self::niceTitle($name);
172
    }
173
174
    /**
175
     * Find out if the given gateway is supported.
176
     * @param  string $gateway gateway name to check
177
     * @return boolean
178
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use isSupported
179
     */
180
    public static function is_supported($gateway)
181
    {
182
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use isSupported');
183
        return self::isSupported($gateway);
184
    }
185
186
    /**
187
     * Checks if the given gateway name is an off-site gateway.
188
     *
189
     * @param  string $gateway gateway name
190
     * @throws RuntimeException
191
     * @return boolean the gateway offsite or not
192
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use isOffsite
193
     */
194
    public static function is_offsite($gateway)
195
    {
196
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use isOffsite');
197
        return self::isOffsite($gateway);
198
    }
199
200
    /**
201
     * Check for special 'manual' payment type.
202
     * @param  string $gateway
203
     * @return boolean
204
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use isManual
205
     */
206
    public static function is_manual($gateway)
207
    {
208
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use isManual');
209
        return self::isManual($gateway);
210
    }
211
212
    /**
213
     * Get the required parameters for a given gateway
214
     * @param string $gateway gateway name
215
     * @return array required parameters
216
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use requiredFields
217
     */
218
    public static function required_fields($gateway)
219
    {
220
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use requiredFields');
221
        return self::requiredFields($gateway);
222
    }
223
}
224