Last active
August 29, 2015 14:05
-
-
Save eileenmcnaughton/bc40a6eabbb7e8304f09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
*/ | |
function api_payment_form_get($params) { | |
$paymentProcessor = civicrm_api3('payment_processor', 'getsingle', array('processor_id' => $params['processor_id'])); | |
$formData = paymentFormBuild($paymentProcessor, $params); | |
if ($params['output'] = 'json') { | |
return json_encode($formData); | |
} | |
return generateHTML($formData); | |
} | |
function paymentFormBuild($paymentProcessor, $params) { | |
$gateway = Omnipay::create(str_replace('omnipay_', '', $paymentProcessor['name'])); | |
if (!empty($params['country'])) { | |
$gateway->setCountry($params['country']); | |
} | |
if (!empty($params['currency'])) { | |
$gateway->setCurrency($params['currency']); | |
} | |
$gateWayFields = $gateway->getSuggestedFields(); | |
//this is civicentric - if the form builder were standalone this would need to be a hookable/ extendable call | |
$configuredFields = civicrm_api('profile', 'getfields', array('profile_id' => 'billing', 'action' => 'submit')); | |
$billingFields = array_merge(array_fill_keys($gateWayFields, $configuredFields); | |
$billingFieldsWithMetadata = addMetaData($billingFields); | |
$submitURL = ''; | |
if ($gateway->isTransactionalRedirect) { | |
$submitURL = $gateway->getSubmitURL(); | |
} | |
return array('submit' => $submitURL, 'fields' => $billingFieldsWithMetadata); | |
} | |
function addMetaData($billingFields) { | |
foreach ($billingFields as $key => $field) { | |
if (!is_array($field)) { | |
$billingFields[$key] = lookupMetadata($key); | |
} | |
$billingFields['class'][] = 'gateway'; | |
if ($billingFields['is_sensitive']) { | |
$billingFields['class'][] = 'billing-profile'; | |
} | |
} | |
return $billingFields; | |
} | |
function lookupMetadata($key) { | |
// here we need to look up a library of possible fields to fill in metadata about field length, whether it is select, options | |
// & possibly any validation or js classes | |
// & have a default of text of medium length | |
} | |
function generateHTML() { | |
// call a generic api level function that can turn a form array into html. This would be tested & would largely not care if it the form architecture | |
// changes | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment