PurchaseRequest::getData()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 67
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6.9683

Importance

Changes 5
Bugs 1 Features 1
Metric Value
cc 5
eloc 44
c 5
b 1
f 1
nc 5
nop 0
dl 0
loc 67
ccs 24
cts 42
cp 0.5714
crap 6.9683
rs 8.9048

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
 * Moneris Purchase Request.
4
 */
5
6
namespace Omnipay\Moneris\Message;
7
8
use Omnipay\Common\Exception\InvalidRequestException;
9
10
/**
11
 * Moneris Purchase Request class.
12
 *
13
 * Moneris provides various payment related operations based on the data
14
 * submitted to their API. Use purchase for direct credit card payments
15
 * and customer profile payments.
16
 *
17
 * ### Example
18
 *
19
 * #### Initialize Gateway
20
 *
21
 * <code>
22
 *   // Create a gateway for the Moneris Gateway
23
 *   // (routes to GatewayFactory::create)
24
 *   $gateway = Omnipay::create('Moneris');
25
 *
26
 *   // Initialise the gateway
27
 *   $gateway->initialize(array(
28
 *       'merchantId'       => 'MyMonerisStoreId',
29
 *       'merchantSecret'   => 'MyMonerisAPIToken',
30
 *       'cryptType'        => 7,
31
 *       'testMode'         => true, // Or false when you are ready for live transactions
32
 *   ));
33
 * </code>
34
 *
35
 * #### Direct Credit Card Payment
36
 *
37
 * This is for the use case where a customer has presented their
38
 * credit card details and you intend to use the Moneris gateway
39
 * for processing a transaction using that credit card data.
40
 *
41
 * <code>
42
 *   // Create a credit card object
43
 *   // DO NOT USE THESE CARD VALUES -- substitute your own
44
 *   // see the documentation in the class header.
45
 *   $card = new CreditCard(array(
46
 *       'firstName'            => 'Example',
47
 *       'lastName'             => 'User',
48
 *       'number'               => '4111111111111111',
49
 *       'expiryMonth'          => '01',
50
 *       'expiryYear'           => '2020',
51
 *       'cvv'                  => '123',
52
 *       'billingAddress1'      => '1 Scrubby Creek Road',
53
 *       'billingCountry'       => 'AU',
54
 *       'billingCity'          => 'Scrubby Creek',
55
 *       'billingPostcode'      => '4999',
56
 *       'billingState'         => 'QLD',
57
 *   ));
58
 *
59
 *   // Do a purchase transaction on the gateway
60
 *   try {
61
 *       $transaction = $gateway->purchase(array(
62
 *           'orderNumber'   => 'XXXX-XXXX',
63
 *           'amount'        => '10.00',
64
 *           'description'   => 'This is a test purchase transaction.',
65
 *           'card'          => $card,
66
 *       ));
67
 *       $response = $transaction->send();
68
 *       $data = $response->getData();
69
 *       echo "Gateway purchase response data == " . print_r($data, true) . "\n";
70
 *
71
 *       if ($response->isSuccessful()) {
72
 *           echo "Purchase transaction was successful!\n";
73
 *       }
74
 *   } catch (\Exception $e) {
75
 *       echo "Exception caught while attempting authorize.\n";
76
 *       echo "Exception type == " . get_class($e) . "\n";
77
 *       echo "Message == " . $e->getMessage() . "\n";
78
 *   }
79
 * </code>
80
 *
81
 * @link https://developer.moneris.com/en/Documentation/NA/E-Commerce%20Solutions/API/Purchase
82
 */
83
class PurchaseRequest extends AbstractRequest
84
{
85
    /**
86
     * CVD value is deliberately bypassed or is not provided by the merchant.
87
     *
88
     * @var int
89
     * @link https://developer.moneris.com/en/Documentation/NA/E-Commerce%20Solutions/API/Purchase
90
     */
91
    const CVD_BYPASSED = 0;
92
93
    /**
94
     * CVD value is present.
95
     *
96
     * @var int
97
     * @link https://developer.moneris.com/en/Documentation/NA/E-Commerce%20Solutions/API/Purchase
98
     */
99
    const CVD_PRESENT = 1;
100
101
    /**
102
     * CVD value is on the card, but is illegible.
103
     *
104
     * @var int
105
     * @link https://developer.moneris.com/en/Documentation/NA/E-Commerce%20Solutions/API/Purchase
106
     */
107
    const CVD_ILLEGIBLE = 2;
108
109
    /**
110
     * Cardholder states that the card has no CVD imprint.
111
     *
112
     * @var int
113
     * @link https://developer.moneris.com/en/Documentation/NA/E-Commerce%20Solutions/API/Purchase
114
     */
115
    const NO_CVD = 9;
116
117 42
    public function getData()
118
    {
119 42
        $data = null;
120
121 42
        $this->validate('paymentMethod', 'orderNumber', 'cryptType', 'amount', 'description');
122
123 21
        $paymentMethod = $this->getPaymentMethod();
124
125
        switch ($paymentMethod) {
126 21
            case 'payment_profile':
127 12
                $this->validate('cardReference');
128
129 9
                $request = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><request></request>');
130 9
                $request->addChild('store_id', $this->getMerchantId());
131 9
                $request->addChild('api_token', $this->getMerchantKey());
132
133 9
                $res_purchase_cc = $request->addChild('res_purchase_cc');
134 9
                $res_purchase_cc->addChild('data_key', $this->getCardReference());
135 9
                $res_purchase_cc->addChild('order_id', $this->getOrderNumber());
136 9
                $res_purchase_cc->addChild('cust_id', 'Transaction_'.$this->getOrderNumber());
137 9
                $res_purchase_cc->addChild('amount', $this->getAmount());
138 6
                $res_purchase_cc->addChild('crypt_type', $this->getCryptType());
139
140 6
                if ($this->getDescription()) {
141
                    $res_purchase_cc->addChild('dynamic_descriptor', $this->getDescription());
142
                }
143
144 6
                $data = $request->asXML();
145 6
                break;
146
147 9
            case 'card':
148 6
                $this->validate('card');
149
150 3
                $card = $this->getCard();
151 3
                $card->validate();
152
153
                $request = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><request></request>');
154
                $request->addChild('store_id', $this->getMerchantId());
155
                $request->addChild('api_token', $this->getMerchantKey());
156
157
                $purchase = $request->addChild('purchase');
158
                $purchase->addChild('pan', $card->getNumber());
159
                $purchase->addChild('expdate', $card->getExpiryDate('ym'));
160
                $purchase->addChild('order_id', $this->getOrderNumber());
161
                $purchase->addChild('cust_id', 'Transaction_'.$this->getOrderNumber());
162
                $purchase->addChild('amount', $this->getAmount());
163
                $purchase->addChild('crypt_type', $this->getCryptType());
164
165
                if ($this->getDescription()) {
166
                    $purchase->addChild('dynamic_descriptor', $this->getDescription());
167
                }
168
169
                $cvd_info = $purchase->addChild('cvd_info');
170
                $cvd_info->addChild('cvd_indicator', self::CVD_PRESENT);
171
                $cvd_info->addChild('cvd_value', $card->getCvv());
172
173
                $data = $request->asXML();
174
                break;
175
176
            // Todo: token payment
177
178
            default:
179 3
                throw new InvalidRequestException('Invalid payment method');
180
                break;
181
        }
182
183 6
        return preg_replace('/\n/', '', $data);
184
    }
185
}
186