Completed
Push — master ( cb9cb0...a2a2e1 )
by
unknown
43:00 queued 13:11
created

AuthorizeNetDPM::getCreditCardForm()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 121
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 12
Metric Value
dl 0
loc 121
ccs 27
cts 27
cp 1
rs 2
cc 12
eloc 28
nc 2048
nop 7
crap 12

How to fix   Long Method    Complexity   

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
 * Demonstrates the Direct Post Method.
4
 *
5
 * To implement the Direct Post Method you need to implement 3 steps:
6
 *
7
 * Step 1: Add necessary hidden fields to your checkout form and make your form is set to post to AuthorizeNet.
8
 *
9
 * Step 2: Receive a response from AuthorizeNet, do your business logic, and return
10
 *         a relay response snippet with a url to redirect the customer to.
11
 *
12
 * Step 3: Show a receipt page to your customer.
13
 *
14
 * This class is more for demonstration purposes than actual production use.
15
 *
16
 *
17
 * @package    AuthorizeNet
18
 * @subpackage AuthorizeNetDPM
19
 */
20
21
/**
22
 * A class that demonstrates the DPM method.
23
 *
24
 * @package    AuthorizeNet
25
 * @subpackage AuthorizeNetDPM
26
 */
27
class AuthorizeNetDPM extends AuthorizeNetSIM_Form
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
28
{
29
30
    const LIVE_URL = 'https://secure2.authorize.net/gateway/transact.dll';
31
    const SANDBOX_URL = 'https://test.authorize.net/gateway/transact.dll';
32
33
    /**
34
     * Implements all 3 steps of the Direct Post Method for demonstration
35
     * purposes.
36
     */
37
    public static function directPostDemo($url, $api_login_id, $transaction_key, $amount = "0.00", $md5_setting = "")
0 ignored issues
show
Coding Style introduced by
directPostDemo uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
Coding Style introduced by
directPostDemo uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
38
    {
39
        
40
        // Step 1: Show checkout form to customer.
41
        if (!count($_POST) && !count($_GET))
42
        {
43
            $fp_sequence = time(); // Any sequential number like an invoice number.
44
            echo AuthorizeNetDPM::getCreditCardForm($amount, $fp_sequence, $url, $api_login_id, $transaction_key);
45
        }
46
        // Step 2: Handle AuthorizeNet Transaction Result & return snippet.
47
        elseif (count($_POST)) 
48
        {
49
            $response = new AuthorizeNetSIM($api_login_id, $md5_setting);
50
            if ($response->isAuthorizeNet()) 
51
            {
52
                if ($response->approved) 
53
                {
54
                    // Do your processing here.
55
                    $redirect_url = $url . '?response_code=1&transaction_id=' . $response->transaction_id; 
56
                }
57
                else
58
                {
59
                    // Redirect to error page.
60
                    $redirect_url = $url . '?response_code='.$response->response_code . '&response_reason_text=' . $response->response_reason_text;
61
                }
62
                // Send the Javascript back to AuthorizeNet, which will redirect user back to your site.
63
                echo AuthorizeNetDPM::getRelayResponseSnippet($redirect_url);
64
            }
65
            else
66
            {
67
                echo "Error -- not AuthorizeNet. Check your MD5 Setting.";
68
            }
69
        }
70
        // Step 3: Show receipt page to customer.
71
        elseif (!count($_POST) && count($_GET))
72
        {
73
            if ($_GET['response_code'] == 1)
74
            {
75
                echo "Thank you for your purchase! Transaction id: " . htmlentities($_GET['transaction_id']);
76
            }
77
            else
78
            {
79
              echo "Sorry, an error occurred: " . htmlentities($_GET['response_reason_text']);
80
            }
81
        }
82
    }
83
    
84
    /**
85
     * A snippet to send to AuthorizeNet to redirect the user back to the
86
     * merchant's server. Use this on your relay response page.
87
     *
88
     * @param string $redirect_url Where to redirect the user.
89
     *
90
     * @return string
91
     */
92 1
    public static function getRelayResponseSnippet($redirect_url)
93
    {
94
        return "<html><head><script language=\"javascript\">
95
                <!--
96 1
                window.location=\"{$redirect_url}\";
97
                //-->
98
                </script>
99 1
                </head><body><noscript><meta http-equiv=\"refresh\" content=\"1;url={$redirect_url}\"></noscript></body></html>";
100
    }
101
    
102
    /**
103
     * Generate a sample form for use in a demo Direct Post implementation.
104
     *
105
     * @param string $amount                   Amount of the transaction.
106
     * @param string $fp_sequence              Sequential number(ie. Invoice #)
107
     * @param string $relay_response_url       The Relay Response URL
108
     * @param string $api_login_id             Your API Login ID
109
     * @param string $transaction_key          Your API Tran Key.
110
     * @param bool   $test_mode                Use the sandbox?
111
     * @param bool   $prefill                  Prefill sample values(for test purposes).
112
     *
113
     * @return string
114
     */
115 1
    public static function getCreditCardForm($amount, $fp_sequence, $relay_response_url, $api_login_id, $transaction_key, $test_mode = true, $prefill = true)
116
    {
117 1
        $time = time();
118 1
        $fp = self::getFingerprint($api_login_id, $transaction_key, $amount, $fp_sequence, $time);
119 1
        $sim = new AuthorizeNetSIM_Form(
120
            array(
121 1
            'x_amount'        => $amount,
122 1
            'x_fp_sequence'   => $fp_sequence,
123 1
            'x_fp_hash'       => $fp,
124 1
            'x_fp_timestamp'  => $time,
125 1
            'x_relay_response'=> "TRUE",
126 1
            'x_relay_url'     => $relay_response_url,
127 1
            'x_login'         => $api_login_id,
128
            )
129
        );
130 1
        $hidden_fields = $sim->getHiddenFieldString();
131 1
        $post_url = ($test_mode ? self::SANDBOX_URL : self::LIVE_URL);
132
        
133
        $form = '
134
        <style>
135
        fieldset {
136
            overflow: auto;
137
            border: 0;
138
            margin: 0;
139
            padding: 0; }
140
141
        fieldset div {
142
            float: left; }
143
144
        fieldset.centered div {
145
            text-align: center; }
146
147
        label {
148
            color: #183b55;
149
            display: block;
150
            margin-bottom: 5px; }
151
152
        label img {
153
            display: block;
154
            margin-bottom: 5px; }
155
156
        input.text {
157
            border: 1px solid #bfbab4;
158
            margin: 0 4px 8px 0;
159
            padding: 6px;
160
            color: #1e1e1e;
161
            -webkit-border-radius: 5px;
162
            -moz-border-radius: 5px;
163
            border-radius: 5px;
164
            -webkit-box-shadow: inset 0px 5px 5px #eee;
165
            -moz-box-shadow: inset 0px 5px 5px #eee;
166
            box-shadow: inset 0px 5px 5px #eee; }
167
        .submit {
168
            display: block;
169
            background-color: #76b2d7;
170
            border: 1px solid #766056;
171
            color: #3a2014;
172
            margin: 13px 0;
173
            padding: 8px 16px;
174
            -webkit-border-radius: 12px;
175
            -moz-border-radius: 12px;
176
            border-radius: 12px;
177
            font-size: 14px;
178
            -webkit-box-shadow: inset 3px -3px 3px rgba(0,0,0,.5), inset 0 3px 3px rgba(255,255,255,.5), inset -3px 0 3px rgba(255,255,255,.75);
179
            -moz-box-shadow: inset 3px -3px 3px rgba(0,0,0,.5), inset 0 3px 3px rgba(255,255,255,.5), inset -3px 0 3px rgba(255,255,255,.75);
180
            box-shadow: inset 3px -3px 3px rgba(0,0,0,.5), inset 0 3px 3px rgba(255,255,255,.5), inset -3px 0 3px rgba(255,255,255,.75); }
181
        </style>
182 1
        <form method="post" action="'.$post_url.'">
183 1
                '.$hidden_fields.'
184
            <fieldset>
185
                <div>
186
                    <label>Credit Card Number</label>
187 1
                    <input type="text" class="text" size="15" name="x_card_num" value="'.($prefill ? '6011000000000012' : '').'"></input>
188
                </div>
189
                <div>
190
                    <label>Exp.</label>
191 1
                    <input type="text" class="text" size="4" name="x_exp_date" value="'.($prefill ? '04/17' : '').'"></input>
192
                </div>
193
                <div>
194
                    <label>CCV</label>
195 1
                    <input type="text" class="text" size="4" name="x_card_code" value="'.($prefill ? '782' : '').'"></input>
196
                </div>
197
            </fieldset>
198
            <fieldset>
199
                <div>
200
                    <label>First Name</label>
201 1
                    <input type="text" class="text" size="15" name="x_first_name" value="'.($prefill ? 'John' : '').'"></input>
202
                </div>
203
                <div>
204
                    <label>Last Name</label>
205 1
                    <input type="text" class="text" size="14" name="x_last_name" value="'.($prefill ? 'Doe' : '').'"></input>
206
                </div>
207
            </fieldset>
208
            <fieldset>
209
                <div>
210
                    <label>Address</label>
211 1
                    <input type="text" class="text" size="26" name="x_address" value="'.($prefill ? '123 Main Street' : '').'"></input>
212
                </div>
213
                <div>
214
                    <label>City</label>
215 1
                    <input type="text" class="text" size="15" name="x_city" value="'.($prefill ? 'Boston' : '').'"></input>
216
                </div>
217
            </fieldset>
218
            <fieldset>
219
                <div>
220
                    <label>State</label>
221 1
                    <input type="text" class="text" size="4" name="x_state" value="'.($prefill ? 'MA' : '').'"></input>
222
                </div>
223
                <div>
224
                    <label>Zip Code</label>
225 1
                    <input type="text" class="text" size="9" name="x_zip" value="'.($prefill ? '02142' : '').'"></input>
226
                </div>
227
                <div>
228
                    <label>Country</label>
229 1
                    <input type="text" class="text" size="22" name="x_country" value="'.($prefill ? 'US' : '').'"></input>
230
                </div>
231
            </fieldset>
232
            <input type="submit" value="BUY" class="submit buy">
233 1
        </form>';
234 1
        return $form;
235
    }
236
237
}