Passed
Push — master ( 6eac5c...db19ec )
by Unoapp
06:46 queued 12s
created

PurchaseRequest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 6
Bugs 1 Features 1
Metric Value
eloc 49
c 6
b 1
f 1
dl 0
loc 101
ccs 24
cts 42
cp 0.5714
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B getData() 0 67 5
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