|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gbowo\Adapter\Amplifypay\Plugin; |
|
4
|
|
|
|
|
5
|
|
|
use Gbowo\Plugin\AbstractPlugin; |
|
6
|
|
|
use function GuzzleHttp\json_decode; |
|
7
|
|
|
use function GuzzleHttp\json_encode; |
|
8
|
|
|
use Gbowo\Adapter\Amplifypay\Traits\KeyVerifier; |
|
9
|
|
|
use Gbowo\Exception\InvalidHttpResponseException; |
|
10
|
|
|
use Gbowo\Exception\TransactionVerficationFailedException; |
|
11
|
|
|
|
|
12
|
|
|
class UnsubscribeCustomer extends AbstractPlugin |
|
13
|
|
|
{ |
|
14
|
|
|
use KeyVerifier; |
|
15
|
|
|
|
|
16
|
|
|
const UN_SUBSCRIBE_LINK = "/subscription/cancel"; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* It's with a double `ll` |
|
20
|
|
|
* @see https://amplifypay.com/developers Unsubscribe a customer from plan |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
const STATUS_DESCRIPTION_SUCCESS = "Successfull Request"; |
|
24
|
|
|
|
|
25
|
|
|
const STATUS_DESCRIPTION_FAILURE = "Unsuccessfull Request"; |
|
26
|
|
|
|
|
27
|
|
|
protected $baseUrl; |
|
28
|
|
|
|
|
29
|
|
|
protected $apiKeys; |
|
30
|
|
|
|
|
31
|
25 |
|
public function __construct(string $baseUrl, array $apiKeys) |
|
32
|
|
|
{ |
|
33
|
25 |
|
$this->baseUrl = $baseUrl; |
|
34
|
25 |
|
$this->apiKeys = $apiKeys; |
|
35
|
25 |
|
} |
|
36
|
|
|
|
|
37
|
25 |
|
public function getPluginAccessor() :string |
|
38
|
|
|
{ |
|
39
|
25 |
|
return "unsubcribeCustomerFromPlan"; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param array $args |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
* @throws \Gbowo\Exception\TransactionVerficationFailedException |
|
46
|
|
|
* @throws \Gbowo\Exception\InvalidHttpResponseException if we don't get a 200 Status code |
|
47
|
|
|
*/ |
|
48
|
3 |
|
public function handle(array $args) |
|
49
|
|
|
{ |
|
50
|
3 |
|
$link = $this->baseUrl . self::UN_SUBSCRIBE_LINK; |
|
51
|
|
|
|
|
52
|
3 |
|
$response = $this->adapter->getHttpClient() |
|
53
|
3 |
|
->post( |
|
54
|
3 |
|
$link, [ |
|
55
|
3 |
|
'body' => json_encode(array_merge($this->apiKeys, $args)) |
|
56
|
|
|
] |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
3 |
|
if ($response->getStatusCode() !== 200) { |
|
60
|
1 |
|
throw InvalidHttpResponseException::createFromResponse($response); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
$res = json_decode($response->getBody(), true); |
|
64
|
|
|
|
|
65
|
2 |
|
$validated = false; |
|
66
|
|
|
|
|
67
|
2 |
|
if (strcmp($res['StatusDesc'], self::STATUS_DESCRIPTION_SUCCESS) === 0) { |
|
68
|
1 |
|
$validated = true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
if (false === $validated) { |
|
72
|
1 |
|
throw TransactionVerficationFailedException::createFromResponse($response); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
//not consistent, some transaction with the Amplifypay API returns `ApiKey`, some `apiKey`. |
|
76
|
|
|
|
|
77
|
1 |
|
$this->verifyKeys($res['apiKey'], $this->apiKeys['apiKey']); |
|
78
|
|
|
|
|
79
|
1 |
|
return $res; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|