View source
<?php
function webform_paypal_webform_component_info() {
$components = array();
$components['paypal'] = array(
'label' => 'Paypal Button',
'description' => 'Paypal button to replace the submit button for this form',
'features' => array(
'title_display' => FALSE,
),
);
return $components;
}
function webform_paypal_permission() {
return array(
'update payment status' => array(
'title' => t('Update PayPal Payment Status'),
),
'use paypal sandbox' => array(
'title' => t('Use PayPal in Sandbox Mode'),
),
);
}
function _webform_defaults_paypal() {
return array(
'value' => '',
'extra' => array(
'private' => 1,
'transaction' => 'live',
),
);
}
function _webform_theme_paypal() {
return array(
'webform_display_paypal' => array(
'render element' => 'element',
),
);
}
function _webform_edit_paypal($component) {
$form = array();
$form['value'] = array(
'#title' => t('Paypal Button ID'),
'#type' => 'textfield',
'#required' => TRUE,
'#default_value' => $component['value'],
'#element_validate' => array(
'_webform_edit_paypal_validate',
),
'#description' => "The id of the button generated within your Paypal account. This can be found by visiting Merchant Services > My Saved Buttons or by copying the value for 'hosted-button-id' in the button code provided by Paypal",
);
if (user_access('use paypal sandbox')) {
$form['paypal'] = array(
'#type' => 'fieldset',
'#title' => t('Development Settings'),
);
$form['paypal']['transaction'] = array(
"#type" => 'select',
'#title' => t('Choose the type of transaction to process.'),
'#options' => array(
'live' => 'Live Transaction',
'testing' => 'Testing/Sandbox',
),
'#description' => t('In order to use "Testing/Sandbox", you must have a <a href="https://developer.paypal.com/">developer account</a> set up with PayPal.'),
'#default_value' => $component['extra']['transaction'],
'#parents' => array(
'extra',
'transaction',
),
);
}
$type = '';
$style = '';
$language = '';
if (!empty($component['extra']['type'])) {
$type = $component['extra']['type'];
}
if (!empty($component['extra']['style'])) {
$style = $component['extra']['style'];
}
if (!empty($component['extra']['language'])) {
$language = $component['extra']['language'];
}
$form['extra']['type'] = array(
'#title' => t('Button Type'),
'#type' => 'select',
'#options' => array(
'btn_cart' => 'Shopping Cart',
'btn_buynow' => 'Buy Now',
'btn_donate' => 'Donation',
'btn_git' => 'Gift Certificate',
'btn_subscribe' => 'Subscribe',
),
'#required' => TRUE,
'#default_value' => $type,
);
$form['extra']['style'] = array(
'#title' => t('Button Style'),
'#type' => 'select',
'#options' => array(
'_SM' => 'Small',
'_LG' => 'Large',
'CC_LG' => 'Large with Credit Card logos',
),
'#required' => TRUE,
'#default_value' => $style,
);
$form['extra']['language'] = array(
'#title' => t('Button Language'),
'#type' => 'select',
'#options' => array(
'en_US' => 'English',
'es_ES' => 'Spanish',
'fr_FR' => 'French',
'it_IT' => 'Italian',
'de_DE' => 'German',
),
'#required' => TRUE,
'#default_value' => $language,
);
return $form;
}
function _webform_edit_paypal_validate($form, &$form_state) {
$value = $form_state['input']['value'];
if (preg_match("/[^A-Z0-9]/", $value)) {
form_set_error('webform_paypal', 'Please enter a valid button ID');
}
}
function _webform_render_paypal($component, $value = NULL, $filter = TRUE) {
$item = menu_get_item();
if ($value['status'] == 'paid') {
$value = 1;
}
else {
$value = 0;
}
if (strpos($item['path'], 'edit') && user_access('update payment status')) {
$element = array(
'#title' => 'Mark Status as Paid',
'#type' => 'checkbox',
'#theme_wrappers' => array(
'webform_element',
),
'#webform_component' => $component,
'#default_value' => $value,
);
}
else {
$element = array(
'#type' => 'hidden',
'#default_value' => $component['value'],
'#theme_wrappers' => array(
'webform_element',
),
'#webform_component' => $component,
);
}
return $element;
}
function _webform_submit_paypal($component, $value) {
$return = array();
$return['id'] = $component['value'];
$return['status'] = 'unpaid';
if (is_int($value) && $value == 1) {
$return['status'] = 'paid';
}
return $return;
}
function _webform_display_paypal($component, $value) {
if (!empty($value['status']) && $value['status'] == 'unpaid') {
$value = t('Submitted to PayPal');
}
if (!empty($value['status']) && $value['status'] == 'paid') {
$value = t('Submitted to PayPal - Payment Confirmed');
}
return array(
'#title' => t('PayPal Status'),
'#weight' => $component['weight'],
'#theme' => 'webform_display_paypal',
'#theme_wrappers' => array(
'webform_element',
),
'#value' => $value,
'#webform_component' => $component,
);
}
function theme_webform_display_paypal($variables) {
$element = $variables['element'];
if (empty($element['#value'])) {
return;
}
else {
return $element['#value'];
}
}
function webform_paypal_form_alter(&$form, &$form_state, $form_id) {
if (strpos($form_id, 'webform_client_form_') !== FALSE) {
if (strpos($form['#action'], 'edit')) {
$form['actions']['submit']['#value'] = t('Update');
}
else {
$webform_components = $form_state['webform']['component_tree']['children'];
foreach ($webform_components as $key => $value) {
if ($value['type'] == 'paypal') {
$form['actions']['submit']['#theme_wrappers'] = array(
"image_button",
);
$form['actions']['submit']['#button_type'] = "image";
$form['actions']['submit']['#src'] = webform_paypal_button_url($value['extra']['type'], $value['extra']['style'], $value['extra']['language']);
$form['#submit'][] = 'webform_paypal_submit';
}
}
}
}
}
function webform_paypal_button_url($type, $style, $language) {
$url = 'https://www.paypal.com/' . $language . '/i/btn/' . $type . $style . '.gif';
return $url;
}
function webform_paypal_submit(&$form, &$form_state) {
$submission_id = $form_state['values']['details']['sid'];
$nid = $form_state['values']['details']['nid'];
if (drupal_lookup_path('alias', 'node/' . $nid)) {
$node_path = drupal_lookup_path('alias', 'node/' . $nid);
}
else {
$node_path = base_path() . 'node/' . $nid;
}
global $base_url;
$submission_url = $base_url . $node_path . '/submission/' . $submission_id;
$webform_components = $form_state['webform']['component_tree']['children'];
foreach ($webform_components as $key => $value) {
if ($value['type'] == 'paypal') {
$paypal_component_name = $value['form_key'];
if ($value['extra']['transaction'] == 'live') {
$url = 'https://www.paypal.com/cgi-bin/webscr';
}
else {
$url = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
}
}
}
$post_data = array();
$post_data['cmd'] = '_s-xclick';
$post_data['hosted_button_id'] = $form['submitted'][$paypal_component_name]['#default_value'];
$post_data['custom'] = $submission_url;
drupal_goto($url, array(
'query' => $post_data,
));
}
function _webform_table_paypal($component, $value) {
if (empty($value['status'])) {
return;
}
else {
return ucfirst($value['status']);
}
}
function _webform_csv_headers_paypal($component, $export_options) {
$header = array();
$header[0] = '';
$header[1] = '';
$header[2] = $component['name'];
return $header;
}
function _webform_csv_data_paypal($component, $export_options, $value) {
if (empty($value['status'])) {
return;
}
else {
return ucfirst($value['status']);
}
}