diff --git a/pg_simplepay.info b/pg_simplepay.info index 073be20..91cd7a4 100644 --- a/pg_simplepay.info +++ b/pg_simplepay.info @@ -2,4 +2,4 @@ name = Simple pay description = Create simple payment peer node. dependencies[] = pgapi package = "Commerce" -core = 6.x +core = 7.x diff --git a/pg_simplepay.module b/pg_simplepay.module index bd85aad..5d88725 100755 --- a/pg_simplepay.module +++ b/pg_simplepay.module @@ -15,26 +15,81 @@ function pg_simplepay_init() { } /** - * Implements hook_perm(). + * Implements hook_menu(). */ -function pg_simplepay_perm() { - return array('administer pgapi', 'free access', 'buy access'); +function pg_simplepay_menu() { + $items = array(); + $items['admin/pgdata/simplepay'] = array( + 'title' => 'Simple Pay Settings', + 'description' => 'Configure default settings for Simple Pay.', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('pg_simplepay_common'), + 'access callback' => 'user_access', + 'access arguments' => array('pg_simplepay administer pgapi'), + 'file' => 'pg_simplepay.admin.inc', + 'type' => MENU_NORMAL_ITEM, + ); + + $items['simplepay/%node'] = array( + 'title' => 'Simple Pay', + 'page callback' => 'pg_simplepay_prepay', + 'page arguments' => array(1), + 'access callback' => 'user_access', + 'access arguments' => array('pg_simplepay buy access'), + 'file' => 'pg_simplepay.admin.inc', + ); + + $items['simplepay/complete/%txnid'] = array( + 'title' => 'Payment completed', + 'page callback' => 'pg_simplepay_complete', + 'page arguments' => array(2), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + + $items['simplepay/fail/%txnid'] = array( + 'title' => 'Payment failed', + 'page callback' => 'pg_simplepay_fail', + 'page arguments' => array(2), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + + return $items; } /** - * Implements hook_link(). + * Implements hook_permission(). */ -function pg_simplepay_link($type, $object, $teaser = FALSE) { - $links = array(); - if (!user_access('free access') && variable_get('pg_simplepay_allowed_' . $object->type, 0) && $object->pg_simplepay_price > 0 && !pg_simplepay_is_paid($object->nid)) { - $links['sample_link'] = array( - 'title' => t('Buy access'), - 'href' => 'simplepay/' . $object->nid, - 'attributes' => array('title' => t('Buy access')), - ); - } +function pg_simplepay_permission() { + return array( + 'pg_simplepay administer pgapi' => array( + 'title' => t('Simple Pay Settings'), + 'description' => t('Access to settings module page.'), + ), + 'pg_simplepay free access' => array( + 'title' => t('Free access'), + 'description' => t('Free access to simplepay.'), + ), + 'pg_simplepay buy access' => array( + 'title' => t('Simple Pay'), + 'description' => t('Access to page simplepay.'), + ), + ); +} - return $links; +/** + * Implements hook_theme(). + */ +function pg_simplepay_theme() { + return array( + 'pg_simplepay_complete' => array( + 'arguments' => array('t' => array()), + ), + 'pg_simplepay_fail' => array( + 'arguments' => array('t' => array()), + ), + ); } /** @@ -49,13 +104,14 @@ function pg_simplepay_form_alter(&$form, $form_state, $form_id) { '#collapsed' => FALSE, '#weight' => 0, ); + $form['pg_simplepay']['pg_simplepay_price'] = array( '#type' => 'textfield', '#title' => t('Price'), '#default_value' => $form['#node']->pg_simplepay_price, - ); } + if ($form_id == 'node_type_form') { $form['pg_simplepay'] = array( '#type' => 'fieldset', @@ -63,147 +119,122 @@ function pg_simplepay_form_alter(&$form, $form_state, $form_id) { '#collapsible' => TRUE, '#collapsed' => TRUE, ); + $form['pg_simplepay']['pg_simplepay_allowed'] = array( '#type' => 'checkbox', '#title' => t('Simple pay purchase allowed'), '#default_value' => variable_get('pg_simplepay_allowed_' . $form['#node_type']->type, 0), ); - } } - /** - * Implements hook_nodeapi(). + * Implements hook_node_load(). */ -function pg_simplepay_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { - switch ($op) { - case 'load': - $pr = db_select('pg_simplepay_payment', 'psp') - ->fields('psp') - ->condition('nid', $node->nid) - ->condition('vid', $node->vid) - ->execute() - ->fetch(); - return array('pg_simplepay_price' => $pr->price ? $pr->price : 0); - - case 'update': - $pr = db_select('pg_simplepay_payment', 'psp') - ->fields('psp') - ->condition('nid', $node->nid) - ->condition('vid', $node->vid) - ->execute() - ->fetch(); - if (isset($pr->nid)) { - $pr->price = $node->pg_simplepay_price; - drupal_write_record('pg_simplepay_price', $pr, array('nid', 'vid')); - } - else { - $pr->price = $node->pg_simplepay_price; - $pr->nid = $node->nid; - $pr->vid = $node->vid; - drupal_write_record('pg_simplepay_price', $pr); - }; - break; - - case 'insert': - $pr = (object) (NULL); - $pr->price = $node->pg_simplepay_price; - $pr->nid = $node->nid; - $pr->vid = $node->vid; - drupal_write_record('pg_simplepay_price', $pr); - break; - - case 'delete': - db_query('DELETE FROM {pg_simplepay_price} WHERE nid = %d AND vid = %d', $node->nid, $node->vid); - break; +function pg_simplepay_node_load($nodes, $types) { + foreach ($nodes as $node) { + $simplepay_price = db_select('pg_simplepay_price', 'p') + ->fields('p', array('price')) + ->condition('nid', $node->nid) + ->condition('vid', $node->vid) + ->execute() + ->fetchObject(); - case 'view': - if (!user_access('free access') && variable_get('pg_simplepay_allowed_' . $node->type, 0) && $node->pg_simplepay_price > 0) { - if ($a4 == 1) { - if (!pg_simplepay_is_paid($node->nid)) { - $node2 = node_build_content(node_load($node->nid), 'TRUE'); - $node->content['body']['#value'] = $node2->teaser; - if (!empty($node->content['files']['#value'])) { - $node->content['files']['#value'] = t('Please buy access to see attached files'); - } - } - } - } - break; + $node->price = $simplepay_price ? $simplepay_price->price : 0; } } /** - * Implements hook_theme(). + * Implements hook_node_insert(). */ -function pg_simplepay_theme() { - return array( - 'pg_simplepay_complete' => array( - 'arguments' => array('t' => array()), - ), - 'pg_simplepay_fail' => array( - 'arguments' => array('t' => array()), - ), - ); +function pg_simplepay_node_insert($node) { + db_insert('pg_simplepay_price') + ->fields(array( + 'price' => $node->pg_simplepay_price, + 'nid' => $node->nid, + 'vid' => $node->vid, + )) + ->execute(); } /** - * Implements hook_menu(). + * Implements hook_node_delete(). */ -function pg_simplepay_menu() { - $items['admin/pgdata/simplepay'] = array( - 'title' => 'Simple Pay Settings', - 'description' => 'Configure default settings for Simple Pay.', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('pg_simplepay_common'), - 'access callback' => 'user_access', - 'access arguments' => array('administer pgapi'), - 'file' => 'pg_simplepay.admin.inc', - 'type' => MENU_NORMAL_ITEM, - ); - - $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'), - 'file' => 'pg_simplepay.admin.inc', - ); +function pg_simplepay_node_delete($node) { + db_delete('pg_simplepay_price') + ->condition('nid', $node->nid) + ->condition('vid', $node->vid) + ->execute(); +} - $items['simplepay/complete/%txnid'] = array( - 'title' => 'Payment completed', - 'page callback' => 'pg_simplepay_complete', - 'page arguments' => array(2), - 'access callback' => TRUE, - 'type' => MENU_CALLBACK, - ); +/** + * Implements hook_node_update(). + */ +function pg_simplepay_node_update($node) { + $simplepay_price = db_select('pg_simplepay_price', 'p') + ->fields('p') + ->condition('nid', $node->nid) + ->condition('vid', $node->vid) + ->execute() + ->fetchObject(); + + if ($simplepay_price) { + db_update('pg_simplepay_price') + ->fields(array( + 'price' => $node->pg_simplepay_price, + )) + ->condition('nid', $node->nid) + ->condition('vid', $node->vid) + ->execute(); + } + else { + db_insert('pg_simplepay_price') + ->fields(array( + 'price' => $node->pg_simplepay_price, + 'nid' => $node->nid, + 'vid' => $node->vid, + )) + ->execute(); + } +} - $items['simplepay/fail/%txnid'] = array( - 'title' => 'Payment failed', - 'page callback' => 'pg_simplepay_fail', - 'page arguments' => array(2), - 'access callback' => TRUE, - 'type' => MENU_CALLBACK, - ); +/** + * Implements hook_node_view(). + */ +function pg_simplepay_node_view($node, $view_mode) { + if (!user_access('pg_simplepay free access') && variable_get('pg_simplepay_allowed_' . $node->type, 0) && $node->pg_simplepay_price > 0) { + if ($view_mode == 'teaser') { + if (!pg_simplepay_is_paid($node->nid)) { + $node2 = node_build_content($node, TRUE); + $node->content['body']['#value'] = $node2->teaser; + + if (!empty($node->content['files']['#value'])) { + $node->content['files']['#value'] = t('Please buy access to see attached files.'); + } - return $items; + $node->content['pg_simplepay_link'] = array( + '#markup' => l(t('Buy access'), 'simplepay/' . $object->nid, array('title' => t('Buy access'))), + ); + } + } + } } - /** - * Pgapi Gateway Hooks. + * Implements hook_pgapi_format_price(). */ - function pg_simplepay_pgapi_format_price() { $format['decimals'] = variable_get('pg_simplepay_decimal_places', 2); $format['dec_point'] = variable_get('pg_simplepay_decimal', '.'); $format['thousands_sep'] = variable_get('pg_simplepay_thousands', ','); $format['position'] = variable_get('pg_simplepay_position', TRUE); + return $format; } +/** + * Implements hook_pgapi_transaction(). + */ function pg_simplepay_pgapi_transaction($op, &$t) { switch ($op) { case PG_COMPLETED: @@ -211,12 +242,13 @@ function pg_simplepay_pgapi_transaction($op, &$t) { $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: @@ -227,9 +259,18 @@ function pg_simplepay_pgapi_transaction($op, &$t) { $t->workflow = pgapi_get_workflow_id('canceled'); break; } + return $t; } +/** + * Return page url with status payment. + * + * @param object $t + * Payment object. + * + * @return string + */ function pg_simplepay_pgapi_callback($t) { switch ($t->status) { case PG_COMPLETED: @@ -253,7 +294,7 @@ function pg_simplepay_prepay($node) { $t->uid = $user->uid; $t->title = t('You have been ordered !title', array( '!title' => l($node->title, 'node/' . $node->nid, array('html' => TRUE)), - )); + )); $ses = $_COOKIE[session_name()]; @@ -261,12 +302,22 @@ function pg_simplepay_prepay($node) { 'nid' => $node->nid, 'session' => $ses, ); + $t = pgapi_transaction_save($t); $payment_url = url('payment/' . $t->txnid, array('absolute' => TRUE)); drupal_goto($payment_url); + return ''; } +/** + * Page after unsuccessful completion of payment. + * + * @param object $t + * Payment object. + * + * @return string + */ function pg_simplepay_fail($t) { global $user; @@ -277,7 +328,14 @@ function pg_simplepay_fail($t) { return theme('pg_simplepay_fail', $t); } - +/** + * Page after the successful completion of payment. + * + * @param object $t + * Payment object. + * + * @return string + */ function pg_simplepay_complete($t) { global $user; @@ -288,17 +346,24 @@ function pg_simplepay_complete($t) { return theme('pg_simplepay_complete', $t); } - /** - * Internal functions. + * Payment is paid. + * + * @param int $nid + * Node NID. + * + * @param object $account + * User object. + * + * @return bool */ - function pg_simplepay_is_paid($nid, $account = NULL) { global $user; if (!$account) { $account = $user; } + $ret = FALSE; if ($account->uid == 0) { $ses = $_COOKIE[session_name()]; @@ -309,10 +374,10 @@ function pg_simplepay_is_paid($nid, $account = NULL) { ->condition('uid', $account->uid) ->execute() ->fetch(); + if (!empty($pr)) { $ret = TRUE; - }; - + } } else { $pr = db_select('pg_simplepay_payment', 'psp') @@ -321,17 +386,18 @@ function pg_simplepay_is_paid($nid, $account = NULL) { ->condition('uid', $account->uid) ->execute() ->fetch(); + if (!empty($pr)) { $ret = TRUE; - }; + } } + return $ret; } /** - * Functions theme. + * Implements theme_THEMENAME(). */ - function theme_pg_simplepay_fail($t) { $header = array( array('data' => $t->title, 'colspan' => '2'), @@ -339,33 +405,37 @@ function theme_pg_simplepay_fail($t) { $rows = array(); - $row = array(); - $row[] = t('Price'); - $row[] = t('!currency !amount', array( - '!amount' => $t->amount, - '!currency' => variable_get('pg_simplepay_symbol', '$'), - )); - $rows[] = $row; + $rows[] = array( + t('Price'), + t('!currency !amount', array( + '!amount' => $t->amount, + '!currency' => variable_get('pg_simplepay_symbol', '$'), + )), + ); - $row = array(); - $row[] = t('Status'); - $row[] = pgapi_get_status($t->status); - $rows[] = $row; + $rows[] = array( + t('Status'), + pgapi_get_status($t->status) + ); - $row = array(); - $row[] = t('By method'); - $row[] = module_invoke($t->method, 'pgapi_gw', 'display name'); - $rows[] = $row; + $rows[] = array( + t('By method'), + module_invoke($t->method, 'pgapi_gw', 'display name'), + ); - $row = array(); - $row[] = t('Reason'); - $row[] = pgapi_get_status($t->description); - $rows[] = $row; + $rows[] = array( + t('Reason'), + pgapi_get_status($t->description), + ); $output = theme('table', $header, $rows); + return $output; } +/** + * Implements theme_THEMENAME(). + */ function theme_pg_simplepay_complete($t) { $header = array( array('data' => $t->title, 'colspan' => '2'), @@ -373,30 +443,31 @@ function theme_pg_simplepay_complete($t) { $rows = array(); - $row = array(); - $row[] = t('Price'); - $row[] = t('!currency !amount', array( - '!amount' => $t->amount, - '!currency' => variable_get('pg_simplepay_symbol', '$'), - )); - $rows[] = $row; + $rows[] = array( + t('Price'), + t('!currency !amount', array( + '!amount' => $t->amount, + '!currency' => variable_get('pg_simplepay_symbol', '$'), + )), + ); - $row = array(); - $row[] = t('Status'); - $row[] = pgapi_get_status($t->status); - $rows[] = $row; + $rows[] = array( + t('Status'), + pgapi_get_status($t->status), + ); - $row = array(); - $row[] = t('By method'); - $row[] = module_invoke($t->method, 'pgapi_gw', 'display name'); - $rows[] = $row; + $rows[] = array( + t('By method'), + module_invoke($t->method, 'pgapi_gw', 'display name'), + ); - $row = array(); - $row[] = t('Node URL'); $node = node_load($t->extra['nid']); - $row[] = l($node->title, 'node/' . $node->nid); - $rows[] = $row; + $rows[] = array( + t('Node URL'), + l($node->title, 'node/' . $node->nid), + ); $output = theme('table', $header, $rows); + return $output; }