Passed
Push — master ( b66f88...8b7870 )
by Kelvin
03:33
created

Chpter::hostedRedirectPayment()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 3
nop 3
dl 0
loc 23
rs 9.7998
1
<?php
2
3
namespace KiplingKelvin\ChpterLaravelSdk;
4
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Exception\BadResponseException;
7
8
class Chpter 
9
{
10
    public $chpter_payment_url;
11
    public $chpter_hosted_redirect_payment_url;
12
    public $chpter_express_redirect_payment_url;
13
14
    public $chpter_accounts_token_renewal_url;
15
    public $token;
16
    public $domain;
17
  
18
    /**
19
     * Construct method
20
     *
21
     * Initializes the class with an array of API values.
22
     *
23
     * @param array $config
24
     * @return void
25
     * @throws exception if the values array is not valid
26
     */
27
28
29
30
    public function __construct()
31
    {
32
        //Base URL for the API endpoints. This is basically the 'common' part of the API endpoints
33
         $this->chpter_payment_url = config('chpter.payment_url'); 	
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
         $this->chpter_payment_url = /** @scrutinizer ignore-call */ config('chpter.payment_url'); 	
Loading history...
34
         $this->chpter_hosted_redirect_payment_url = config('chpter.hosted_redirect_payment_url'); 
35
         $this->chpter_express_redirect_payment_url = config('chpter.express_redirect_payment_url'); 	
36
37
         $this->chpter_accounts_token_renewal_url = config('chpter.accounts_token_renewal_url'); 
38
39
40
         $this->token = config('chpter.chpter_token'); 
41
         $this->domain =config('chpter.domain');
42
    }
43
44
45
    public function mpesaPayment($customer, $products, $amount, $callback_details)
46
    {
47
48
        $client  = new Client();
49
50
        $requestBody = array( 
51
            "customer_details"=> $customer,
52
            "products"=> $products,
53
            "amount"=> $amount,
54
            "callback_details"=> $callback_details,
55
        );
56
57
        try {
58
            $response = $client->post($this->chpter_payment_url, [
59
                "headers" => [
60
                    "Authorization" => "Token {$this->token}",
61
                    "domain"  => $this->domain,
62
                ],
63
                "json"    => $requestBody,
64
            ]);
65
66
            return json_decode((string) $response->getBody(), true);
67
        } catch (BadResponseException $exception) {
68
            return json_decode((string) $exception->getResponse()->getBody()->getContents(), true);
69
        }
70
71
    }
72
73
    public function hostedRedirectPayment($customer, $amount, $redirect_urls)
74
    {
75
76
        $client  = new Client();
77
        
78
        $requestBody = array( 
79
            "customer_details"=> $customer,
80
            "amount"=> $amount,
81
            "redirect_urls"=> $redirect_urls,
82
        );
83
84
        try {
85
            $response = $client->post($this->chpter_hosted_redirect_payment_url, [
86
                "headers" => [
87
                    "Authorization" => "Token {$this->token}",
88
                    "domain"  => $this->domain,
89
                ],
90
                "json"    => $requestBody,
91
            ]);
92
93
            return json_decode((string) $response->getBody(), true);
94
        } catch (BadResponseException $exception) {
95
            return json_decode((string) $exception->getResponse()->getBody()->getContents(), true);
96
        }
97
    }
98
99
    public function expressRedirectPayment($transaction_data, $redirect_urls)
100
    {
101
102
        $client  = new Client();
103
        
104
        $requestBody = array( 
105
            "transaction_data"=> $transaction_data,
106
            "redirect_urls"=> $redirect_urls,
107
        );
108
109
        try {
110
            $response = $client->post($this->chpter_express_redirect_payment_url, [
111
                "headers" => [
112
                    "Authorization" => "Token {$this->token}",
113
                    "domain"  => $this->domain,
114
                ],
115
                "json"    => $requestBody,
116
            ]);
117
118
            return json_decode((string) $response->getBody(), true);
119
        } catch (BadResponseException $exception) {
120
            return json_decode((string) $exception->getResponse()->getBody()->getContents(), true);
121
        }
122
    }
123
124
    public function accountsTokenRenewal()
125
    {
126
127
        $client  = new Client();
128
        
129
        try {
130
            $response = $client->post($this->chpter_accounts_token_renewal_url, [
131
                "headers" => [
132
                    "Authorization" => "Token {$this->token}",
133
                    "domain"  => $this->domain,
134
                ],
135
                "json"    => null,
136
            ]);
137
138
            return json_decode((string) $response->getBody(), true);
139
        } catch (BadResponseException $exception) {
140
            return json_decode((string) $exception->getResponse()->getBody()->getContents(), true);
141
        }
142
    }
143
144
}
145