Completed
Push — master ( 857394...4794c5 )
by Mahmoud
03:25
created

ChargeWithPaypalTask::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
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(
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')
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