| 1 |
<?php
|
| 2 |
/**
|
| 3 |
* @file
|
| 4 |
* API en helper module for DirectDebit payment method
|
| 5 |
*
|
| 6 |
* This module provides a user form and a config form
|
| 7 |
* Known modules using this are
|
| 8 |
* - uc_directdebit : ubercart direct debit
|
| 9 |
* - clieop : a dutch direct debit batch
|
| 10 |
*
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Returns the form fields
|
| 15 |
*
|
| 16 |
* The API is at the moment very simple ... just the form fields
|
| 17 |
*
|
| 18 |
* @return array of form field definitions
|
| 19 |
*/
|
| 20 |
function directdebit_api() {
|
| 21 |
/*
|
| 22 |
* TODO: add more field
|
| 23 |
* Account holder, Name of bank, Branch office
|
| 24 |
* Bank clearing no. (if known), Postal code and City, IBAN, Account no.
|
| 25 |
* PostalCode, Region, Country
|
| 26 |
*/
|
| 27 |
return array(
|
| 28 |
'directdebit_bank_withdraw_permission' =>
|
| 29 |
array(
|
| 30 |
'#description' => t('Give us permission to withdraw money from your bank account.'),
|
| 31 |
'#options' => array(
|
| 32 |
1 => t('I give permission to withdraw money from the given bank account.'),
|
| 33 |
),
|
| 34 |
'#required' => TRUE,
|
| 35 |
'#title' => t('I give permission'),
|
| 36 |
//'#type' => 'radios',
|
| 37 |
'#type' => 'checkboxes',
|
| 38 |
),
|
| 39 |
'directdebit_customer_reference' =>
|
| 40 |
array(
|
| 41 |
'#description' => t( "You may provide a reference for your sake. Ie 'Gifts for Pattys birdsday'"),
|
| 42 |
'#required' => TRUE,
|
| 43 |
'#title' => t('Your reference'),
|
| 44 |
'#type' => 'textfield',
|
| 45 |
),
|
| 46 |
'directdebit_bank_account_name' =>
|
| 47 |
array(
|
| 48 |
'#description' => t( "What is your name as you are registered at your bank. Ie 'Mr Smith'"),
|
| 49 |
'#required' => TRUE,
|
| 50 |
'#title' => t('Name'),
|
| 51 |
'#type' => 'textfield',
|
| 52 |
),
|
| 53 |
'directdebit_bank_account_street' =>
|
| 54 |
array(
|
| 55 |
'#description' => t( "What is your street as you are registered at your bank. Ie '22 Rockroad'"),
|
| 56 |
'#required' => TRUE,
|
| 57 |
'#title' => t('Street'),
|
| 58 |
'#type' => 'textfield',
|
| 59 |
),
|
| 60 |
'directdebit_bank_account_city' =>
|
| 61 |
array(
|
| 62 |
'#description' => t( "What is your city as you are registered at your bank. Ie 'Bedrock City'"),
|
| 63 |
'#required' => TRUE,
|
| 64 |
'#title' => t('City'),
|
| 65 |
'#type' => 'textfield',
|
| 66 |
),
|
| 67 |
'directdebit_bank_account_number' =>
|
| 68 |
array(
|
| 69 |
'#description' => t("What is your account number. Ie '0123456789'"),
|
| 70 |
'#title' => t('Bank account number'),
|
| 71 |
'#required' => TRUE,
|
| 72 |
'#type' => 'textfield',
|
| 73 |
),
|
| 74 |
'directdebit_bank_name' =>
|
| 75 |
array(
|
| 76 |
'#description' => t("What is the name of your bank. Ie 'Royal Bank of Scotland'"),
|
| 77 |
'#title' => t('Name of your bank'),
|
| 78 |
'#required' => TRUE,
|
| 79 |
'#type' => 'textfield',
|
| 80 |
),
|
| 81 |
'directdebit_site_reference' =>
|
| 82 |
array(
|
| 83 |
'#description' => t("Our reference appearring on the direct debit bank transaction. Ie 'AF123-BD432-PQ654-DD129"),
|
| 84 |
'#title' => t('Our reference'),
|
| 85 |
'#required' => TRUE,
|
| 86 |
'#type' => 'textfield',
|
| 87 |
'#disabled' => TRUE,
|
| 88 |
),
|
| 89 |
);
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Extract the given form field value from the directdebit API
|
| 94 |
*
|
| 95 |
* @param $search_key key to extract
|
| 96 |
*
|
| 97 |
* @return array of all formfield-keys and the value of the search_key field value
|
| 98 |
*/
|
| 99 |
function directdebit_api_extract( $search_key= NULL) {
|
| 100 |
if ( isset( $search_key)) {
|
| 101 |
$result= array();
|
| 102 |
foreach ( directdebit_api() as $key => $value) {
|
| 103 |
$result[$key]= $value[$search_key];
|
| 104 |
}
|
| 105 |
return $result;
|
| 106 |
}
|
| 107 |
else {
|
| 108 |
return array_keys( directdebit_api());
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
/**
|
| 113 |
* Implementation of hook_form()
|
| 114 |
*
|
| 115 |
* TODO: this is ubercart centric @see directdebit_process_post
|
| 116 |
*
|
| 117 |
* @param $order could be an $_POST array or a order object
|
| 118 |
* @return a drupal form
|
| 119 |
*/
|
| 120 |
function directdebit_form($order=null) {
|
| 121 |
// Some datamassage is needed
|
| 122 |
$data=array();
|
| 123 |
if ( isset( $order)) {
|
| 124 |
if ( isset($order->payment_details)) {
|
| 125 |
$data = $order->payment_details;
|
| 126 |
}
|
| 127 |
else if ( is_array( $order)) {
|
| 128 |
$data = $order;
|
| 129 |
}
|
| 130 |
}
|
| 131 |
// end of massage
|
| 132 |
|
| 133 |
$fields_available = directdebit_api();
|
| 134 |
$fields_to_use = variable_get('directdebit_field_list', array( 'directdebit_bank_withdraw_permission'));
|
| 135 |
$form = array();
|
| 136 |
|
| 137 |
foreach ( $fields_to_use as $key => $value ) {
|
| 138 |
if ( $value) {
|
| 139 |
$form[$key]= $fields_available[$key];
|
| 140 |
|
| 141 |
if ( $key == 'directdebit_bank_withdraw_permission') {
|
| 142 |
// this is a checkbox so change to option array
|
| 143 |
$form[$key]['#default_value'] = array( 1 => ($data[$key]== 1 ? 1 : 0));
|
| 144 |
}
|
| 145 |
else {
|
| 146 |
$form[$key]['#default_value'] = $data[$key];
|
| 147 |
}
|
| 148 |
}
|
| 149 |
}
|
| 150 |
return $form;
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Implementation of hook_validate()
|
| 155 |
*/
|
| 156 |
function directdebit_form_validate( $form_id, $form_values) {
|
| 157 |
$fields_to_use = variable_get('directdebit_field_list', array( 'directdebit_bank_withdraw_permission'));
|
| 158 |
|
| 159 |
if ( $fields_to_use['directdebit_bank_withdraw_permission'] &&
|
| 160 |
( !isset($form_values['directdebit_bank_withdraw_permission']) || !$form_values['directdebit_bank_withdraw_permission'][1])) {
|
| 161 |
form_set_error('directdebit_bank_withdraw_permission', t('You must give us permission to withdraw money from your bank accuunt or choose another payment method'));
|
| 162 |
}
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Convert the POST data into a payment structure
|
| 167 |
*
|
| 168 |
* The checkboxes are converted into string.
|
| 169 |
* This is a helper function for ubercart.
|
| 170 |
*
|
| 171 |
* @return array with key value of fields to use
|
| 172 |
*/
|
| 173 |
function directdebit_process_post() {
|
| 174 |
$payment_details = array();
|
| 175 |
|
| 176 |
// Proces only the needed fields
|
| 177 |
$fields_to_use = variable_get('directdebit_field_list', array( 'directdebit_bank_withdraw_permission'));
|
| 178 |
|
| 179 |
foreach ($fields_to_use as $key => $value) {
|
| 180 |
if ( $value) {
|
| 181 |
$post = $_POST[$key];
|
| 182 |
if( $key == 'directdebit_bank_withdraw_permission'){
|
| 183 |
// check whether $post[1] == 1
|
| 184 |
$payment_details[$key] = $post[1]== 1 ? 1 : 0;
|
| 185 |
}
|
| 186 |
else {
|
| 187 |
$payment_details[$key] = check_plain( $post);
|
| 188 |
}
|
| 189 |
}
|
| 190 |
}
|
| 191 |
|
| 192 |
return $payment_details;
|
| 193 |
}
|