PayoutService::createPayout()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 21
c 1
b 0
f 0
nc 2
nop 6
dl 0
loc 34
rs 9.584
1
<?php
2
3
namespace App\Service\Paypal;
4
5
use Exception;
6
use PayPal\Api\Currency;
7
use PayPal\Api\Payout;
8
use PayPal\Api\PayoutBatch;
9
use PayPal\Api\PayoutItem;
10
use PayPal\Api\PayoutSenderBatchHeader;
11
12
/**
13
 * Class PayoutsService
14
 * @package App\Service\Paypal
15
 */
16
class PayoutService extends AbstractPaypalService
17
{
18
    /**
19
     * @param string $subject
20
     * @param string $note
21
     * @param string $receiverEmail
22
     * @param string $itemId
23
     * @param float $amount
24
     * @param string $currency
25
     * @return PayoutBatch|null
26
     */
27
    public function createPayout(
28
        string $subject,
29
        string $note,
30
        string $receiverEmail,
31
        string $itemId,
32
        float $amount,
33
        string $currency
34
    ): ?PayoutBatch {
35
        $payout = new Payout();
36
        $senderBatchHeader = new PayoutSenderBatchHeader();
37
        $senderBatchHeader
38
            ->setSenderBatchId(uniqid())
39
            ->setEmailSubject($subject);
40
        $senderItem = new PayoutItem();
41
        $senderItem->setRecipientType('Email')
42
            ->setNote($note)
43
            ->setReceiver($receiverEmail)
44
            ->setSenderItemId($itemId)
45
            ->setAmount(new Currency(json_encode((object) [
46
                'value' => $amount,
47
                'currency' => $currency,
48
            ])));
49
50
        $payout->setSenderBatchHeader($senderBatchHeader)
51
            ->addItem($senderItem);
52
53
        try {
54
            $payouts = $payout->create(null, $this->apiContext);
55
        } catch (Exception $e) {
56
            $this->logger->error('Error on PayPal::createPayout = ' . $e->getMessage());
57
            return null;
58
        }
59
60
        return $payouts;
61
    }
62
63
    /**
64
     * @param string $payoutId
65
     * @return PayoutBatch|null
66
     */
67
    public function getPayout(string $payoutId): ?PayoutBatch
68
    {
69
        $payout = new Payout();
70
        try {
71
            $payout = $payout->get($payoutId, $this->apiContext);
72
        } catch (Exception $e) {
73
            $this->logger->error('Error on PayPal::getPayout = ' . $e->getMessage());
74
            return null;
75
        }
76
77
        return $payout;
78
    }
79
}
80