/** * Populate the protected $_data SimpleXmlElement with the passed in array * Warning: no validation is done so you can end up with some crazy SimpleXmlElement objects * if you aren't careful passing in valid arrays. * * The array keys may be camelCaseWords or dash-type-words. If they are camelCaseWords, they * will be converted to dash-type-words for the SimpleXmlElement. * * @param array $data * @param string $wrapper * @return void */ public function hydrate(array $data, $wrapper) { $wrapper = SpreedlyCommon::camelToDash($wrapper); $xml = '<' . $wrapper . '/>'; $xml = new SimpleXmlElement($xml); foreach ($data as $key => $value) { $key = SpreedlyCommon::camelToDash($key); $xml->addChild($key, $value); } $this->setData($xml); }
public function __set($key, $value) { $key = SpreedlyCommon::camelToDash($key); if (array_key_exists($key, $this->_cardData)) { if ($key == 'number') { $value = preg_replace('/\\D/', '', $value); } elseif ($key == 'month') { if (!is_numeric($value) || $value < 1 || $value > 12) { $value = ''; // Do not allow invalid month numbers to be set } } elseif ($key == 'year') { $thisYear = date('Y'); if (!is_numeric($value) || $value < $thisYear) { $value = ''; // Do not allow invalid or past year numbers to be set } } $this->_cardData[$key] = $value; } }
/** * Return an array of enabled spreedly subscriptions * * @return array */ public static function getSubscriptions() { if (empty(self::$_subscriptionPlans)) { $result = SpreedlyCommon::curlRequest("/subscription_plans.xml", "get"); if ($result->code == '200') { $subscriptions = array(); $plans = new SimpleXmlElement($result->response); foreach ($plans as $plan) { $subscription = new SpreedlySubscription(); $subscription->setData($plan); /// Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Spreedly subscription enabled: " . $subscription->enabled); if ('true' == (string) $subscription->enabled) { $subscriptions[] = $subscription; } } self::$_subscriptionPlans = $subscriptions; } else { throw new SpreedlyException('Spreedly Subscription: Unable to retrieve remote list of subscriptions', 66003); } } return self::$_subscriptionPlans; }
/** * Pay a spreedly invoice. The invoice token is required for payment. * Returns the paid invoice token. * * @param mixed $paymentMethod Either "on-file" or a SpreedlyCreditCard object * @param string $invoiceToken * @return string The invoice token that was paid. * @throws SpreedlyException on failure */ public function pay($paymentMethod, $invoiceToken = null) { $payment = array('account-type' => 'on-file'); if (get_class($paymentMethod) == 'SpreedlyCreditCard') { if (!$paymentMethod->validate()) { $errorDetails = print_r($paymentMethod->getErrors(), true); throw new SpreedlyException('Spreedly Payment: Invalid credit card data trying to be used to pay a spreedly invoice: ' . $errorDetails, 66001); } $cardData = $paymentMethod->getCardData(); $payment = array('account-type' => 'credit-card', 'credit-card' => $cardData); } // Set invoice token if provided if (isset($invoiceToken)) { $this->setToken($invoiceToken); } // Make sure there is an invoice token before trying to process the payment if (empty($this->_invoiceToken)) { throw new SpreedlyException('Spreedly Payment: Trying to pay spreedly invoice without a valid invoice token', 66002); } $xml = Cart66Common::arrayToXml($payment, 'payment'); $result = SpreedlyCommon::curlRequest('/invoices/' . $this->_invoiceToken . '/pay.xml', "put", $xml); $responseCode = $result->code; if (!empty($responseCode)) { if ($responseCode != 200) { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Spreedly Invoice Payment: Failed to pay invoice. \n Code: " . $responseCode . "\nResponse: " . $result->response . "\n Payment XML:\n{$xml}"); $errorResponse = $result->response; throw new SpreedlyException("Spreedly Payment: Failed to pay spreedly invoice. \n\n{$errorResponse}", $responseCode); } try { $invoice = new SimpleXMLElement($result->response); $this->_invoiceToken = $invoice->token; } catch (Exception $e) { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] SpreedlyInvoice pay(): \n Unable to create SimpleXmlElement from result response: " . $result->response); } } }
/** * All fields are optional - any field that is not provided will not be updated. */ public static function updateRemoteAccount($customerId, $subscriberData) { $subscriberXml = Cart66Common::arrayToXml($subscriberData, 'subscriber'); $result = SpreedlyCommon::curlRequest("/subscribers/{$customerId}.xml", "put", $subscriberXml); if ($result->code != '200') { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Spreedly Subscriber: Account information could not be updated. " . $result->response . "\nCode: {$result->code}"); throw new SpreedlyException('Spreedly Subscriber: Account information could not be updated.', $result->code); } }
/** * This function must be called before any others to initialize the Spreedly account api details */ public static function init($siteName, $apiToken) { self::$siteName = $siteName; self::$apiToken = $apiToken; self::$baseUri = "https://spreedly.com/api/v4/{$siteName}"; }
public function loadSpreedlyModels() { $shortName = Cart66Setting::getValue('spreedly_shortname'); $apiToken = Cart66Setting::getValue('spreedly_apitoken'); if (CART66_PRO && $shortName && $apiToken) { require_once CART66_PATH . "/pro/models/SpreedlyCommon.php"; require_once CART66_PATH . "/pro/models/SpreedlyCreditCard.php"; require_once CART66_PATH . "/pro/models/SpreedlyException.php"; require_once CART66_PATH . "/pro/models/SpreedlyInvoice.php"; require_once CART66_PATH . "/pro/models/SpreedlySubscriber.php"; require_once CART66_PATH . "/pro/models/SpreedlySubscription.php"; require_once CART66_PATH . "/pro/models/SpreedlyXmlObject.php"; SpreedlyCommon::init($shortName, $apiToken); } }