divido.php (2667B)
1 <?php 2 class ModelExtensionPaymentDivido extends Model { 3 const CACHE_KEY_PLANS = 'divido_plans'; 4 5 public function getAllPlans() { 6 if ($plans = $this->cache->get(self::CACHE_KEY_PLANS)) { 7 // OpenCart 2.1 decodes json objects to associative arrays so we 8 // need to make sure we're getting a list of simple objects back. 9 $plans = array_map(function ($plan) { 10 return (object)$plan; 11 }, $plans); 12 13 return $plans; 14 } 15 16 $api_key = $this->config->get('payment_divido_api_key'); 17 if (!$api_key) { 18 throw new Exception("No Divido api-key defined"); 19 } 20 21 Divido::setMerchant($api_key); 22 23 $response = Divido_Finances::all(); 24 if ($response->status != 'ok') { 25 throw new Exception("Can't get list of finance plans from Divido!"); 26 } 27 28 $plans = $response->finances; 29 30 // OpenCart 2.1 switched to json for their file storage cache, so 31 // we need to convert to a simple object. 32 $plans_plain = array(); 33 foreach ($plans as $plan) { 34 $plan_copy = new stdClass(); 35 $plan_copy->id = $plan->id; 36 $plan_copy->text = $plan->text; 37 $plan_copy->country = $plan->country; 38 $plan_copy->min_amount = $plan->min_amount; 39 $plan_copy->min_deposit = $plan->min_deposit; 40 $plan_copy->max_deposit = $plan->max_deposit; 41 $plan_copy->interest_rate = $plan->interest_rate; 42 $plan_copy->deferral_period = $plan->deferral_period; 43 $plan_copy->agreement_duration = $plan->agreement_duration; 44 45 $plans_plain[] = $plan_copy; 46 } 47 48 $this->cache->set(self::CACHE_KEY_PLANS, $plans_plain); 49 50 return $plans_plain; 51 } 52 53 public function getLookupByOrderId($order_id) { 54 return $this->db->query("SELECT * FROM `" . DB_PREFIX . "divido_lookup` WHERE `order_id` = " . (int)$order_id); 55 } 56 57 public function install() { 58 $this->db->query(" 59 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "divido_product` ( 60 `product_id` INT(11) NOT NULL, 61 `display` CHAR(7) NOT NULL, 62 `plans` text, 63 PRIMARY KEY (`product_id`) 64 ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;"); 65 66 $this->db->query(" 67 CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "divido_lookup` ( 68 `order_id` INT(11) NOT NULL, 69 `salt` CHAR(64) NOT NULL, 70 `proposal_id` CHAR(40), 71 `application_id` CHAR(40), 72 `deposit_amount` NUMERIC(6,2), 73 PRIMARY KEY (`order_id`) 74 ) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;"); 75 } 76 77 public function uninstall() { 78 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "divido_product`;"); 79 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "divido_lookup`;"); 80 } 81 }