-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommerce_midtrans.install
executable file
·144 lines (125 loc) · 5.12 KB
/
commerce_midtrans.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* @file
* Performs install/uninstall used for Commerce Midtrans.
*/
// Field name for Commerce direcpay billing phone.
define('COMMERCE_MIDTRANS_BILLING_PHONE_FIELD', 'commerce_midtrans_billing_phone');
/**
* Implements hook_requirements().
*/
function commerce_midtrans_requirements($phase) {
$requirements = array();
if (in_array($phase, array('runtime', 'install'))) {
$t = get_t();
$has_curl = function_exists('curl_init');
$version = explode('-', PHP_VERSION);
$has_php = version_compare($version[0], '5.2.1', '>');
$has_json = function_exists('json_decode');
$requirements['commerce_midtrans_curl'] = array(
'title' => $t('CURL'),
'value' => $has_curl ? $t('Enabled') : $t('Not found'),
'description' => '',
'severity' => REQUIREMENT_OK,
);
$requirements['commerce_midtrans_php'] = array(
'title' => $t('PHP Version > 5.2.1'),
'value' => $has_php ? $t('True') : $t('False'),
'description' => '',
'severity' => REQUIREMENT_OK,
);
$requirements['commerce_midtrans_json'] = array(
'title' => $t('JSON'),
'value' => $has_json ? $t('Enabled') : $t('Not found'),
'description' => '',
'severity' => REQUIREMENT_OK,
);
if (!$has_curl) {
$requirements['commerce_midtrans_curl']['severity'] = REQUIREMENT_ERROR;
$url = l($t('cURL'), "http://php.net/manual/en/curl.setup.php");
$requirements['commerce_midtrans_curl']['description'] = $t('The testing framework could not be installed because the PHP !cURL library is not available.', array('!cURL' => $url));
}
if (!$has_php) {
$requirements['commerce_midtrans_php']['severity'] = REQUIREMENT_ERROR;
$requirements['commerce_midtrans_php']['description'] = $t('The testing framework could not be installed because the PHP library is not available.');
}
if (!$has_json) {
$requirements['commerce_midtrans_json']['severity'] = REQUIREMENT_ERROR;
$url = l($t('Json'), "http://php.net/manual/en/book.json.php");
$requirements['commerce_midtrans_json']['description'] = $t('The testing framework could not be installed because the PHP !cURL library is not available.', array('!cURL' => $url));
}
}
// TODO: to be removed
// if (in_array($phase, array('runtime', 'update'))) {
// $library = libraries_detect('veritrans');
// $requirements['veritrans'] = array(
// 'title' => $t('Veritrans Library'),
// 'value' => ($library['installed']) ? $t('Installed') : $t('Not found'),
// 'description' => $t('PHP client library for Veritrans is installed correctly.'),
// 'severity' => REQUIREMENT_OK,
// );
// if (!$library['installed']) {
// $requirements['veritrans']['severity'] = REQUIREMENT_ERROR;
// $requirements['veritrans']['description'] = $t('PHP client library for Veritrans is not installed correctly.');
// }
// }
return $requirements;
}
/**
* Implements hook_install().
*/
function commerce_midtrans_install() {
// Add phone number field to billing profile type.
commerce_midtrans_add_phone_field();
// Tell user a new field has been added.
$message_text = 'Commerce Midtrans added a new field to your "billing" customer profile for a phone number. If you already have one enabled, you must <a href="!disablelink">disable one</a> now to avoid customer confusion during checkout.';
$t = get_t();
$message = $t($message_text, array('!disablelink' => url('admin/commerce/customer-profiles/types/billing/fields')));
drupal_set_message($message, 'warning');
}
/**
* Commerce customer module creates a customer profile type called "billing".
* This profile type only has the field "addressfield", which does not
* include a phone number. Midtrans requires a phone number for
* transactions, so we create our own.
*/
function commerce_midtrans_add_phone_field() {
// Look for or add a billing phone field to add to billing customer profile.
$field = field_info_field(COMMERCE_MIDTRANS_BILLING_PHONE_FIELD);
$instance = field_info_instance('commerce_customer_profile', COMMERCE_MIDTRANS_BILLING_PHONE_FIELD, 'billing');
if (empty($field)) {
$field = array(
'field_name' => COMMERCE_MIDTRANS_BILLING_PHONE_FIELD,
'type' => 'text',
'cardinality' => 1,
'entity_types' => array('commerce_customer_profile'),
'translatable' => FALSE,
'locked' => FALSE,
);
$field = field_create_field($field);
}
if (empty($instance)) {
$t = get_t();
$instance = array(
'field_name' => COMMERCE_MIDTRANS_BILLING_PHONE_FIELD,
'entity_type' => 'commerce_customer_profile',
'bundle' => 'billing',
'label' => $t('Phone Number'),
'required' => TRUE,
'widget' => array(
'type' => 'text_textfield',
),
'settings' => array(),
'display' => array(),
);
// Set the default display formatters for various view modes.
foreach (array('default', 'customer', 'administrator') as $view_mode) {
$instance['display'][$view_mode] = array(
'label' => 'hidden',
'type' => 'text_default',
'weight' => -10,
);
}
field_create_instance($instance);
}
}