Completed
Push — 2.0 ( 281022...1bc7b4 )
by Mark
8s
created

GatewayInfo::shouldUseAsyncNotifications()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
use Omnipay\Common\GatewayFactory;
4
5
/**
6
 * Provides information about gateways.
7
 *
8
 * Use this class in YAML to configure your gateway settings.
9
 * Eg.
10
 * <code>
11
 * GatewayInfo:
12
 *  PayPal_Express:
13
 *    parameters:
14
 *      username: 'my.user.name'
15
 *      # more parameters…
16
 * </code>
17
 *
18
 * The following config settings are allowed per gateway:
19
 * * `is_manual` *boolean*: Set this to true if this gateway should be considered a "Manual" Payment (eg. Invoice)
20
 * * `use_authorize` *boolean*: Whether or not this Gateway should prefer authorize over purchase
21
 * * `use_async_notification` *boolean*: When set to true, this Gateway will receive asynchronous notifications from the Payment provider
22
 * * `token_key` *string*: Key for the token parameter
23
 * * `required_fields` *array*: An array of required form-fields
24
 * * `properties` *map*: All gateway properties that will be passed along to the Omnipay Gateway instance
25
 */
26
class GatewayInfo
27
{
28
    /**
29
     * Config accessor
30
     * @return Config_ForClass
31
     */
32
    public static function config()
33
    {
34
        return Config::inst()->forClass('GatewayInfo');
35
    }
36
37
    /**
38
     * Get the available configured payment types, optionally with i18n readable names.
39
     * @param bool $nice make the array values i18n readable.
40
     * @return array map of gateway short name to translated long name.
41
     */
42
    public static function getSupportedGateways($nice = true)
43
    {
44
        $allowed = Payment::config()->allowed_gateways;
45
        if (!is_array($allowed)) {
46
            //include the manual payment type by default, if no gateways are configured
47
            $allowed = array("Manual");
48
        }
49
        $allowed = array_combine($allowed, $allowed);
50
        if ($nice) {
51
            $allowed = array_map('GatewayInfo::niceTitle', $allowed);
52
        }
53
54
        return $allowed;
55
    }
56
57
    /**
58
     * Get a locale aware title for the given gateway
59
     * @param string $name gateway short name
60
     * @return string nice title for the gateway. Uses translations, if available
61
     */
62
    public static function niceTitle($name)
63
    {
64
        $gateway = null;
65
        try {
66
            $factory = new GatewayFactory();
67
            $gateway = $factory->create($name);
68
        } catch (Exception $e) {
69
            /** do nothing */
70
        }
71
        return _t(
72
            "Payment." . $name,
73
            $gateway ? $gateway->getName() : $name
74
        );
75
    }
76
77
    /**
78
     * Find out if the given gateway is supported.
79
     * @param  string $gateway gateway name to check
80
     * @return boolean
81
     */
82
    public static function isSupported($gateway)
83
    {
84
        $gateways = self::getSupportedGateways(false);
85
        return isset($gateways[$gateway]);
86
    }
87
88
    /**
89
     * Checks if the given gateway name is an off-site gateway.
90
     *
91
     * @param  string $gateway gateway name
92
     * @throws RuntimeException
93
     * @return boolean the gateway offsite or not
94
     */
95
    public static function isOffsite($gateway)
96
    {
97
        $factory = new GatewayFactory();
98
        $gateway = $factory->create($gateway);
99
        // Some offsite gateways don't separate between authorize and complete requests,
100
        // so we need a different way to determine they're off site in the first place
101
        // without kicking off a purchase request within Omnipay.
102
        if (method_exists($gateway, 'isOffsite')) {
103
            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...
104
        }
105
106
        if($gateway instanceof \Omnipay\Common\AbstractGateway){
107
            return ($gateway->supportsCompletePurchase() || $gateway->supportsCompleteAuthorize());
108
        }
109
110
        return false;
111
    }
112
113
    /**
114
     * Check for special 'manual' payment type.
115
     * @param  string $gateway
116
     * @return boolean
117
     */
118
    public static function isManual($gateway)
119
    {
120
        if (self::getConfigSetting($gateway, 'is_manual')) {
121
            return true;
122
        }
123
124
        $manualGateways = Payment::config()->manual_gateways;
125
        if (is_array($manualGateways)) {
126
            Deprecation::notice(
127
                '3.0',
128
                'Please refrain from using Payment:manual_gateways config. ' .
129
                'Mark individual gateways with `is_manual` instead (see docs).'
130
            );
131
        }
132
133
        // if not defined in config, set default manual gateway to 'Manual'
134
        if (!$manualGateways) {
135
            $manualGateways = array('Manual');
136
        }
137
138
        return in_array($gateway, $manualGateways);
139
    }
140
141
    /**
142
     * Check if the given gateway should use authorize payments
143
     * @param string $gateway the gateway name
144
     * @return boolean
145
     */
146
    public static function shouldUseAuthorize($gateway)
147
    {
148
        // Manual gateways are "authorized" by nature
149
        if (self::isManual($gateway)) {
150
            return true;
151
        }
152
153
        return self::getConfigSetting($gateway, 'use_authorize') == true;
154
    }
155
156
    /**
157
     * Check if the given gateway should use asynchronous notifications
158
     * @param string $gateway the gateway name
159
     * @return boolean
160
     */
161
    public static function shouldUseAsyncNotifications($gateway)
162
    {
163
        // Manual gateways can be excluded
164
        if (self::isManual($gateway)) {
165
            return false;
166
        }
167
168
        return self::getConfigSetting($gateway, 'use_async_notification') == true;
169
    }
170
171
    /**
172
     * Get the token key value configured for the given gateway
173
     * @param string $gateway the gateway name
174
     * @param string $default the default token key if not found in config
175
     * @return string
176
     */
177
    public static function getTokenKey($gateway, $default = 'token')
178
    {
179
        $tokenKey = Payment::config()->token_key;
180
        if($tokenKey){
181
            Deprecation::notice(
182
                '3.0',
183
                'Please refrain from setting token_key as config parameter of Payment. ' .
184
                'Use GatewayInfo and set the token key on a gateway basis (see docs).'
185
            );
186
        } else {
187
            $tokenKey = self::getConfigSetting($gateway, 'token_key');
188
        }
189
190
        return is_string($tokenKey) ? $tokenKey : $default;
191
    }
192
193
    /**
194
     * Get the required parameters for a given gateway
195
     * @param string $gateway gateway name
196
     * @return array required parameters
197
     */
198
    public static function requiredFields($gateway)
199
    {
200
        $parameters = self::getParameters($gateway);
201
        $fields = array();
202
        if (isset($parameters['required_fields']) && is_array($parameters['required_fields'])) {
203
            Deprecation::notice(
204
                '3.0',
205
                'Please refrain from setting required_fields in the gateway parameters. ' .
206
                'Put the `required_fields` directly under the gateway (see docs).'
207
            );
208
            $fields = $parameters['required_fields'];
209
        } else {
210
            $requiredFields = self::getConfigSetting($gateway, 'required_fields');
211
            if (is_array($requiredFields)) {
212
                $fields = $requiredFields;
213
            }
214
        }
215
216
        //always require the following for on-site gateways (and not manual)
217
        if (!self::isOffsite($gateway) && !self::isManual($gateway)) {
218
            $fields = array_merge(
219
                $fields,
220
                array('name', 'number', 'expiryMonth', 'expiryYear', 'cvv')
221
            );
222
        }
223
224
        return $fields;
225
    }
226
227
    /**
228
     * Get the gateway config-parameters.
229
     *
230
     * @param string $gateway the gateway name
231
     * @return array|null gateway parameters
232
     */
233
    public static function getParameters($gateway)
234
    {
235
        $params = Payment::config()->parameters;
236
        if (isset($params[$gateway])) {
237
            Deprecation::notice(
238
                '3.0',
239
                'Please refrain from setting Gateway parameters under Payment. ' .
240
                'Use GatewayConfig instead (see docs).'
241
            );
242
            return $params[$gateway];
243
        }
244
245
        $params = self::getConfigSetting($gateway, 'parameters');
246
        return is_array($params) ? $params : null;
247
    }
248
249
    /**
250
     * Get a single config setting for a gateway
251
     * @param string $gateway the gateway name
252
     * @param string $key the config key to get
253
     * @return mixed
254
     */
255
    public static function getConfigSetting($gateway, $key)
256
    {
257
        $config = self::config()->get($gateway);
258
        if (!is_array($config)) {
259
            return null;
260
        }
261
262
        return isset($config[$key]) ? $config[$key] : null;
263
    }
264
265
    // -----------------------------------------------------------------------------------------------------------------
266
    // Deprecated methods.
267
    // TODO: Remove with 3.0
268
    // -----------------------------------------------------------------------------------------------------------------
269
270
    /**
271
     * Get the available configured payment types, optionally with i18n readable names.
272
     * @param bool $nice make the array values i18n readable.
273
     * @return array map of gateway short name to translated long name.
274
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use getSupportedGateways
275
     */
276
    public static function get_supported_gateways($nice = true)
277
    {
278
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use getSupportedGateways');
279
        return self::getSupportedGateways($nice);
280
    }
281
282
    /**
283
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use niceTitle
284
     */
285
    public static function nice_title($name)
286
    {
287
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use niceTitle');
288
        return self::niceTitle($name);
289
    }
290
291
    /**
292
     * Find out if the given gateway is supported.
293
     * @param  string $gateway gateway name to check
294
     * @return boolean
295
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use isSupported
296
     */
297
    public static function is_supported($gateway)
298
    {
299
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use isSupported');
300
        return self::isSupported($gateway);
301
    }
302
303
    /**
304
     * Checks if the given gateway name is an off-site gateway.
305
     *
306
     * @param  string $gateway gateway name
307
     * @throws RuntimeException
308
     * @return boolean the gateway offsite or not
309
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use isOffsite
310
     */
311
    public static function is_offsite($gateway)
312
    {
313
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use isOffsite');
314
        return self::isOffsite($gateway);
315
    }
316
317
    /**
318
     * Check for special 'manual' payment type.
319
     * @param  string $gateway
320
     * @return boolean
321
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use isManual
322
     */
323
    public static function is_manual($gateway)
324
    {
325
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use isManual');
326
        return self::isManual($gateway);
327
    }
328
329
    /**
330
     * Get the required parameters for a given gateway
331
     * @param string $gateway gateway name
332
     * @return array required parameters
333
     * @deprecated 3.0 Snake-case methods will be deprecated with 3.0, use requiredFields
334
     */
335
    public static function required_fields($gateway)
336
    {
337
        Deprecation::notice('3.0', 'Snake-case methods will be deprecated with 3.0. Use requiredFields');
338
        return self::requiredFields($gateway);
339
    }
340
}
341