Skip to content

Commit

Permalink
v1.0-beta.3
Browse files Browse the repository at this point in the history
  • Loading branch information
husseinalhammad committed Oct 29, 2019
0 parents commit caa975c
Show file tree
Hide file tree
Showing 175 changed files with 16,238 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pipit_stripe/_default_index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

# include the API and classes
include(__DIR__.'/../../../core/inc/api.php');

foreach($classes as $class) {
include($class);
}

$API = new PerchAPI(1.0, 'pipit_stripe');
$Lang = $API->get('Lang');
$HTML = $API->get('HTML');
$Paging = $API->get('Paging');
$Template = $API->get('Template');


# Do anything you want to do before output is started
$Perch->page_title = $Lang->get($title);
include('modes/_subnav.php');
include('modes/'.$mode.'.pre.php');

# Top layout
include(PERCH_CORE . '/inc/top.php');

# Display your page
include('modes/'.$mode.'.post.php');

# Bottom layout
include(PERCH_CORE . '/inc/btm.php');
2 changes: 2 additions & 0 deletions pipit_stripe/_version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
if (!defined('PIPIT_STRIPE_VERSION')) define('PIPIT_STRIPE_VERSION', '1.0-beta.3');
17 changes: 17 additions & 0 deletions pipit_stripe/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
include(__DIR__.'/_version.php');
$this->register_app('pipit_stripe', 'Stripe', 99, 'Stripe app', PIPIT_STRIPE_VERSION);
$this->require_version('pipit_stripe', '3.0');

include(__DIR__.'/lib/vendor/autoload.php');

spl_autoload_register(function($class_name){
if (strpos($class_name, 'PipitStripe_')===0) {
include(PERCH_PATH.'/addons/apps/pipit_stripe/lib/'.$class_name.'.class.php');
return true;
}
return false;
});



6 changes: 6 additions & 0 deletions pipit_stripe/import/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$mode = 'import';
$title = 'Stripe: Import Products and Plans';
$classes = [];

include('../_default_index.php');
6 changes: 6 additions & 0 deletions pipit_stripe/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
$mode = 'dashboard';
$title = 'Dashboard';
$classes = array();

include('_default_index.php');
180 changes: 180 additions & 0 deletions pipit_stripe/lib/PipitStripe_Plans.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
<?php

class PipitStripe_Plans {
public $file_path;
public $dir;

function __construct() {
$this->dir = PERCH_PATH . '/pipit_stripe';
$this->file_path = PerchUtil::file_path($this->dir . '/plans.json');
}




/**
* Get all plans from cached file if exists, otherwise directly from Stripe
*
* @return array|boolean
*/
public function get(){
if(file_exists($this->file_path)) {
PerchUtil::debug('Getting plans from cached file');

$json = file_get_contents($this->file_path);
$plans = json_decode($json, 1);
if($plans === NULL) {
PerchUtil::debug('Could not read cache file', 'notice');
}
}

if(!file_exists($this->file_path) || !$plans) {
$plans = $this->get_from_stripe();
}


if($plans) {
array_walk($plans, function(&$item){
if(isset($item['created'])) {
$item['created'] = date('Y-m-d H:i:s', $item['created']);
}

if(isset($item['updated'])) {
$item['updated'] = date('Y-m-d H:i:s', $item['updated']);
}

if(isset($item['metadata'])) {
// create meta_*
foreach($item['metadata'] as $key => $val) {
$item['meta_' . PerchUtil::urlify($key, '_')] = $val;
}

// Perch repeater friendly array
array_walk($item['metadata'], function(&$val, $key) {
$val = [
'label' => $key,
'value' => $val
];
});

$item['metadata'] = array_values($item['metadata']);
}

if(isset($item['amount'])) {
$item['amount_formatted'] = $item['amount'] / 100;
}

});
}



if($plans) return $plans;
return false;
}




/**
* Get all plans from Stripe API
*
* @return array|boolean
*/
public function get_from_stripe($count = 100, $return_cache_result=false) {
if(!defined('PIPIT_STRIPE_SECRET_KEY')) {
PerchUtil::debug('Stripe secret key not set', 'error');
return false;
}

PerchUtil::debug('Getting plans from Stripe API');
\Stripe\Stripe::setApiKey(PIPIT_STRIPE_SECRET_KEY);
$Plans = \Stripe\Plan::all(['limit' => $count]);


if(isset($Plans->data)) {
//update cache
$cache_result = $this->update_cache($Plans->data);
if($return_cache_result) return $cache_result;

return $Plans->data;
}
return false;
}





/**
* Update JSON file in perch/pipit_stripe
* @param array $data
*/
function update_cache($data) {
if(!is_dir($this->dir)) {
if(!mkdir($this->dir)) {
$result['result'] = 'FAILED';
$result['message'] = 'Could not create directory ' . $this->dir;
return $result;
}
}


// save to JSON file
$data_json = json_encode($data);
if($data_json === NULL) {
$result['result'] = 'FAILED';
$result['message'] = 'Could not JSON encode data';
return $result;
}


if(!file_put_contents($this->file_path, $data_json)) {
$result['result'] = 'FAILED';
$result['message'] = 'Could not write file ' . $this->file_path;
return $result;
}


$result['result'] = 'OK';
$result['message'] = 'Plans saved.';
return $result;
}




/**
* Get a single plan
*
* @param string $planID
* @return array|boolean
*/
public function get_plan($planID) {
$plans = $this->get();

$plans = array_filter($plans, function($item) use($planID) {
if($item['id'] == $planID) return $item;
});

return $plans;
}



/**
* Get plans for a single product
* @param string $productID
* @return array|boolean
*/
public function get_plans_for($productID) {
$plans = $this->get();

$plans = array_filter($plans, function($item) use($productID) {
if($item['product'] == $productID) return $item;
});

return $plans;
}

}
Loading

0 comments on commit caa975c

Please sign in to comment.