Completed
Push — gocardless-upgrade ( ab99fa )
by Arthur
02:58
created

GoCardlessPaymentController::handleManualBill()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 19
nc 2
nop 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B GoCardlessPaymentController::getName() 0 16 6
1
<?php namespace BB\Http\Controllers;
2
3
use BB\Entities\User;
4
5
class GoCardlessPaymentController extends Controller
6
{
7
    /**
8
     * @var \BB\Repo\PaymentRepository
9
     */
10
    private $paymentRepository;
11
    /**
12
     * @var \BB\Helpers\GoCardlessHelper
13
     */
14
    private $goCardless;
15
16
    function __construct(\BB\Repo\PaymentRepository $paymentRepository, \BB\Helpers\GoCardlessHelper $goCardless)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
17
    {
18
        $this->paymentRepository = $paymentRepository;
19
20
        $this->middleware('role:member', array('only' => ['create', 'store']));
21
        $this->goCardless = $goCardless;
22
    }
23
24
    /**
25
     * Main entry point for all gocardless payments - not subscriptions
26
     * @param $userId
27
     * @return mixed
28
     * @throws \BB\Exceptions\AuthenticationException
29
     */
30
    public function create($userId)
31
    {
32
        $user = User::findWithPermission($userId);
33
34
        $requestData = \Request::only(['reason', 'amount', 'return_path']);
35
36
        $reason = $requestData['reason'];
37
        $amount = ($requestData['amount'] * 1) / 100;
38
        $returnPath = $requestData['return_path'];
39
        $ref = $this->getReference($reason);
40
41
        if ($user->payment_method == 'gocardless-variable') {
42
43
            return $this->handleBill($amount, $reason, $user, $ref, $returnPath);
44
45
        } elseif ($user->payment_method == 'gocardless') {
46
47
            return $this->ddMigratePrompt($returnPath);
48
49
        } else {
50
51
            abort(500, 'Not supported');
52
53
        }
54
    }
55
56
    private function ddMigratePrompt($returnPath)
57
    {
58
        if (\Request::wantsJson()) {
59
            return \Response::json(['error' => 'Please visit the "Your Membership" page and migrate your Direct Debit first, then return and make the payment'], 400);
60
        }
61
        \Notification::error("Please visit the \"Your Membership\" page and migrate your Direct Debit first, then return and make the payment");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Please visit the \"Your ...rn and make the payment does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
62
        return \Redirect::to($returnPath);
63
    }
64
65
    /**
66
     * Process a direct debit payment when we have a preauth
67
     *
68
     * @param $amount
69
     * @param $reason
70
     * @param User $user
71
     * @param $ref
72
     * @param $returnPath
73
     * @return mixed
74
     */
75
    private function handleBill($amount, $reason, $user, $ref, $returnPath)
76
    {
77
        if (is_null($ref)) {
78
            $ref = '';
79
        }
80
        $bill = $this->goCardless->newBill($user->subscription_id, $amount * 100, $this->goCardless->getNameFromReason($reason));
81
        //dd($bill);
82
83
        if ($bill) {
84
            //Store the payment
85
            $fee = 0;
86
            $paymentSourceId = $bill->id;
87
            $amount = $bill->amount / 100;
88
            $status = $bill->status;
89
            if ($status == 'pending_submission') {
90
                $status = 'pending';
91
            }
92
93
            //The record payment process will make the necessary record updates
94
            $this->paymentRepository->recordPayment($reason, $user->id, 'gocardless-variable', $paymentSourceId, $amount, $status, $fee, $ref);
95
96
            if (\Request::wantsJson()) {
97
                return \Response::json(['message' => 'The payment was submitted successfully']);
98
            }
99
100
            \Notification::success("The payment was submitted successfully");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal The payment was submitted successfully does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
101
        } else {
102
            //something went wrong - we still have the pre auth though
103
104
            if (\Request::wantsJson()) {
105
                return \Response::json(['error' => 'There was a problem charging your account'], 400);
106
            }
107
108
            \Notification::error("There was a problem charging your account");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal There was a problem charging your account does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
109
        }
110
111
        return \Redirect::to($returnPath);
112
    }
113
114
115
116
    private function getDescription($reason)
117
    {
118
        if ($reason == 'subscription') {
119
            return "Monthly Subscription Fee - Manual";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Monthly Subscription Fee - Manual does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
120
        } elseif ($reason == 'induction') {
121
            return strtoupper(\Input::get('induction_key')) . " Induction Fee";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Induction Fee does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
122
        } elseif ($reason == 'door-key') {
123
            return "Door Key Deposit";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Door Key Deposit does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
124
        } elseif ($reason == 'storage-box') {
125
            return "Storage Box Deposit";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Storage Box Deposit does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
126
        } elseif ($reason == 'balance') {
127
            return "BB Credit Payment";
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BB Credit Payment does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
128
        } else {
129
            throw new \BB\Exceptions\NotImplementedException();
130
        }
131
    }
132
133
    private function getName($reason, $userId)
134
    {
135
        if ($reason == 'subscription') {
136
            return strtoupper("BBSUB" . $userId . ":MANUAL");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBSUB does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal :MANUAL does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
137
        } elseif ($reason == 'induction') {
138
            return strtoupper("BBINDUCTION" . $userId . ":" . \Request::get('induction_key'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBINDUCTION does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal : does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
139
        } elseif ($reason == 'door-key') {
140
            return strtoupper("BBDOORKEY" . $userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBDOORKEY does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
141
        } elseif ($reason == 'storage-box') {
142
            return strtoupper("BBSTORAGEBOX" . $userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBSTORAGEBOX does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
143
        } elseif ($reason == 'balance') {
144
            return strtoupper("BBBALANCE" . $userId);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal BBBALANCE does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
145
        } else {
146
            throw new \BB\Exceptions\NotImplementedException();
147
        }
148
    }
149
150
    private function getReference($reason)
151
    {
152
        if ($reason == 'induction') {
153
            return \Request::get('ref');
154
        } elseif ($reason == 'balance') {
155
            return \Request::get('reference');
156
        }
157
        return false;
158
    }
159
}
160