-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpg_simplepay.module
More file actions
executable file
·230 lines (193 loc) · 6.11 KB
/
pg_simplepay.module
File metadata and controls
executable file
·230 lines (193 loc) · 6.11 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* Implements hook_permission().
*/
function pg_simplepay_permission() {
// Add permission to admin pgapi.
return array(
'free access' => array(
'title' => t('Full access'),
'description' => t('Allows to access fields with no payment'),
'restrict access' => TRUE,
),
'buy access' => array(
'title' => t('Buy access'),
'description' => t('Allows to buy access'),
'restrict access' => TRUE,
),
);
}
/**
* Implements hook_field_formatter_info().
*/
function pg_simplepay_field_formatter_info() {
$types = field_info_field_types();
$res_types = array();
foreach($types as $type => $info){
$res_types[] = $type;
}
return array(
'pg_simplepay_access' => array(
'label' => t('Payment required'),
'field types' => $res_types,
'settings' => array(
'formatter' => '',
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function pg_simplepay_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
foreach (module_implements('field_formatter_info') as $module) {
$formatter_types = (array) module_invoke($module, 'field_formatter_info');
foreach ($formatter_types as $name => $formatter_info) {
$options[$name.":".$module] = $formatter_info['label'];
}
}
$element['formatter'] = array(
'#type' => 'select',
'#title' => t('Formater'),
'#options' => $options,
'#default_value' => $settings['formatter'],
);
return $element;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function pg_simplepay_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = array();
$summary[] = t('Display like !formatter', array('!formatter' => $settings['formatter']));
return implode('<br />', $summary);
}
/**
* Implements hook_field_formatter_view().
*/
function pg_simplepay_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
foreach ($items as $delta => $item) {
list($formatter,$module) = explode(":", $settings['formatter']);
$function = $module."_field_formatter_view";
$s_items = array();
$s_items[0] = $item;
$s_display['type'] = $formatter;
if (pg_simplepay_is_paid($entity) or user_access('free access') or $entity->pg_simplepay_free['und'][0]['value'] == 0) {
$data = $function($entity_type, $entity, $field, $instance, $langcode, $s_items, $s_display);
$element[$delta] = $data[0];
}
}
return $element;
}
/**
* Implements hook_node_view().
*/
function pg_simplepay_node_view($node, $view_mode = 'full') {
global $user;
if($node->type == 'pg_simplepay' && !user_access('free access') && !pg_simplepay_is_paid($node) && $node->pg_simplepay_free['und'][0]['value']){
$node->content['links']['pg_simplepay'] = array(
'#theme' => 'links__node__statistics',
'#links' => array(
'pg_simplepay' => array(
'title' => t('Buy access'),
'href' => 'simplepay/'.$node->nid,
)
),
'#attributes' => array('class' => array('links', 'inline')),
);
}
}
/**
* Implements hook_menu()
*/
function pg_simplepay_menu() {
$items['simplepay/%node'] = array(
'title' => 'Simple Pay',
'page callback' => 'pg_simplepay_prepay',
'page arguments' => array(1),
'access callback' => 'user_access',
'access arguments' => array('buy access'),
);
return $items;
}
/*
* Implements hook_pgapi_payment_details
*/
function pg_simplepay_pgapi_payment_details($transaction){
$node = node_load($transaction->extra['nid']);
$uri = entity_uri('node', $node);
return array(
t('Link') => l($node->title,$uri['path'], $uri['options']),
);
}
/*
* Implements hook_pgapi_transaction
*/
function pg_simplepay_pgapi_transaction($op,$t){
switch($op){
case PG_COMPLETED:
$simple_access['nid'] = $t->extra['nid'];
$simple_access['uid'] = $t->uid;
$simple_access['txnid'] = $t->txnid;
$simple_access['created'] = time();
if($t->uid == 0 ){
$simple_access['session'] = $t->extra['session'];
}
drupal_write_record('pg_simplepay_payment', $simple_access);
$t->workflow = pgapi_get_workflow_id('completed');
break;
case PG_DENIED:
$t->workflow = pgapi_get_workflow_id('security violation');
break;
case PG_FAILED:
$t->workflow = pgapi_get_workflow_id('canceled');
break;
}
}
function pg_simplepay_prepay ($node){
global $user;
//create transactions
$transaction = new stdClass();
$transaction->amount = $node->pg_simplepay_price['und'][0]['value'];
$transaction->service = 'pg_simplepay';
$transaction->uid = $user->uid;
$transaction->title=t('Access to !title ', array('!title' => l($node->title, 'node/'.$node->nid.'', array('html'=>true))));
$session_value = $_COOKIE[session_name()];
$transaction->extra = array(
'nid' => $node->nid,
'session' => $session_value,
);
pgapi_transaction_save($transaction);
$payment_url = url('payment/'. $transaction->txnid, array('absolute' => TRUE));
drupal_goto($payment_url);
return '';
}
/*
* Is Paid access by user
*/
function pg_simplepay_is_paid($node, $account=null){
global $user;
if (!$account) {
$account = $user;
}
$ret = FALSE;
if ($account->uid == 0){
$session_value = $_COOKIE[session_name()];
$pr = db_query("SELECT * FROM {pg_simplepay_payment} WHERE nid = :nid AND session = :session AND uid = :uid", array( ':nid' => $node->nid, ':session' => $session_value, ':uid' => $account->uid))->fetchObject();
if(!empty($pr)){
$ret = TRUE;
};
}else{
$pr = db_query("SELECT * FROM {pg_simplepay_payment} WHERE nid = :nid AND uid = :uid", array( ':nid' => $node->nid, ':uid' => $account->uid))->fetchObject();
if(!empty($pr)){
$ret = TRUE;
};
}
return $ret;
}