public function all() { $path = $this->_config->merchantPath() . '/discounts'; $response = $this->_http->get($path); $discounts = array("discount" => $response['discounts']); return Braintree_Util::extractAttributeAsArray($discounts, 'discount'); }
/** * initializes instance properties from the keys/values of an array * @ignore * @access protected * @param array $attributes array of properties to set - single level * @return none */ private function _initializeFromArray($attributes) { foreach ($attributes as $name => $value) { $varName = "_{$name}"; $this->{$varName} = Braintree_Util::delimiterToCamelCase($value, '_'); } }
public function all() { $path = $this->_config->merchantPath() . '/add_ons'; $response = $this->_http->get($path); $addOns = array("addOn" => $response['addOns']); return Braintree_Util::extractAttributeAsArray($addOns, 'addOn'); }
/** * Construct XML elements with attributes from an associative array. * * @access protected * @static * @param object $writer XMLWriter object * @param array $aData contains attributes and values * @return none */ private static function _createElementsFromArray(&$writer, $aData) { if (!is_array($aData)) { $writer->text($aData); return; } foreach ($aData as $index => $element) { // convert the style back to gateway format $elementName = Braintree_Util::camelCaseToDelimiter($index, '-'); // handle child elements $writer->startElement($elementName); if (is_array($element)) { if (array_key_exists(0, $element) || empty($element)) { $writer->writeAttribute('type', 'array'); foreach ($element as $ignored => $itemInArray) { $writer->startElement('item'); self::_createElementsFromArray($writer, $itemInArray); $writer->endElement(); } } else { self::_createElementsFromArray($writer, $element); } } else { // generate attributes as needed $attribute = self::_generateXmlAttribute($element); if (is_array($attribute)) { $writer->writeAttribute($attribute[0], $attribute[1]); $element = $attribute[2]; } $writer->text($element); } $writer->endElement(); } }
/** * Converts an XML string into a multidimensional array * * @param string $xml * @return array */ public static function arrayFromXml($xml) { $document = new DOMDocument('1.0', 'UTF-8'); $document->loadXML($xml); $root = $document->documentElement->nodeName; return Braintree_Util::delimiterToCamelCaseArray(array($root => self::_nodeToValue($document->childNodes->item(0)))); }
public function update($subscriptionId, $attributes) { Braintree_Util::verifyKeys(self::_updateSignature(), $attributes); $path = $this->_config->merchantPath() . '/subscriptions/' . $subscriptionId; $response = $this->_http->put($path, array('subscription' => $attributes)); return $this->_verifyGatewayResponse($response); }
public function connectUrl($params = array()) { $query = Braintree_Util::camelCaseToDelimiterArray($params, '_'); $query['client_id'] = $this->_config->getClientId(); $url = $this->_config->baseUrl() . '/oauth/connect?' . http_build_query($query); return $this->signUrl($url); }
public function conditionallyVerifyKeys($params) { if (array_key_exists("customerId", $params)) { Braintree_Util::verifyKeys($this->generateWithCustomerIdSignature(), $params); } else { Braintree_Util::verifyKeys($this->generateWithoutCustomerIdSignature(), $params); } }
public function connectUrl($params = array()) { $query = Braintree_Util::camelCaseToDelimiterArray($params, '_'); $query['client_id'] = $this->_config->getClientId(); $queryString = preg_replace('/\\%5B\\d+\\%5D/', '%5B%5D', http_build_query($query)); $url = $this->_config->baseUrl() . '/oauth/connect?' . $queryString; return $this->signUrl($url); }
/** * processes SimpleXMLIterator objects recursively * * @access protected * @param object $iterator * @return array xml converted to array */ private static function _iteratorToArray($iterator) { $xmlArray = array(); $value = null; // rewind the iterator and check if the position is valid // if not, return the string it contains $iterator->rewind(); if (!$iterator->valid()) { return self::_typecastXmlValue($iterator); } for ($iterator->rewind(); $iterator->valid(); $iterator->next()) { $tmpArray = null; $value = null; // get the attribute type string for use in conditions below $attributeType = $iterator->attributes()->type; // extract the parent element via xpath query $parentElement = $iterator->xpath($iterator->key() . '/..'); if ($parentElement[0] instanceof SimpleXMLIterator) { $parentElement = $parentElement[0]; $parentKey = Braintree_Util::delimiterToCamelCase($parentElement->getName()); } else { $parentElement = null; } if ($parentKey == "customFields") { $key = Braintree_Util::delimiterToUnderscore($iterator->key()); } else { $key = Braintree_Util::delimiterToCamelCase($iterator->key()); } // process children recursively if ($iterator->hasChildren()) { // return the child elements $value = self::_iteratorToArray($iterator->current()); // if the element is an array type, // use numeric keys to allow multiple values if ($attributeType != 'array') { $tmpArray[$key] = $value; } } else { // cast values according to attributes $tmpArray[$key] = self::_typecastXmlValue($iterator->current()); } // set the output string $output = isset($value) ? $value : $tmpArray[$key]; // determine if there are multiple tags of this name at the same level if (isset($parentElement) && $parentElement->attributes()->type == 'collection' && $iterator->hasChildren()) { $xmlArray[$key][] = $output; continue; } // if the element was an array type, output to a numbered key // otherwise, use the element name if ($attributeType == 'array') { $xmlArray[] = $output; } else { $xmlArray[$key] = $output; } } return $xmlArray; }
public function __toString() { $display = array('amount', 'reason', 'status', 'replyByDate', 'receivedDate', 'currencyIsoCode'); $displayAttributes = array(); foreach ($display as $attrib) { $displayAttributes[$attrib] = $this->{$attrib}; } return __CLASS__ . '[' . Braintree_Util::attributesToString($displayAttributes) . ']'; }
public function __toString() { $display = array('id', 'merchantAccountDetails', 'exceptionMessage', 'amount', 'disbursementDate', 'followUpAction', 'retry', 'success', 'transactionIds'); $displayAttributes = array(); foreach ($display as $attrib) { $displayAttributes[$attrib] = $this->{$attrib}; } return __CLASS__ . '[' . Braintree_Util::attributesToString($displayAttributes) . ']'; }
/** * * @param string $className * @param object $resultObj * @return object returns the passed object if successful * @throws Braintree_Exception_ValidationsFailed */ public static function returnObjectOrThrowException($className, $resultObj) { $resultObjName = Braintree_Util::cleanClassName($className); if ($resultObj->success) { return $resultObj->{$resultObjName}; } else { throw new Braintree_Exception_ValidationsFailed(); } }
public static function fetch($query, $ids) { $criteria = array(); foreach ($query as $term) { $criteria[$term->name] = $term->toparam(); } $criteria["ids"] = Braintree_CreditCardVerificationSearch::ids()->in($ids)->toparam(); $response = Braintree_Http::post('/verifications/advanced_search', array('search' => $criteria)); return Braintree_Util::extractattributeasarray($response['creditCardVerifications'], 'verification'); }
public static function put($path, $params = null) { $response = self::_doRequest('PUT', $path, self::_buildXml($params)); $responseCode = $response['status']; if ($responseCode === 200 || $responseCode === 201 || $responseCode === 422) { return Braintree_Xml::buildArrayFromXml($response['body']); } else { Braintree_Util::throwStatusCodeException($responseCode); } }
public static function all() { $response = Braintree_Http::get('/plans'); if (key_exists('plans', $response)) { $plans = array("plan" => $response['plans']); } else { $plans = array("plan" => array()); } return Braintree_Util::extractAttributeAsArray($plans, 'plan'); }
private function _mapPropertyNamesToObjsToReturn($propertyNames, $objsToReturn) { if (count($objsToReturn) != count($propertyNames)) { $propertyNames = array(); foreach ($objsToReturn as $obj) { array_push($propertyNames, Braintree_Util::cleanClassName(get_class($obj))); } } return array_combine($propertyNames, $objsToReturn); }
public function fetch($query, $ids) { $criteria = array(); foreach ($query as $term) { $criteria[$term->name] = $term->toparam(); } $criteria["ids"] = Braintree_CreditCardVerificationSearch::ids()->in($ids)->toparam(); $path = $this->_config->merchantPath() . '/verifications/advanced_search'; $response = $this->_http->post($path, array('search' => $criteria)); return Braintree_Util::extractattributeasarray($response['creditCardVerifications'], 'verification'); }
private static function _underscoreCustomField($groupByCustomField, $records) { $updatedRecords = array(); foreach ($records as $record) { $camelized = Braintree_Util::delimiterToCamelCase($groupByCustomField); $record[$groupByCustomField] = $record[$camelized]; unset($record[$camelized]); $updatedRecords[] = $record; } return $updatedRecords; }
/** * @ignore * @param string $classToReturn name of class to instantiate */ public function __construct($objToReturn = null) { if (!empty($objToReturn)) { // get a lowercase direct name for the property $property = Braintree_Util::cleanClassName(get_class($objToReturn)); // save the name for indirect access $this->_returnObjectName = $property; // create the property! $this->{$property} = $objToReturn; } }
/** * returns a string representation of the customer * @return string */ public function __toString() { $excludedAttributes = array('statusHistory'); $displayAttributes = array(); foreach ($this->_attributes as $key => $val) { if (!in_array($key, $excludedAttributes)) { $displayAttributes[$key] = $val; } } return __CLASS__ . '[' . Braintree_Util::attributesToString($displayAttributes) . ']'; }
public function put($path, $params = null) { $body = http_build_query(Braintree_Util::camelCaseToDelimiterArray($params, '_')); $response = $this->_doRequest('PUT', $path, $body); $responseCode = $response['status']; if ($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode === 400) { return Braintree_Util::delimiterToCamelCaseArray(json_decode($response['body'], true), '_'); } else { Braintree_Util::throwStatusCodeException($responseCode); } }
public function all() { $path = $this->_config->merchantPath() . '/plans'; $response = $this->_http->get($path); if (key_exists('plans', $response)) { $plans = array("plan" => $response['plans']); } else { $plans = array("plan" => array()); } return Braintree_Util::extractAttributeAsArray($plans, 'plan'); }
/** * sets up the SimpleXMLIterator and starts the parsing * @access public * @param string $xml * @return array array mapped to the passed xml */ public static function arrayFromXml($xml) { // SimpleXML provides the root information on construct $iterator = new SimpleXMLIterator($xml); $xmlRoot = $iterator->getName(); $type = $iterator->attributes()->type; self::$_xmlRoot = $iterator->getName(); self::$_responseType = $type; // return the mapped array with the root element as the header $array = array($xmlRoot => self::_iteratorToArray($iterator)); return Braintree_Util::delimiterToCamelCaseArray($array); }
/** * return errors for the passed html field. * For example, $result->errors->onHtmlField("transaction[customer][last_name]") * * @param string $field * @return array */ public function onHtmlField($field) { $pieces = preg_split("/[\\[\\]]+/", $field, 0, PREG_SPLIT_NO_EMPTY); $errors = $this; foreach (array_slice($pieces, 0, -1) as $key) { $errors = $errors->forKey(Braintree_Util::delimiterToCamelCase($key)); if (!isset($errors)) { return array(); } } $finalKey = Braintree_Util::delimiterToCamelCase(end($pieces)); return $errors->onAttribute($finalKey); }
/** * @ignore * @param string $classToReturn name of class to instantiate */ public function __construct($objToReturn = null, $propertyName = null) { $this->_attributes = array(); if (!empty($objToReturn)) { if (empty($propertyName)) { $propertyName = Braintree_Util::cleanClassName(get_class($objToReturn)); } // save the name for indirect access $this->_returnObjectName = $propertyName; // create the property! $this->{$propertyName} = $objToReturn; } }
/** * arrays passed to this method should have a single root element * with an array as its value * @param array $aData the array of data * @return var XML string */ public static function arrayToXml($aData) { $aData = Braintree_Util::camelCaseToDelimiterArray($aData, '-'); // set up the XMLWriter $writer = new XMLWriter(); $writer->openMemory(); $writer->setIndent(true); $writer->setIndentString(' '); $writer->startDocument('1.0', 'UTF-8'); // get the root element name $aKeys = array_keys($aData); $rootElementName = $aKeys[0]; // open the root element $writer->startElement($rootElementName); // create the body self::_createElementsFromArray($writer, $aData[$rootElementName], $rootElementName); // close the root element and document $writer->endElement(); $writer->endDocument(); // send the output as string return $writer->outputMemory(); }
function testVerifyKeys() { $signature = array('amount', 'customerId', 'orderId', 'paymentMethodToken', 'type', array('creditCard' => array('token', 'cvv', 'expirationDate', 'number')), array('customer' => array('id', 'company', 'email', 'fax', 'firstName', 'lastName', 'phone', 'website')), array('billing' => array('firstName', 'lastName', 'company', 'countryName', 'extendedAddress', 'locality', 'postalCode', 'region', 'streetAddress')), array('shipping' => array('firstName', 'lastName', 'company', 'countryName', 'extendedAddress', 'locality', 'postalCode', 'region', 'streetAddress')), array('options' => array('storeInVault', 'submitForSettlement', 'addBillingAddressToPaymentMethod')), array('customFields' => array('_anyKey_'))); // test valid $userKeys = array('amount' => '100.00', 'customFields' => array('HEY' => 'HO', 'WAY' => 'NO'), 'creditCard' => array('number' => '5105105105105100', 'expirationDate' => '05/12')); $n = Braintree_Util::verifyKeys($signature, $userKeys); $this->assertNull($n); $userKeys = array('amount' => '100.00', 'customFields' => array('HEY' => 'HO', 'WAY' => 'NO'), 'bogus' => 'FAKE', 'totallyFake' => 'boom', 'creditCard' => array('number' => '5105105105105100', 'expirationDate' => '05/12')); // test invalid $this->setExpectedException('InvalidArgumentException'); Braintree_Util::verifyKeys($signature, $userKeys); }
public function update($token, $attribs) { Braintree_Util::verifyKeys(self::updateSignature(), $attribs); return $this->_doUpdate('/payment_methods/any/' . $token, array('payment_method' => $attribs)); }
/** * create a printable representation of the object as: * ClassName[property=value, property=value] * @return string */ public function __toString() { return __CLASS__ . '[' . Braintree_Util::attributesToString($this->_attributes) . ']'; }