ChargeWithWepayTask::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Containers\Wepay\Tasks;
4
5
use App\Containers\Payment\Contracts\ChargeableInterface;
6
use App\Containers\Payment\Contracts\PaymentChargerInterface;
7
use App\Containers\Payment\Models\AbstractPaymentAccount;
8
use App\Containers\Payment\Models\PaymentTransaction;
9
use App\Containers\Wepay\Exceptions\WepayApiErrorException;
10
use App\Ship\Parents\Tasks\Task;
11
use Exception;
12
use Illuminate\Support\Facades\Config;
13
use KevinEm\WePay\Laravel\WePayLaravel;
14
15
/**
16
 * Class ChargeWithWepayTask
17
 *
18
 * @author  Rockers Technologies <[email protected]>
19
 * @author  Mahmoud Zalt  <[email protected]>
20
 */
21
class ChargeWithWepayTask extends Task implements PaymentChargerInterface
22
{
23
24
    public $wepayLaravel;
25
26
    /**
27
     * WepayApi constructor.
28
     *
29
     * @param \KevinEm\WePay\Laravel\WePayLaravel $wepayLaravel
30
     */
31
    public function __construct(WePayLaravel $wepayLaravel)
32
    {
33
        $this->wepayLaravel = $wepayLaravel->make(
34
            Config::get('wepay-container.client_secret'),
35
            Config::get('wepay-container.version')
36
        );
37
    }
38
39
    /**
40
     * @param \App\Containers\Payment\Contracts\ChargeableInterface $user
41
     * @param \App\Containers\Payment\Models\AbstractPaymentAccount $account
42
     * @param float                                                 $amount
43
     * @param string                                                $currency
44
     *
45
     * @return PaymentTransaction
46
     * @throws WepayApiErrorException
47
     */
48
    public function charge(ChargeableInterface $user, AbstractPaymentAccount $account, $amount, $currency = 'USD') : PaymentTransaction
49
    {
50
//        $valid = $account->checkIfPaymentDataIsSet([...]);
51
//
52
//        if(!$valid){
53
//            throw new WepayAccountNotFoundException('We could not find your credit card information.
54
//            For security reasons, we do not store your credit card information on our server.
55
//            So please login to our Web App and enter your credit card information directly into Stripe,
56
//            then try to purchase the credits again.
57
//            Thanks.');
58
//        }
59
60
        try {
61
62
            $response = $this->wepayLaravel->charges()->create([
63
                'customer' => $user->wepayAccount->customer_id,
0 ignored issues
show
Bug introduced by Jaimin Suthar
Accessing wepayAccount on the interface App\Containers\Payment\C...cts\ChargeableInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
64
                'currency' => $currency,
65
                'amount'   => $amount,
66
            ]);
67
68
        } catch (Exception $e) {
69
            throw (new WepayApiErrorException('Wepay API error (chargeCustomer)'))->debug($e->getMessage(), true);
70
        }
71
72
        if ($response['status'] != 'succeeded') {
73
            throw new WepayApiErrorException('Wepay response status not succeeded (chargeCustomer)');
74
        }
75
76
        if ($response['paid'] !== true) {
77
            throw new WepayApiErrorException('Transaction was not set to PAID.');
78
        }
79
80
        $transaction = new PaymentTransaction([
81
            'transaction_id' => $response['id'],
82
            'status' => $response['status'],
83
            'is_successful' => true,
84
            'data' => $response,
85
        ]);
86
87
        return $transaction;
88
    }
89
90
}
91