Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. composer require "pagarme/pagarme-php:dev-master"
  2.  
  3. <?PHP
  4. use PagarMe;
  5. use PagarMe_Charge;
  6. use PagarMe_Exception;
  7. use PagarMe_Subscription;
  8. use PagarMe_Transaction;
  9. class PagarMePayment implements PaymentInterface {
  10. public static $key;
  11. public $customer;
  12. public $plan;
  13. public $charge;
  14. public function __construct(){
  15. if (empty(self::$key)){
  16. self::$key = getenv('comocode-keys.pagarme.secret');
  17. PagarMe::setApiKey(self::$key);
  18. }
  19. }
  20. public function customer_id(){
  21. return $this->customer->id;
  22. }
  23. public function plan_id($type){
  24. return !empty(getenv("comocode-keys.pagarme.{$type}")) ? getenv("comocode-keys.pagarme.{$type}") : false;
  25. }
  26. public function charge_id(){
  27. return $this->charge->id;
  28. }
  29. public function isActive(){
  30. return ($this->plan->getStatus() !== 'canceled');
  31. }
  32. public function create_a_customer($token, User $user){
  33. $this->customer = new PagarMe_Transaction([
  34. "card_hash" => $token,
  35. 'customer'=>[
  36. 'name' => $user->name,
  37. 'document_number' => $user->id,
  38. 'email' => $user->email
  39. ]
  40. ]
  41. );
  42. dd(['customer',$this->customer->getCustomer()]);
  43. }
  44. public function subscribe_a_customer(Subscription $subscription, User $user, $token){
  45. $plan_id = $this->plan_id($subscription->name);
  46. if (empty($plan_id)) {
  47. return false;
  48. }
  49. $this->plan = new \PagarMe_Subscription([
  50. "card_hash" => $token,
  51. "plan_id"=>$plan_id,
  52. 'customer'=>[
  53. 'name' => $user->name,
  54. 'email' => $user->email
  55. ]
  56. ]);
  57. try {
  58. $this->plan->create();
  59. }
  60. catch(PagarMe_Exception $e){
  61. throw new GeneralException($e->getMessage());
  62. }
  63. $this->customer=$this->plan->getCustomer();
  64. $this->charge=$this->plan;
  65. }
  66. public function charge_a_customer(Subscription $subscription, Customer $customer){
  67. }
  68. public function cancel_a_subscription(Customer $customer, Charge $charge){
  69. try {
  70. $this->plan = PagarMe_Subscription::findById($charge->charge_id);
  71. $this->plan->cancel(); // Cancel
  72. }
  73. catch(PagarMe_Exception $e){
  74. throw new GeneralException($e->getMessage());
  75. }
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement