View source
<?php
include_once dirname(__FILE__) . '/payment.ui.inc';
include_once dirname(__FILE__) . '/payment.classes.inc';
define('PAYMENT_STATUS_MONEY_TRANSFERRED', 'payment_status_money_transferred');
define('PAYMENT_STATUS_MONEY_NOT_TRANSFERRED', 'payment_status_money_not_transferred');
define('PAYMENT_STATUS_UNKNOWN', 'payment_status_unknown');
define('PAYMENT_STATUS_NEW', 'payment_status_new');
define('PAYMENT_STATUS_PENDING', 'payment_status_pending');
define('PAYMENT_STATUS_SUCCESS', 'payment_status_success');
define('PAYMENT_STATUS_FAILED', 'payment_status_failed');
define('PAYMENT_STATUS_CANCELLED', 'payment_status_cancelled');
define('PAYMENT_STATUS_EXPIRED', 'payment_status_expired');
define('PAYMENT_STATUS_AUTHORIZATION_FAILED', 'payment_status_authorization_failed');
define('PAYMENT_LINE_ITEM_TOKEN_PREFIX', 'line_item-');
define('PAYMENT_MINIMUM_AMOUNT', 0);
function payment_init() {
if (preg_match('#^admin/config/services/payment/method#', $_GET['q']) || preg_match('#^admin/content/payment#', $_GET['q']) || preg_match('#^user/\\d+?/payment#', $_GET['q'])) {
drupal_add_css(drupal_get_path('module', 'payment') . '/css/payment.css');
}
}
function payment_module_implements_alter(array &$implementations, $hook) {
if ($hook == 'payment_method_load') {
unset($implementations['commerce']);
}
if (in_array($hook, array(
'payment_update',
'payment_insert',
))) {
$group = $implementations['payment'];
unset($implementations['payment']);
$implementations = array(
'payment' => $group,
) + $implementations;
}
}
function payment_menu() {
$items['payment/%entity_object'] = array(
'load arguments' => array(
'payment',
),
'title' => 'Payment',
'title callback' => 'payment_title',
'title arguments' => array(
1,
),
'page callback' => 'payment_page_payment_view',
'page arguments' => array(
1,
),
'access callback' => 'payment_access',
'access arguments' => array(
'view',
1,
),
'file' => 'payment.ui.inc',
);
$items['payment/%entity_object/view'] = array(
'load arguments' => array(
'payment',
),
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'weight' => -10,
);
$items['payment/%entity_object/edit'] = array(
'load arguments' => array(
'payment',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'payment_form_standalone',
1,
),
'access callback' => 'payment_access',
'access arguments' => array(
'update',
1,
),
'title' => 'Edit',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'weight' => -9,
'file' => 'payment.ui.inc',
);
$items['payment/%entity_object/delete'] = array(
'load arguments' => array(
'payment',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'payment_form_payment_delete',
1,
),
'access callback' => 'payment_access',
'access arguments' => array(
'delete',
1,
),
'title' => 'Delete',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => -8,
'file' => 'payment.ui.inc',
);
$items['admin/config/services/payment'] = array(
'title' => 'Payment',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array(
'access administration pages',
),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/config/services/payment/global'] = array(
'title' => 'Global configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'payment_form_global_configuration',
),
'access arguments' => array(
'payment.global.administer',
),
'file' => 'payment.ui.inc',
);
$items['admin/config/services/payment/method/add'] = array(
'title' => 'Add payment method',
'page callback' => 'payment_page_payment_method_add_select_controller',
'access callback' => 'payment_page_payment_method_add_select_controller_access',
'file' => 'payment.ui.inc',
'type' => MENU_NORMAL_ITEM | MENU_LOCAL_ACTION,
'weight' => 1,
);
$items['admin/config/services/payment/method/add/%payment_method_controller'] = array(
'title' => 'Add payment method',
'title callback' => 'payment_method_form_add_title',
'title arguments' => array(
6,
),
'page callback' => 'payment_method_form_add',
'page arguments' => array(
6,
),
'access callback' => 'payment_method_form_add_access',
'access arguments' => array(
6,
),
'file' => 'payment.ui.inc',
'type' => MENU_CALLBACK,
);
$items['admin/config/services/payment/method/%entity_object'] = array(
'load arguments' => array(
'payment_method',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'payment_form_payment_method',
5,
),
'access callback' => 'payment_method_access',
'access arguments' => array(
'update',
5,
),
'title' => 'Payment method',
'title callback' => 'payment_method_title',
'title arguments' => array(
5,
),
'file' => 'payment.ui.inc',
);
$items['admin/config/services/payment/method/%entity_object/edit'] = array(
'load arguments' => array(
'payment_method',
),
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
);
$items['admin/config/services/payment/method/%entity_object/enable'] = array(
'load arguments' => array(
'payment_method',
),
'page callback' => 'payment_page_method_enable_disable',
'page arguments' => array(
'enable',
5,
),
'access callback' => 'payment_method_access_token',
'access arguments' => array(
7,
'enable',
5,
),
'title' => 'Enable',
'file' => 'payment.ui.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 1,
);
$items['admin/config/services/payment/method/%entity_object/disable'] = array(
'load arguments' => array(
'payment_method',
),
'page callback' => 'payment_page_method_enable_disable',
'page arguments' => array(
'disable',
5,
),
'access callback' => 'payment_method_access_token',
'access arguments' => array(
7,
'disable',
5,
),
'title' => 'Disable',
'file' => 'payment.ui.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 1,
);
$items['admin/config/services/payment/method/%entity_object/clone'] = array(
'load arguments' => array(
'payment_method',
),
'page callback' => 'payment_page_method_clone',
'page arguments' => array(
5,
),
'access callback' => 'payment_method_access',
'access arguments' => array(
'clone',
5,
),
'title' => 'Clone',
'file' => 'payment.ui.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 2,
);
$items['admin/config/services/payment/method/%entity_object/delete'] = array(
'load arguments' => array(
'payment_method',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'payment_form_payment_method_delete',
5,
),
'access callback' => 'payment_method_access',
'access arguments' => array(
'delete',
5,
),
'title' => 'Delete',
'file' => 'payment.ui.inc',
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'weight' => 3,
);
$items['admin/config/services/payment/status'] = array(
'title' => 'Payment statuses',
'page callback' => 'payment_page_status',
'access arguments' => array(
'payment.payment_status.view',
),
'file' => 'payment.ui.inc',
'weight' => 1,
);
$base = array(
'page callback' => 'payment_page_required_modules',
'page arguments' => array(
array(
'views' => 'Views',
),
),
'file' => 'payment.ui.inc',
'menu_name' => 'management',
);
if (!module_exists('views')) {
$items['admin/content/payment'] = array(
'title' => 'Payments',
'access callback' => 'payment_access',
'access arguments' => array(
'view',
),
) + $base;
$items['admin/config/services/payment/method'] = array(
'title' => 'Payment methods',
'access callback' => 'payment_method_access',
'access arguments' => array(
'view',
),
) + $base;
$items['user/%user/payment'] = array(
'title' => 'Payments',
'access arguments' => array(
'payment.payment.view.own',
),
) + $base;
}
return $items;
}
function payment_menu_alter(&$items) {
if (isset($items['admin/config/services/payment/payment/fields'])) {
$items['admin/config/services/payment/payment'] = array(
'title' => 'Payments',
'type' => MENU_NORMAL_ITEM,
) + $items['admin/config/services/payment/payment/fields'];
$items['admin/config/services/payment/payment/fields'] = array(
'title' => 'Manage fields',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
}
}
function payment_permission() {
$permissions = array(
'payment.global.administer' => array(
'title' => t('Administer global Payment configuration'),
),
'payment.rules.administer' => array(
'title' => t('Administer rule configurations'),
'restrict access' => TRUE,
),
'payment.payment.administer' => array(
'title' => t('Administer payment configuration'),
),
'payment.payment.view.any' => array(
'title' => t('View any payment'),
),
'payment.payment.view.own' => array(
'title' => t('View own payments'),
),
'payment.payment.update.any' => array(
'title' => t('Update any payment'),
),
'payment.payment.update.own' => array(
'title' => t('Update own payments'),
),
'payment.payment.delete.any' => array(
'title' => t('Delete any payment'),
),
'payment.payment.delete.own' => array(
'title' => t('Delete own payments'),
),
'payment.payment_method.update.any' => array(
'title' => t('Update any payment method'),
'restrict access' => TRUE,
),
'payment.payment_method.update.own' => array(
'title' => t('Update own payment methods'),
),
'payment.payment_method.delete.any' => array(
'title' => t('Delete any payment method'),
),
'payment.payment_method.delete.own' => array(
'title' => t('Delete own payment methods'),
),
'payment.payment_method.view.any' => array(
'title' => t('View any payment method'),
'restrict access' => TRUE,
),
'payment.payment_method.view.own' => array(
'title' => t('View own payment methods'),
),
'payment.payment_status.view' => array(
'title' => t('View payment status overview'),
),
);
$controllers = payment_method_controller_load_multiple();
unset($controllers['PaymentMethodControllerUnavailable']);
foreach ($controllers as $controller) {
$permissions['payment.payment_method.create.' . $controller->name] = array(
'title' => t('Create %controller_title payment methods', array(
'%controller_title' => $controller->title,
)),
);
}
return $permissions;
}
function payment_hook_info() {
$hooks['payment_status_info'] = array(
'group' => 'payment',
);
$hooks['payment_method_controller_info'] = array(
'group' => 'payment',
);
$hooks['payment_line_item_info'] = array(
'group' => 'payment',
);
$hooks['payment_status_change'] = array(
'group' => 'payment',
);
$hooks['payment_pre_execute'] = array(
'group' => 'payment',
);
$hooks['payment_pre_finish'] = array(
'group' => 'payment',
);
$hooks['payment_validate'] = array(
'group' => 'payment',
);
return $hooks;
}
function payment_field_extra_fields() {
$field['payment']['payment'] = array(
'form' => array(
'payment_line_items' => array(
'label' => t('Line items'),
'weight' => 0,
),
'payment_method' => array(
'label' => t('Payment method selection and configuration'),
'weight' => 0,
),
'payment_status' => array(
'label' => t('Status'),
'weight' => 0,
),
),
'display' => array(
'payment_line_items' => array(
'label' => t('Line items'),
'weight' => 0,
),
'payment_method' => array(
'label' => t('Payment method (generic title)'),
'weight' => 0,
),
'payment_status_items' => array(
'label' => t('Status items'),
'weight' => 0,
),
),
);
return $field;
}
function payment_entity_info() {
$entity_info['payment'] = array(
'label' => t('Payment'),
'controller class' => 'PaymentEntityController',
'entity class' => 'Payment',
'module' => 'payment',
'base table' => 'payment',
'entity keys' => array(
'id' => 'pid',
),
'label callback' => 'payment_label',
'uri callback' => 'payment_uri',
'fieldable' => TRUE,
'bundles' => array(
'payment' => array(
'label' => t('Payment'),
'admin' => array(
'path' => 'admin/config/services/payment/payment',
'access arguments' => array(
'payment.payment.administer',
),
),
),
),
'access callback' => 'payment_access',
);
$entity_info['payment_method'] = array(
'label' => t('Payment method'),
'controller class' => 'PaymentMethodEntityController',
'features controller class' => 'PaymentMethodFeaturesController',
'entity class' => 'PaymentMethod',
'module' => 'payment',
'base table' => 'payment_method',
'entity keys' => array(
'id' => 'pmid',
'label' => 'title_specific',
'name' => 'name',
),
'uri callback' => 'payment_method_uri',
'exportable' => TRUE,
'access callback' => 'payment_method_access',
);
return $entity_info;
}
function payment_label(Payment $entity) {
return t($entity->description, $entity->description_arguments);
}
function payment_entity_property_info() {
$properties['payment']['properties']['context'] = array(
'description' => t("The machine-readable name of the context that created the payment."),
'label' => t('Context'),
'required' => TRUE,
'schema field' => 'context',
);
$properties['payment']['properties']['currency_code'] = array(
'description' => t('A three-letter ISO 4217 currency code.'),
'label' => t('Currency code'),
'required' => TRUE,
'schema field' => 'currency_code',
);
$properties['payment']['properties']['description'] = array(
'label' => t('Description'),
'schema field' => 'description',
'getter callback' => 'payment_entity_property_get',
);
$properties['payment']['properties']['finish_callback'] = array(
'description' => t('The name of the function to call once payment processing is completed.'),
'label' => t('Finish callback'),
'required' => TRUE,
'schema field' => 'finish_callback',
);
$properties['payment']['properties']['method'] = array(
'label' => t('Payment method'),
'required' => TRUE,
'type' => 'payment_method',
);
$properties['payment']['properties']['pid'] = array(
'label' => t('Payment ID'),
'schema field' => 'pid',
'type' => 'integer',
);
$properties['payment']['properties']['pmid'] = array(
'label' => t('Payment method ID'),
'schema field' => 'pmid',
'type' => 'integer',
);
$properties['payment']['properties']['uid'] = array(
'label' => t('User ID'),
'description' => t('The ID of the user this payment belongs to.'),
'required' => TRUE,
'schema field' => 'uid',
'type' => 'integer',
);
$properties['payment_method']['properties']['controller_class_name'] = array(
'label' => t('Payment method type'),
'schema field' => 'controller_class_name',
);
$properties['payment_method']['properties']['enabled'] = array(
'description' => t('Whether the payment method is enabled and can be used.'),
'label' => t('Enabled'),
'schema field' => 'enabled',
'type' => 'boolean',
);
$properties['payment_method']['properties']['pmid'] = array(
'label' => t('Payment method ID'),
'schema field' => 'pmid',
'type' => 'integer',
);
$properties['payment_method']['properties']['title_specific'] = array(
'label' => t('Title (specific)'),
'required' => TRUE,
'schema field' => 'title_specific',
);
$properties['payment_method']['properties']['title_generic'] = array(
'label' => t('Title (generic)'),
'schema field' => 'title_generic',
);
$properties['payment_method']['properties']['uid'] = array(
'label' => t('User ID'),
'description' => t('The ID of the user this payment method belongs to.'),
'required' => TRUE,
'schema field' => 'uid',
'type' => 'integer',
);
return $properties;
}
function payment_entity_property_get(Payment $payment, array $options, $name, $type, $info) {
switch ($name) {
case 'description':
return payment_label($payment);
default:
return entity_property_verbatim_get($payment, $options, $name, $type, $info);
}
}
function payment_element_info() {
$elements['payment_method'] = array(
'#input' => TRUE,
'#process' => array(
'payment_form_process_method',
),
'#element_validate' => array(
'payment_form_process_method_validate',
),
'#pmids' => array(),
);
$elements['payment_amount'] = array(
'#input' => TRUE,
'#process' => array(
'payment_form_process_amount',
),
'#element_validate' => array(
'payment_form_process_amount_validate',
),
'#minimum_amount' => FALSE,
'#currency_code' => 'XXX',
);
$elements['payment_line_item'] = array(
'#input' => TRUE,
'#tree' => TRUE,
'#process' => array(
'payment_form_process_line_item',
),
'#attached' => array(
'css' => array(
drupal_get_path('module', 'payment') . '/css/payment.css',
),
),
'#element_validate' => array(
'payment_form_process_line_item_validate',
),
'#currency_code' => 'XXX',
'#cardinality' => 0,
);
$elements['payment_form_context'] = array(
'#tree' => TRUE,
'#process' => array(
'payment_form_process_context',
),
'#payment_method_controller_name' => '',
'#callback_type' => '',
);
return $elements;
}
function payment_views_api() {
return array(
'api' => '2',
'path' => drupal_get_path('module', 'payment') . '/views',
);
}
function payment_statuses_info() {
$statuses_info =& drupal_static(__FUNCTION__);
if (!$statuses_info) {
$statuses_info = array();
foreach (module_invoke_all('payment_status_info') as $status_info) {
$statuses_info[$status_info->status] = $status_info;
}
drupal_alter('payment_status_info', $statuses_info);
}
return $statuses_info;
}
function payment_status_info($status, $unknown = FALSE) {
$statuses_info = payment_statuses_info();
return isset($statuses_info[$status]) ? $statuses_info[$status] : ($unknown ? $statuses_info[PAYMENT_STATUS_UNKNOWN] : FALSE);
}
function payment_line_items_info() {
$line_items_info =& drupal_static(__FUNCTION__);
if (is_null($line_items_info)) {
$line_items_info = array();
foreach (module_invoke_all('payment_line_item_info') as $line_item_info) {
$line_items_info[$line_item_info->name] = $line_item_info;
}
drupal_alter('payment_line_item_info', $line_items_info);
}
return $line_items_info;
}
function payment_line_item_info($name) {
$line_items_info = payment_line_items_info();
return isset($line_items_info[$name]) ? $line_items_info[$name] : FALSE;
}
function payment_status_has_ancestor($status, $ancestor_status) {
return in_array($ancestor_status, payment_status_info($status, TRUE)
->ancestors());
}
function payment_status_is_or_has_ancestor($status, $ancestor_status) {
return $status == $ancestor_status || in_array($ancestor_status, payment_status_info($status, TRUE)
->ancestors());
}
function payment_method_controllers_info() {
$controllers_info =& drupal_static(__FUNCTION__);
if (!$controllers_info) {
$controllers_info = module_invoke_all('payment_method_controller_info');
foreach ($controllers_info as $controller_class_name_alias => $controller_class_name) {
if (is_numeric($controller_class_name_alias)) {
unset($controllers_info[$controller_class_name_alias]);
$controllers_info[$controller_class_name] = $controller_class_name;
}
}
drupal_alter('payment_method_controller_info', $controllers_info);
}
return $controllers_info;
}
function payment_modules_enabled() {
drupal_static_reset();
}
function payment_modules_disabled() {
drupal_static_reset();
}
function payment_method_controller_load($controller_class_name) {
$controllers = payment_method_controller_load_multiple(array(
$controller_class_name,
));
return reset($controllers);
}
function payment_method_controller_load_multiple(array $controller_class_names = array()) {
$controllers = NULL;
if (is_null($controllers)) {
foreach (payment_method_controllers_info() as $controller_class_name_alias => $controller_class_name) {
$controller = new $controller_class_name();
$controller->name = $controller_class_name_alias;
$controllers[$controller_class_name_alias] = $controller;
}
}
$controllers += array_fill_keys(array_diff($controller_class_names, array_keys($controllers)), FALSE);
return $controller_class_names ? array_intersect_key($controllers, array_flip($controller_class_names)) : array_filter($controllers);
}
function payment_amount_human_readable($amount, $currency_code = NULL) {
if (payment_currency_version() === 2) {
$currency = currency_load($currency_code);
if ($currency) {
return $currency
->format($amount);
}
}
$amount_string = number_format($amount, 2, '.', '');
$decimal_separator_position = strpos($amount_string, '.');
$arguments = array(
'!currency_code' => $currency_code,
'!units' => substr($amount_string, 0, $decimal_separator_position),
'!cents' => substr($amount_string, $decimal_separator_position + 1),
);
return $currency_code ? t('!currency_code !units.!cents', $arguments) : t('!units.!cents', $arguments);
}
function payment_access($operation, Payment $payment = NULL, $account = NULL) {
global $user;
if (!$account) {
$account = $user;
}
switch ($operation) {
case 'create':
return TRUE;
case 'view':
case 'update':
case 'delete':
return user_access('payment.payment.' . $operation . '.any', $account) || $payment && user_access('payment.payment.' . $operation . '.own', $account) && $account->uid == $payment->uid;
}
return FALSE;
}
function payment_line_item_get_specific($name, Payment $payment) {
return isset($payment->line_items[$name]) ? array(
$payment->line_items[$name],
) : array();
}
function payment_line_item_get_all($name, Payment $payment) {
return $payment->line_items;
}
function payment_line_item_get_prefixed($name, Payment $payment) {
$selection = array();
foreach ($payment->line_items as $line_item) {
if (strpos($line_item->name, $name) === 0) {
$selection[] = $line_item;
}
}
return $selection;
}
function payment_rules_access($type, $name) {
return user_access('payment.rules.administer');
}
function payment_method_access($operation, PaymentMethod $payment_method = NULL, $account = NULL) {
global $user;
if (!$account) {
$account = $user;
}
switch ($operation) {
case 'create':
return $payment_method && user_access('payment.payment_method.create.' . $payment_method->controller->name, $account);
case 'enable':
return $payment_method && $payment_method->enabled == FALSE && payment_method_access('update', $payment_method, $account);
case 'disable':
return $payment_method && $payment_method->enabled == TRUE && payment_method_access('update', $payment_method, $account);
case 'clone':
return payment_method_access('create', $payment_method, $account) && payment_method_access('view', $payment_method, $account);
case 'view':
case 'update':
case 'delete':
return user_access('payment.payment_method.' . $operation . '.any', $account) || $payment_method && user_access('payment.payment_method.' . $operation . '.own', $account) && $payment_method->uid == $account->uid;
}
return FALSE;
}
function payment_method_access_token($token, $operation, PaymentMethod $payment_method = NULL, $account = NULL) {
return drupal_valid_token($token, 'payment') && payment_method_access($operation, $payment_method, $account);
}
function payment_debug($message, $file = '', $line = 0) {
if (variable_get('payment_debug', TRUE)) {
if (!$file || !$line) {
if (version_compare(phpversion(), '5.3.6') == -1) {
$backtrace = debug_backtrace(FALSE);
}
elseif (version_compare(phpversion(), '5.4.0') > -1) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 0);
}
else {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}
$caller = _drupal_get_last_caller($backtrace);
if (!$file) {
$file = $caller['file'];
}
if (!$line) {
$line = $caller['line'];
}
}
$link = l(t('Do not log or display Payment debugging messages'), 'admin/config/services/payment/global');
$string = 'Debug: %message on line !line in @file (!hide).';
$string_arguments = array(
'%message' => $message,
'!line' => $line,
'@file' => $file,
'!hide' => $link,
);
drupal_set_message(t($string, $string_arguments));
watchdog('Payment', $string, $string_arguments);
}
}
function payment_currency_version() {
if (module_exists('currency') && function_exists('currency_load')) {
return 2;
}
elseif (module_exists('currency_api')) {
return 1;
}
return FALSE;
}
function payment_currency_options() {
$version = payment_currency_version();
if ($version == 2) {
return currency_options();
}
if ($version == 1) {
return currency_api_get_list();
}
return array();
}
function payment_uri(Payment $payment) {
return array(
'path' => 'payment/' . $payment->pid,
);
}
function payment_method_uri(PaymentMethod $payment_method) {
return array(
'path' => 'admin/config/services/payment/method/' . $payment_method->pmid,
);
}
function _payment_own_overview_views_handler_access($account = NULL) {
global $user;
if (!$account) {
$account = $user;
}
return user_access('payment.payment.view.any') || user_access('payment.payment.view.own') && $account->uid == arg(1);
}
function payment_payment_update($payment) {
entity_get_controller('payment')
->saveAttachedData($payment);
}
function payment_payment_insert($payment) {
entity_get_controller('payment')
->saveAttachedData($payment);
}