Created
November 25, 2015 01:32
-
-
Save 82rules/a7852a25305eacb004e6 to your computer and use it in GitHub Desktop.
old code for pagar.me example in L4, to be used as basic example not working (or good) code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composer require "pagarme/pagarme-php:dev-master" | |
<?PHP | |
use PagarMe; | |
use PagarMe_Charge; | |
use PagarMe_Exception; | |
use PagarMe_Subscription; | |
use PagarMe_Transaction; | |
class PagarMePayment implements PaymentInterface { | |
public static $key; | |
public $customer; | |
public $plan; | |
public $charge; | |
public function __construct(){ | |
if (empty(self::$key)){ | |
self::$key = getenv('comocode-keys.pagarme.secret'); | |
PagarMe::setApiKey(self::$key); | |
} | |
} | |
public function customer_id(){ | |
return $this->customer->id; | |
} | |
public function plan_id($type){ | |
return !empty(getenv("comocode-keys.pagarme.{$type}")) ? getenv("comocode-keys.pagarme.{$type}") : false; | |
} | |
public function charge_id(){ | |
return $this->charge->id; | |
} | |
public function isActive(){ | |
return ($this->plan->getStatus() !== 'canceled'); | |
} | |
public function create_a_customer($token, User $user){ | |
$this->customer = new PagarMe_Transaction([ | |
"card_hash" => $token, | |
'customer'=>[ | |
'name' => $user->name, | |
'document_number' => $user->id, | |
'email' => $user->email | |
] | |
] | |
); | |
dd(['customer',$this->customer->getCustomer()]); | |
} | |
public function subscribe_a_customer(Subscription $subscription, User $user, $token){ | |
$plan_id = $this->plan_id($subscription->name); | |
if (empty($plan_id)) { | |
return false; | |
} | |
$this->plan = new \PagarMe_Subscription([ | |
"card_hash" => $token, | |
"plan_id"=>$plan_id, | |
'customer'=>[ | |
'name' => $user->name, | |
'email' => $user->email | |
] | |
]); | |
try { | |
$this->plan->create(); | |
} | |
catch(PagarMe_Exception $e){ | |
throw new GeneralException($e->getMessage()); | |
} | |
$this->customer=$this->plan->getCustomer(); | |
$this->charge=$this->plan; | |
} | |
public function charge_a_customer(Subscription $subscription, Customer $customer){ | |
} | |
public function cancel_a_subscription(Customer $customer, Charge $charge){ | |
try { | |
$this->plan = PagarMe_Subscription::findById($charge->charge_id); | |
$this->plan->cancel(); // Cancel | |
} | |
catch(PagarMe_Exception $e){ | |
throw new GeneralException($e->getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment