Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * The base Buyer class.
- *
- * Specialized `Buyer Types`, eg. FinalOfferBuyer/PingPostBuyer, may extend
- * this class with additional methods, their own variation of the main public
- * method process(), and overrides of any other method as needed.
- *
- * `Individual Buyers`, eg. CreditDotCom/Swish/LeadFlash, are welcome to extend this
- * class directly. They may also extend a specialized buyer type class
- * like those mentioned above. When *absolutely necessary*, in *special-case scenarios*,
- * `Individual Buyers` can override methods such as process().
- *
- * `Individual Buyers` should *NEVER* be instantiated directly using PHP's 'new' keyword.
- * Instead, they should *ALWAYS* be instantiated using the Buyer's static method factory().
- * *CORRECT EXAMPLE* $Swish = Buyer::factory('Swish','tier_02',$lead);
- * *INCORRECT EXAMPLE* $Swish = new Swish($lead, $tier, $config);
- *
- */
- abstract class Buyer {
- //mandatory
- protected $lead = array(); //array of lead's form data key=>fields
- protected $tier; //assuming this to be a number to use for looking up config info
- protected $buyerTierConfig = array();
- protected $buyerResponse; //['bought'](bool), ['status'](string enum), ['price'](int), ['buyerRedirectURL'](string), ['buyerErrors'](array), ['buyerLeadID'](string)
- //possible
- protected $pathTime;
- protected $timeZone;
- //testing
- protected $testing;
- /**
- * Should only occur when an individual buyer is instantiated via the factory method
- * @param <array> $lead
- * @param <string> $tier
- * @param <array> $buyer_tier_config
- */
- public function __construct($lead, $tier, $buyer_tier_config) {
- $this->lead = $lead;
- $this->tier = $tier;
- $this->buyerTierConfig = $buyer_tier_config;
- }
- /**
- * Encapsulates the main buyer process. Please note the use of $_lead as a
- * temporary, altered variation of the system's lead. The unaltered system
- * lead always remains accessable via $this->lead. In similar fashion, the
- * raw, unformatted $this->buyerResponse is set as $_response while we are
- * in the process of parsing and normalizing it for use by the system.
- */
- public function process() {
- $_lead = $this->lead;
- if ($this->passed_filters($_lead)) {
- $_lead = $this->append_lead_values($_lead);
- $_lead = $this->normalize_lead_data_for_buyer($_lead);
- $_lead = $this->format_lead_data_for_buyer($_lead);
- $this->buyerResponse['rawResponse'] = $this->post_lead_to_buyer($_lead);
- $_response = $this->parse_buyer_response();
- }
- $this->normalize_response_data($_response);
- H_debug::pre($this->buyerResponse,'BUYER RESPONSE');
- //echo __CLASS__;
- //var_dump($this instanceof Affnet);
- }
- /**
- * Check the lead against the buyer's specific filters.
- * @param <array> $_lead
- * @return <bool> true if the lead passed all filters
- */
- abstract protected function passed_filters($_lead);
- /**
- * Append additional required values. Reserved for hard-setting values.
- * @param <array> $_lead
- * @return <array> appropriately altered lead
- */
- abstract protected function append_lead_values($_lead);
- /**
- * Using the key mapping $this->buyerTierConfig['settings']['mapping'],
- * create a version of the lead array as required by the buyer.
- * May also do CAPS adjustments, date formatting, etc at this point.
- * @param <array> $_lead
- * @return <array> appropriately altered lead
- */
- abstract protected function normalize_lead_data_for_buyer($_lead);
- /**
- * Convert the lead array into the format required for posting the lead
- * e.g. to a query string, xml, json, etc. Can usually be accomplished using
- * the H_format helper library.
- * @param <array> $_lead
- * @return <array> appropriately altered lead
- */
- abstract protected function format_lead_data_for_buyer($_lead);
- /**
- * Post the formatted lead to the buyer generally via a cURL. Nothing is returned.
- * @param <string> $_lead
- * @return <string> the unaltered buyer response
- */
- abstract protected function post_lead_to_buyer($_lead);
- /**
- * Uses the $this->buyerResponse['rawResponse'] string to create an array.
- * @return <array> the raw response in array format
- */
- abstract protected function parse_buyer_response();
- /**
- * Sets the standarized array of information that the rest of the system
- * expects. Maps the parsed buyer response array to our standarized keys or,
- * in the case of an error or unexepected outcome, an array of error instructions.
- * ['bought'](bool), ['status'](string enum), ['price'](int),
- * ['buyerRedirectURL'](string), ['buyerErrors'](array),
- * ['buyerLeadID'](string), ['rawResponse'](string)
- * @param <array> $data
- * @return <array> the response used by our system
- */
- abstract protected function normalize_response_data($data);
- /**
- * Returns buyerResponse
- * @return <array>
- */
- public function get_response() {
- return $this->buyerResponse;
- }
- /**
- * Instantiates the correct individual buyer class based on the buyer config
- * @param <string> $buyer_name
- * @param <string> $tier_name
- * @param <array> $lead
- * @return new BuyerClass
- */
- public static function factory($buyer_name, $tier_name, $lead) {
- $class_name = $buyer_name;
- $buyer_tier_config = self::get_config_array($buyer_name, $tier_name);
- if (isset($buyer_tier_config['settings']['class_extension']) && $buyer_tier_config['settings']['class_extension'] != '') {
- $class_name = $buyer_name.'_'.$buyer_tier_config['settings']['class_extension'];
- } else if (isset($buyer_tier_config['settings']['ping']) && $buyer_tier_config['settings']['ping'] == 'True') {
- $class_name = $buyer_name.'_PingPost';
- } else if (isset($buyer_tier_config['settings']['final_offer']) && $buyer_tier_config['settings']['final_offer']) {
- $class_name = $buyer_name.'_FinalOffer';
- }
- return new $class_name($lead, $tier_name, $buyer_tier_config);
- }
- /**
- * Returns the buyer tier config
- * @param <string> $buyer_name
- * @param <tier> $tier_name
- * @return <array> buyer tier config
- */
- protected function get_config_array($buyer_name,$tier_name) {
- $buyer_name = strtolower($buyer_name);
- $config = array();
- $product_name = strtolower('Payday');
- include_once dirname(__FILE__) . '/buyers/buyer_configs/' . $product_name . '_configs/' . $buyer_name . '.' . $product_name . '.config.php';
- //*CREDENTIALS* fetch & set *********************
- //if credentials are in the tier, use them. else grab from buyer
- foreach (${$buyer_name}['credentials'] as $key=>$val) {
- if (isset(${$tier_name}['credentials'][$key])) {
- $config['credentials'][$key] = ${$tier_name}['credentials'][$key];
- } else {
- $config['credentials'][$key] = $val;
- }
- }
- //*SETTINGS* fetch & set *********************
- //grab settings from buyer and possibly tier, combine all. in the case of
- //a tier having a duplicate setting as a buyer, tier overrides.
- if (isset(${$buyer_name}['settings']) && is_array(${$buyer_name}['settings'])) {
- foreach(${$buyer_name}['settings'] as $key=>$val) {
- $config['settings'][$key] = $val;
- }
- }
- //make *sure* we loop through tier *AFTER* buyer so it overrides duplicate keys
- if (isset(${$tier_name}['settings']) && is_array(${$tier_name}['settings'])) {
- foreach(${$tier_name}['settings'] as $key=>$val) {
- $config['settings'][$key] = $val;
- }
- }
- //*MAPPING*
- $config['settings']['mapping'] = ${$config['settings']['mapping']};
- //*PING DETAILS* fetch & set *********************
- if (isset(${$tier_name}) && is_array(${$tier_name})) {
- if (isset(${$tier_name}['settings']['ping']) && ${$tier_name}['settings']['ping'] == 'True') {
- $config['ping_details']['ping_fields'] = array();
- $config['ping_details']['ping_credentials'] = array();
- if (isset(${$tier_name}['ping_details']['ping_fields']) && is_array(${$tier_name}['ping_details']['ping_fields'])) {
- $config['ping_details']['ping_fields'] = ${$tier_name}['ping_details']['ping_fields'];
- }
- if (isset(${$tier_name}['ping_details']['ping_credentials']) && is_array(${$tier_name}['ping_details']['ping_credentials'])) {
- $config['ping_details']['ping_credentials'] = ${$tier_name}['ping_details']['ping_credentials'];
- }
- }
- }
- return $config;
- }
- public function __isset($name) {
- return isset($this->$name);
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement