Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement