|
| 1 | +<?php |
| 2 | + |
| 3 | +class Paylike { |
| 4 | + private $key; |
| 5 | + |
| 6 | + // subsystems |
| 7 | + private $transactions; |
| 8 | + |
| 9 | + public function __construct( $key ){ |
| 10 | + $this->key = $key; |
| 11 | + } |
| 12 | + |
| 13 | + public function setKey( $key ){ |
| 14 | + $this->key = $key; |
| 15 | + } |
| 16 | + |
| 17 | + public function getKey(){ |
| 18 | + return $this->key; |
| 19 | + } |
| 20 | + |
| 21 | + public function __get( $name ){ |
| 22 | + switch ($name) { |
| 23 | + case 'transactions': |
| 24 | + if (!$this->transactions) |
| 25 | + $this->transactions = new PaylikeTransactions($this); |
| 26 | + |
| 27 | + return $this->transactions; |
| 28 | + |
| 29 | + default: |
| 30 | + throw new BadPropertyException($this, $name); |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class PaylikeTransactions extends PaylikeSubsystem { |
| 36 | + public function fetch( $transactionId ){ |
| 37 | + return $this->request('GET', '/transactions/'.$transactionId); |
| 38 | + } |
| 39 | + |
| 40 | + public function capture( $transactionId, $opts ){ |
| 41 | + return $this->request('POST', '/transactions/'.$transactionId.'/captures', $opts); |
| 42 | + } |
| 43 | + |
| 44 | + public function refund( $transactionId, $opts ){ |
| 45 | + return $this->request('POST', '/transactions/'.$transactionId.'/refunds', $opts); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class PaylikeSubsystem { |
| 50 | + private $paylike; |
| 51 | + |
| 52 | + public function __construct( $paylike ){ |
| 53 | + $this->paylike = $paylike; |
| 54 | + } |
| 55 | + |
| 56 | + protected function request( $verb, $path, $data = null ){ |
| 57 | + $c = curl_init(); |
| 58 | + |
| 59 | + curl_setopt($c, CURLOPT_URL, 'https://api.paylike.io'.$path); |
| 60 | + |
| 61 | + if ($this->paylike->getKey() !== null) |
| 62 | + curl_setopt($c, CURLOPT_USERPWD, ':'.$this->paylike->getKey()); |
| 63 | + |
| 64 | + if (in_array($verb, [ 'POST', 'PUT', 'PATCH' ])) |
| 65 | + curl_setopt($c, CURLOPT_POSTFIELDS, $data); |
| 66 | + |
| 67 | + if (in_array($verb, [ 'GET', 'POST' ])) |
| 68 | + curl_setopt($c, CURLOPT_RETURNTRANSFER, true); |
| 69 | + |
| 70 | + $raw = curl_exec($c); |
| 71 | + |
| 72 | + $code = curl_getinfo($c, CURLINFO_HTTP_CODE); |
| 73 | + |
| 74 | + curl_close($c); |
| 75 | + |
| 76 | + if ($code < 200 || $code > 299) |
| 77 | + return false; |
| 78 | + |
| 79 | + if ($code === 204) // No Content |
| 80 | + return true; |
| 81 | + |
| 82 | + return json_decode($raw); |
| 83 | + } |
| 84 | +} |
0 commit comments