Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.93 KB | None | 0 0
  1. <?php
  2. /*
  3.  * The base Buyer class.
  4.  *
  5.  * Specialized `Buyer Types`, eg. FinalOfferBuyer/PingPostBuyer, may extend
  6.  * this class with additional methods, their own variation of the main public
  7.  * method process(), and overrides of any other method as needed.
  8.  *
  9.  * `Individual Buyers`, eg. CreditDotCom/Swish/LeadFlash, are welcome to extend this
  10.  * class directly.  They may also extend a specialized buyer type class
  11.  * like those mentioned above.  When *absolutely necessary*, in *special-case scenarios*,
  12.  * `Individual Buyers` can override methods such as process().
  13.  *
  14.  * `Individual Buyers` should *NEVER* be instantiated directly using PHP's 'new' keyword.
  15.  * Instead, they should *ALWAYS* be instantiated using the Buyer's static method factory().
  16.  * *CORRECT EXAMPLE* $Swish = Buyer::factory('Swish','tier_02',$lead);
  17.  * *INCORRECT EXAMPLE* $Swish = new Swish($lead, $tier, $config);
  18.  *
  19.  */
  20.  
  21. abstract class Buyer {
  22.     //mandatory
  23.     protected $lead = array(); //array of lead's form data key=>fields
  24.     protected $tier; //assuming this to be a number to use for looking up config info
  25.     protected $buyerTierConfig = array();
  26.     protected $buyerResponse; //['bought'](bool), ['status'](string enum), ['price'](int), ['buyerRedirectURL'](string), ['buyerErrors'](array), ['buyerLeadID'](string)
  27.  
  28.     //possible
  29.     protected $pathTime;
  30.     protected $timeZone;
  31.  
  32.     //testing
  33.     protected $testing;
  34.  
  35.     /**
  36.      * Should only occur when an individual buyer is instantiated via the factory method
  37.      * @param <array> $lead
  38.      * @param <string> $tier
  39.      * @param <array> $buyer_tier_config
  40.      */
  41.     public function __construct($lead, $tier, $buyer_tier_config) {
  42.         $this->lead = $lead;
  43.         $this->tier = $tier;
  44.         $this->buyerTierConfig = $buyer_tier_config;
  45.     }
  46.  
  47.  
  48.     /**
  49.      * Encapsulates the main buyer process.  Please note the use of $_lead as a
  50.      * temporary, altered variation of the system's lead.  The unaltered system
  51.      * lead always remains accessable via $this->lead.  In similar fashion, the
  52.      * raw, unformatted $this->buyerResponse is set as $_response while we are
  53.      * in the process of parsing and normalizing it for use by the system.
  54.      */
  55.     public function process() {
  56.         $_lead = $this->lead;
  57.         if ($this->passed_filters($_lead)) {
  58.             $_lead = $this->append_lead_values($_lead);
  59.             $_lead = $this->normalize_lead_data_for_buyer($_lead);
  60.             $_lead = $this->format_lead_data_for_buyer($_lead);
  61.             $this->buyerResponse['rawResponse'] = $this->post_lead_to_buyer($_lead);
  62.             $_response = $this->parse_buyer_response();
  63.         }
  64.  
  65.         $this->normalize_response_data($_response);
  66.         H_debug::pre($this->buyerResponse,'BUYER RESPONSE');
  67.         //echo __CLASS__;
  68.         //var_dump($this instanceof Affnet);
  69.     }
  70.  
  71.  
  72.     /**
  73.      * Check the lead against the buyer's specific filters.
  74.      * @param <array> $_lead
  75.      * @return <bool> true if the lead passed all filters
  76.      */
  77.     abstract protected function passed_filters($_lead);
  78.  
  79.     /**
  80.      * Append additional required values.  Reserved for hard-setting values.
  81.      * @param <array> $_lead
  82.      * @return <array> appropriately altered lead
  83.      */
  84.     abstract protected function append_lead_values($_lead);
  85.  
  86.     /**
  87.      * Using the key mapping $this->buyerTierConfig['settings']['mapping'],
  88.      * create a version of the lead array as required by the buyer.
  89.      * May also do CAPS adjustments, date formatting, etc at this point.
  90.      * @param <array> $_lead
  91.      * @return <array> appropriately altered lead
  92.      */
  93.     abstract protected function normalize_lead_data_for_buyer($_lead);
  94.  
  95.     /**
  96.      * Convert the lead array into the format required for posting the lead
  97.      * e.g. to a query string, xml, json, etc.  Can usually be accomplished using
  98.      * the H_format helper library.
  99.      * @param <array> $_lead
  100.      * @return <array> appropriately altered lead
  101.      */
  102.     abstract protected function format_lead_data_for_buyer($_lead);
  103.  
  104.     /**
  105.      * Post the formatted lead to the buyer generally via a cURL.  Nothing is returned.
  106.      * @param <string> $_lead
  107.      * @return <string> the unaltered buyer response
  108.      */
  109.     abstract protected function post_lead_to_buyer($_lead);
  110.  
  111.     /**
  112.      * Uses the $this->buyerResponse['rawResponse'] string to create an array.
  113.      * @return <array> the raw response in array format
  114.      */
  115.     abstract protected function parse_buyer_response();
  116.  
  117.     /**
  118.      * Sets the standarized array of information that the rest of the system
  119.      * expects.  Maps the parsed buyer response array to our standarized keys or,
  120.      * in the case of an error or unexepected outcome, an array of error instructions.
  121.      * ['bought'](bool), ['status'](string enum), ['price'](int),
  122.      * ['buyerRedirectURL'](string), ['buyerErrors'](array),
  123.      * ['buyerLeadID'](string), ['rawResponse'](string)
  124.      * @param <array> $data
  125.      * @return <array> the response used by our system
  126.      */
  127.     abstract protected function normalize_response_data($data);
  128.  
  129.     /**
  130.      * Returns buyerResponse
  131.      * @return <array>
  132.      */
  133.     public function get_response() {
  134.         return $this->buyerResponse;
  135.     }
  136.  
  137.     /**
  138.      * Instantiates the correct individual buyer class based on the buyer config
  139.      * @param <string> $buyer_name
  140.      * @param <string> $tier_name
  141.      * @param <array> $lead
  142.      * @return new BuyerClass
  143.      */
  144.     public static function factory($buyer_name, $tier_name, $lead) {
  145.         $class_name = $buyer_name;
  146.  
  147.         $buyer_tier_config = self::get_config_array($buyer_name, $tier_name);
  148.  
  149.         if (isset($buyer_tier_config['settings']['class_extension']) && $buyer_tier_config['settings']['class_extension'] != '') {
  150.             $class_name = $buyer_name.'_'.$buyer_tier_config['settings']['class_extension'];
  151.         } else if (isset($buyer_tier_config['settings']['ping']) && $buyer_tier_config['settings']['ping'] == 'True') {
  152.             $class_name = $buyer_name.'_PingPost';
  153.         } else if (isset($buyer_tier_config['settings']['final_offer']) && $buyer_tier_config['settings']['final_offer']) {
  154.             $class_name = $buyer_name.'_FinalOffer';
  155.         }
  156.  
  157.         return new $class_name($lead, $tier_name, $buyer_tier_config);
  158.     }
  159.  
  160.  
  161.     /**
  162.      * Returns the buyer tier config
  163.      * @param <string> $buyer_name
  164.      * @param <tier> $tier_name
  165.      * @return <array> buyer tier config
  166.      */
  167.     protected function get_config_array($buyer_name,$tier_name) {
  168.         $buyer_name = strtolower($buyer_name);
  169.         $config = array();
  170.  
  171.         $product_name = strtolower('Payday');
  172.         include_once dirname(__FILE__) . '/buyers/buyer_configs/' . $product_name . '_configs/' . $buyer_name . '.' . $product_name . '.config.php';
  173.  
  174.         //*CREDENTIALS* fetch & set *********************
  175.         //if credentials are in the tier, use them.  else grab from buyer
  176.         foreach (${$buyer_name}['credentials'] as $key=>$val) {
  177.             if (isset(${$tier_name}['credentials'][$key])) {
  178.                 $config['credentials'][$key] = ${$tier_name}['credentials'][$key];
  179.             } else {
  180.                 $config['credentials'][$key] = $val;
  181.             }
  182.         }
  183.  
  184.         //*SETTINGS* fetch & set *********************
  185.         //grab settings from buyer and possibly tier, combine all.  in the case of
  186.         //a tier having a duplicate setting as a buyer, tier overrides.
  187.         if (isset(${$buyer_name}['settings']) && is_array(${$buyer_name}['settings'])) {
  188.             foreach(${$buyer_name}['settings'] as $key=>$val) {
  189.                 $config['settings'][$key] = $val;
  190.             }
  191.         }
  192.         //make *sure* we loop through tier *AFTER* buyer so it overrides duplicate keys
  193.         if (isset(${$tier_name}['settings']) && is_array(${$tier_name}['settings'])) {
  194.             foreach(${$tier_name}['settings'] as $key=>$val) {
  195.                 $config['settings'][$key] = $val;
  196.             }
  197.         }
  198.  
  199.         //*MAPPING*
  200.         $config['settings']['mapping'] = ${$config['settings']['mapping']};
  201.  
  202.         //*PING DETAILS* fetch & set *********************
  203.         if (isset(${$tier_name}) && is_array(${$tier_name})) {
  204.             if (isset(${$tier_name}['settings']['ping']) && ${$tier_name}['settings']['ping'] == 'True') {
  205.                 $config['ping_details']['ping_fields'] = array();
  206.                 $config['ping_details']['ping_credentials'] = array();
  207.                 if (isset(${$tier_name}['ping_details']['ping_fields']) && is_array(${$tier_name}['ping_details']['ping_fields'])) {
  208.                     $config['ping_details']['ping_fields'] = ${$tier_name}['ping_details']['ping_fields'];
  209.                 }
  210.                 if (isset(${$tier_name}['ping_details']['ping_credentials']) && is_array(${$tier_name}['ping_details']['ping_credentials'])) {
  211.                     $config['ping_details']['ping_credentials'] = ${$tier_name}['ping_details']['ping_credentials'];
  212.                 }
  213.             }
  214.         }
  215.  
  216.         return $config;
  217.     }
  218.  
  219.     public function __isset($name) {
  220.         return isset($this->$name);
  221.     }
  222.    
  223. }
  224. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement