Skip to content

Instantly share code, notes, and snippets.

@JoaoVagner
Created July 13, 2017 03:17
Show Gist options
  • Save JoaoVagner/b93d8e1265bd1a17016f511b85906625 to your computer and use it in GitHub Desktop.
Save JoaoVagner/b93d8e1265bd1a17016f511b85906625 to your computer and use it in GitHub Desktop.
<?php
public function iuguCreateSubscription($subscriptionData, $debug = false)
{
\Fuel\Core\Config::load('iugu');
$url = \Fuel\Core\Config::get('iugu.url');
$idIugu = \Model\Treasury_Marketplace_Store::getTokenStore(new \MongoId($subscriptionData['_store']), true);
$modelPlan = new Treasury_Marketplace_Store_Plan();
$modelBrother = new Hospitality_Brother();
$getPlan = $modelPlan->get(new \MongoId($subscriptionData['_plan']));
$getBrother = $modelBrother->get(new \MongoId($subscriptionData['_brother']));
$newSubscription = [
'plan_identifier' => $getPlan['iugu']['identifier'],
'customer_id' => $getBrother['iugu']['id']
];
$postData = http_build_query($newSubscription);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . "/subscriptions");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, $idIugu . ":" . "");
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if ($debug) {
$info = curl_getinfo($ch);
var_dump($info, $result);
die;
}
$result = curl_exec($ch);
if (curl_errno($ch)) {
\Fuel\Core\Log::error(curl_error($ch));
return false;
}
if ($result) {
\Model\Log::register('payment gateway', [
'type' => 'backoffice',
'gateway' => 'iugu',
'action' => 'create subscription',
'uri' => \Fuel\Core\Uri::current(),
'result' => json_decode($result, true),
'success' => 'subscription create success in gateway']);
}
curl_close($ch);
return [
'iugu-api' => json_decode($result, true)
];
}
<?php
public function post_add()
{
$SubscriptionModel = new Treasury_Subscription();
$postData = \Fuel\Core\Input::post();
$where = [
'_store' => $postData['_store'],
'_brother' => $postData['_brother'],
'_plan' => $postData['_plan'],
'expires_at' => $postData['expires_at']
];
$findDuplicate = $SubscriptionModel->findOne($where);
$checkDuplicate = !empty($findDuplicate) ? true : false;
if ($checkDuplicate) {
return $this->response_json(['error' => 'A Ação que você está tentando executar irá duplicar uma assinatura']);
}
$iugu = $SubscriptionModel->iuguCreateSubscription($postData);
// var_dump($iugu);
// die;
if (isset($iugu['iugu-api']['errors'])) {
return $this->response_json(['error' => $iugu['iugu-api']['errors']]);
}
$postData['iugu'] = $iugu['iugu-api'];
if (!empty($postData)) {
$add = $SubscriptionModel->add($postData);
}
return $this->response_json($add);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment