1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
2
|
|
|
/** |
3
|
|
|
* Provides payment support for credit cards and other providers like PayPal. |
4
|
|
|
* |
5
|
|
|
* $Id: Payment.php 3769 2008-12-15 00:48:56Z zombor $ |
6
|
|
|
* |
7
|
|
|
* @package Payment |
8
|
|
|
* @author Kohana Team |
9
|
|
|
* @copyright (c) 2007-2008 Kohana Team |
10
|
|
|
* @license http://kohanaphp.com/license.html |
11
|
|
|
*/ |
12
|
|
|
class Payment_Core |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
// Configuration |
16
|
|
|
protected $config = array( |
17
|
|
|
// The driver string |
18
|
|
|
'driver' => null, |
19
|
|
|
// Test mode is set to true by default |
20
|
|
|
'test_mode' => true, |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
protected $driver = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sets the payment processing fields. |
27
|
|
|
* The driver will translate these into the specific format for the provider. |
28
|
|
|
* Standard fields are (Providers may have additional or different fields): |
29
|
|
|
* |
30
|
|
|
* card_num |
31
|
|
|
* exp_date |
32
|
|
|
* cvv |
33
|
|
|
* description |
34
|
|
|
* amount |
35
|
|
|
* tax |
36
|
|
|
* shipping |
37
|
|
|
* first_name |
38
|
|
|
* last_name |
39
|
|
|
* company |
40
|
|
|
* address |
41
|
|
|
* city |
42
|
|
|
* state |
43
|
|
|
* zip |
44
|
|
|
* email |
45
|
|
|
* phone |
46
|
|
|
* fax |
47
|
|
|
* ship_to_first_name |
48
|
|
|
* ship_to_last_name |
49
|
|
|
* ship_to_company |
50
|
|
|
* ship_to_address |
51
|
|
|
* ship_to_city |
52
|
|
|
* ship_to_state |
53
|
|
|
* ship_to_zip |
54
|
|
|
* |
55
|
|
|
* @param array the driver string |
56
|
|
|
*/ |
57
|
|
|
public function __construct($config = array()) |
58
|
|
|
{ |
59
|
|
|
if (empty($config)) { |
60
|
|
|
// Load the default group |
61
|
|
|
$config = Kohana::config('payment.default'); |
62
|
|
|
} elseif (is_string($config)) { |
63
|
|
|
$this->config['driver'] = $config; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Merge the default config with the passed config |
67
|
|
|
is_array($config) and $this->config = array_merge($this->config, $config); |
68
|
|
|
|
69
|
|
|
// Set driver name |
70
|
|
|
$driver = 'Payment_'.ucfirst($this->config['driver']).'_Driver'; |
71
|
|
|
|
72
|
|
|
// Load the driver |
73
|
|
View Code Duplication |
if (! Kohana::auto_load($driver)) { |
|
|
|
|
74
|
|
|
throw new Kohana_Exception('core.driver_not_found', $this->config['driver'], get_class($this)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
// Get the driver specific settings |
78
|
|
|
$this->config = array_merge($this->config, Kohana::config('payment.'.$this->config['driver'])); |
79
|
|
|
|
80
|
|
|
// Initialize the driver |
81
|
|
|
$this->driver = new $driver($this->config); |
82
|
|
|
|
83
|
|
|
// Validate the driver |
84
|
|
View Code Duplication |
if (! ($this->driver instanceof Payment_Driver)) { |
|
|
|
|
85
|
|
|
throw new Kohana_Exception('core.driver_implements', $this->config['driver'], get_class($this), 'Payment_Driver'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the credit card processing fields |
91
|
|
|
* |
92
|
|
|
* @param string field name |
93
|
|
|
* @param string value |
94
|
|
|
*/ |
95
|
|
|
public function __set($name, $val) |
96
|
|
|
{ |
97
|
|
|
$this->driver->set_fields(array($name => $val)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Bulk setting of payment processing fields. |
102
|
|
|
* |
103
|
|
|
* @param array array of values to set |
104
|
|
|
* @return Payment_Core this object |
105
|
|
|
*/ |
106
|
|
|
public function set_fields($fields) |
107
|
|
|
{ |
108
|
|
|
$this->driver->set_fields((array) $fields); |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Runs the transaction |
115
|
|
|
* |
116
|
|
|
* @return TRUE|string TRUE on successful payment, an error string on failure |
117
|
|
|
*/ |
118
|
|
|
public function process() |
119
|
|
|
{ |
120
|
|
|
return $this->driver->process(); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.