Completed
Push — master ( e93f60...2f787f )
by Mahmoud
08:56 queued 02:41
created

ChargeWithPaypalTask::run()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 87
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 87
rs 8.6296
c 0
b 0
f 0
cc 3
eloc 56
nc 3
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Containers\Paypal\Tasks;
4
5
use Anouar\Paypalpayment\PaypalPayment;
6
use App\Containers\Payment\Contracts\Chargeable;
7
use App\Containers\Paypal\Exceptions\PaypalApiErrorException;
8
use App\Containers\User\Models\User;
9
use App\Port\Task\Abstracts\Task;
10
use Exception;
11
use Illuminate\Support\Facades\Config;
12
13
/**
14
 * Class ChargeWithPaypalTask.
15
 *
16
 * @author Mahmoud Zalt <[email protected]>
17
 */
18
class ChargeWithPaypalTask extends Task implements Chargeable
19
{
20
21
    /**
22
     * @var  \Anouar\Paypalpayment\PaypalPayment
23
     */
24
    private $paypalPayment;
25
26
    /**
27
     * ChargeWithPaypalTask constructor.
28
     *
29
     * @param \Anouar\Paypalpayment\PaypalPayment $paypalPayment
30
     */
31
    public function __construct(Paypalpayment $paypalPayment)
32
    {
33
        $this->paypalPayment = $paypalPayment;
34
35
        $this->paypalApi = $paypalPayment->apiContext(
0 ignored issues
show
Bug introduced by
The property paypalApi does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
            Config::get('paypal_payment.Account.ClientId'),
37
            Config::get('paypal_payment.Account.ClientSecret')
38
        );
39
    }
40
41
    /**
42
     * @param \App\Containers\User\Models\User $user
43
     * @param                                  $amount
44
     * @param string                           $currency
45
     *
46
     * @return  array|null
47
     */
48
    public function run(User $user, $amount, $currency = 'USD')
0 ignored issues
show
Unused Code introduced by
The parameter $user is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $amount is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $currency is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
    {
50
51
        // TODO:
52
        // this is code sample from the package readme.
53
        // this does not seem to be compatible with paypal
54
        // I need to remove this package (Paypalpayment) and replace with something else
55
        // was checking the Payum laravel package seems compatible with Paypal Express Checkout
56
57
        $card = $this->paypalPayment->creditCard();
58
        $card->setType("visa")
59
            ->setNumber("4758411877817150")
60
            ->setExpireMonth("05")
61
            ->setExpireYear("2019")
62
            ->setCvv2("456")
63
            ->setFirstName("Joe")
64
            ->setLastName("Shopper");
65
66
        $fi = $this->paypalPayment->fundingInstrument();
67
        $fi->setCreditCard($card);
68
69
        $payer = $this->paypalPayment->payer();
70
        $payer->setPaymentMethod("credit_card")
71
            ->setFundingInstruments(array($fi));
72
73
        $item1 = $this->paypalPayment->item();
74
        $item1->setName('Ground Coffee 40 oz')
75
            ->setDescription('Ground Coffee 40 oz')
76
            ->setCurrency('USD')
77
            ->setQuantity(1)
78
            ->setTax(0.3)
79
            ->setPrice(7.50);
80
81
        $item2 = $this->paypalPayment->item();
82
        $item2->setName('Granola bars')
83
            ->setDescription('Granola Bars with Peanuts')
84
            ->setCurrency('USD')
85
            ->setQuantity(5)
86
            ->setTax(0.2)
87
            ->setPrice(2);
88
89
        $itemList = $this->paypalPayment->itemList();
90
        $itemList->setItems(array($item1, $item2));
91
92
        $details = $this->paypalPayment->details();
93
        $details->setShipping("1.2")
94
            ->setTax("1.3")
95
            //total of items prices
96
            ->setSubtotal("17.5");
97
98
        //Payment Amount
99
        $amount = $this->paypalPayment->amount();
100
        $amount->setCurrency("USD")
101
            // the total is $17.8 = (16 + 0.6) * 1 ( of quantity) + 1.2 ( of Shipping).
102
            ->setTotal("20")
103
            ->setDetails($details);
104
105
        $transaction = $this->paypalPayment->transaction();
106
        $transaction->setAmount($amount)
107
            ->setItemList($itemList)
108
            ->setDescription("Payment description")
109
            ->setInvoiceNumber(uniqid());
110
111
        $payment = $this->paypalPayment->payment();
112
113
        $payment->setIntent("sale")
114
            ->setPayer($payer)
115
            ->setTransactions(array($transaction));
116
117
        try {
118
119
            $response = $payment->create($this->paypalApi);
120
121
        } catch (Exception $e) {
122
            throw (new PaypalApiErrorException('Paypal API error (payment)'))->debug($e->getMessage(), true);
123
        }
124
125
        if ($response['state'] != 'approved') {
126
            throw new PaypalApiErrorException('Paypal response status not succeeded (payment)');
127
        }
128
129
        // this data will be stored on the pivot table (user credits)
130
        return [
131
            'payment_method' => 'paypal',
132
            'description'    => $response['id'], // Pay ID
133
        ];
134
    }
135
136
    /**
137
     * @param \App\Containers\User\Models\User $user
138
     * @param                                  $amount
139
     * @param string                           $currency
140
     *
141
     * @return  array|null
142
     */
143
    public function charge(User $user, $amount, $currency = 'USD')
144
    {
145
        return $this->run($user, $amount, $currency);
146
    }
147
148
}
149