Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
39 / 39
Payment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
9 / 9
11
100.00% covered (success)
100.00%
39 / 39
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
10 / 10
 setCustomer
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 setCustomerPhone
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 setCustomerCPF
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 setShipping
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
3 / 3
 setShippingAddress
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 setOrder
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
9 / 9
 getLink
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getCode
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
1<?php namespace Framework\PagSeguro;
2
3use Framework\Shop\PaymentInterface;
4use Framework\Shop\Product;
5use PagSeguro\Configuration\Configure;
6use PagSeguro\Domains\Requests\Payment as PagSeguroPayment;
7use PagSeguro\Library;
8
9class Payment implements PaymentInterface
10{
11    public const SHIP_TYPE_PAC = \PagSeguro\Enum\Shipping\Type::PAC;
12    public const SHIP_TYPE_SEDEX = \PagSeguro\Enum\Shipping\Type::SEDEX;
13    public const SHIP_TYPE_NOT_SPECIFIED = \PagSeguro\Enum\Shipping\Type::NOT_SPECIFIED;
14    protected PagSeguroPayment $payment;
15
16    public function __construct(Config $config)
17    {
18        Library::initialize();
19        Configure::setEnvironment($config->environment);
20        Configure::setAccountCredentials($config->email, $config->token);
21        Configure::setCharset($config->charset);
22        Configure::setLog($config->logActive, $config->logPath);
23        $this->payment = new PagSeguroPayment();
24        $this->payment->setCurrency($config->currency);
25        $this->payment->setRedirectUrl($config->redirectURL);
26        $this->payment->setNotificationUrl($config->notificationURL);
27    }
28
29    public function setCustomer(string $name, string $email)
30    {
31        $this->payment->setSender()->setName($name);
32        $this->payment->setSender()->setEmail($email);
33        return $this;
34    }
35
36    public function setCustomerPhone($areaCode, $number)
37    {
38        $this->payment->setSender()->setPhone()->withParameters(
39            $areaCode,
40            $number
41        );
42        return $this;
43    }
44
45    public function setCustomerCPF($cpf)
46    {
47        $this->payment->setSender()->setDocument()->withParameters(
48            'CPF',
49            $cpf
50        );
51        return $this;
52    }
53
54    /**
55     * @param float $cost Shipping price
56     * @param int   $type One of the SHIP_TYPE_ constants
57     *
58     * @return $this
59     */
60    public function setShipping(float $cost, int $type)
61    {
62        $this->payment->setShipping()->setCost()->withParameters($cost);
63        $this->payment->setShipping()->setType()->withParameters($type);
64        return $this;
65    }
66
67    /**
68     * @param string      $street
69     * @param int         $number
70     * @param string      $district
71     * @param string      $postalCode
72     * @param string      $city
73     * @param string      $state      State code with 2 chars
74     * @param string      $country    Country code with 3 chars
75     * @param string|null $complement
76     *
77     * @return $this
78     */
79    public function setShippingAddress(
80        string $street,
81        int $number,
82        string $district,
83        string $postalCode,
84        string $city,
85        string $state,
86        string $country = 'BRA',
87        string $complement = null
88    ) {
89        $this->payment->setShipping()->setAddress()->withParameters(
90            $street,
91            $number,
92            $district,
93            $postalCode,
94            $city,
95            $state,
96            $country,
97            $complement = null
98        );
99        return $this;
100    }
101
102    /**
103     * @param array|Product[] $products
104     * @param string|null     $invoice_number
105     *
106     * @return $this
107     */
108    public function setOrder(array $products, string $invoice_number = null)
109    {
110        if ($invoice_number !== null) {
111            $this->payment->setReference($invoice_number);
112        }
113
114        foreach ($products as $product) {
115            $this->payment->addItems()->withParameters(
116                $product->id,
117                $product->title,
118                $product->quantity,
119                $product->price
120            );
121        }
122        return $this;
123    }
124
125    public function getLink() : string
126    {
127        $credentials = Configure::getAccountCredentials();
128        return $this->payment->register($credentials);
129    }
130
131    public function getCode() : string
132    {
133        $credentials = Configure::getAccountCredentials();
134        return $this->payment->register($credentials, true)->getCode();
135    }
136}