1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Authorizenet_Model_Directpost_Request extends Varien_Object
35: {
36: protected $_transKey = null;
37:
38: 39: 40: 41: 42: 43:
44: protected function _getTransactionKey()
45: {
46: return $this->_transKey;
47: }
48:
49: 50: 51: 52: 53: 54: 55:
56: protected function _setTransactionKey($transKey)
57: {
58: $this->_transKey = $transKey;
59: return $this;
60: }
61:
62: 63: 64: 65: 66: 67: 68: 69: 70: 71:
72: public function generateRequestSign($merchantApiLoginId, $merchantTransactionKey, $amount, $currencyCode, $fpSequence, $fpTimestamp)
73: {
74: if (phpversion() >= '5.1.2') {
75: return hash_hmac("md5",
76: $merchantApiLoginId . "^" .
77: $fpSequence . "^" .
78: $fpTimestamp . "^" .
79: $amount . "^" .
80: $currencyCode, $merchantTransactionKey
81: );
82: }
83:
84: return bin2hex(mhash(MHASH_MD5,
85: $merchantApiLoginId . "^" .
86: $fpSequence . "^" .
87: $fpTimestamp . "^" .
88: $amount . "^" .
89: $currencyCode, $merchantTransactionKey
90: ));
91: }
92:
93: 94: 95: 96: 97: 98:
99: public function setConstantData(Mage_Authorizenet_Model_Directpost $paymentMethod)
100: {
101: $this->setXVersion('3.1')
102: ->setXDelimData('FALSE')
103: ->setXRelayResponse('TRUE');
104:
105: $this->setXTestRequest($paymentMethod->getConfigData('test') ? 'TRUE' : 'FALSE');
106:
107: $this->setXLogin($paymentMethod->getConfigData('login'))
108: ->setXType('AUTH_ONLY')
109: ->setXMethod(Mage_Paygate_Model_Authorizenet::REQUEST_METHOD_CC)
110: ->setXRelayUrl($paymentMethod->getRelayUrl());
111:
112: $this->_setTransactionKey($paymentMethod->getConfigData('trans_key'));
113: return $this;
114: }
115:
116: 117: 118: 119: 120: 121: 122:
123: public function setDataFromOrder(Mage_Sales_Model_Order $order, Mage_Authorizenet_Model_Directpost $paymentMethod)
124: {
125: $payment = $order->getPayment();
126:
127: $this->setXFpSequence($order->getQuoteId());
128: $this->setXInvoiceNum($order->getIncrementId());
129: $this->setXAmount($payment->getBaseAmountAuthorized());
130: $this->setXCurrencyCode($order->getBaseCurrencyCode());
131: $this->setXTax(sprintf('%.2F', $order->getBaseTaxAmount()))
132: ->setXFreight(sprintf('%.2F', $order->getBaseShippingAmount()));
133:
134:
135: $billing = $order->getBillingAddress();
136: if (!empty($billing)) {
137: $this->setXFirstName(strval($billing->getFirstname()))
138: ->setXLastName(strval($billing->getLastname()))
139: ->setXCompany(strval($billing->getCompany()))
140: ->setXAddress(strval($billing->getStreet(1)))
141: ->setXCity(strval($billing->getCity()))
142: ->setXState(strval($billing->getRegion()))
143: ->setXZip(strval($billing->getPostcode()))
144: ->setXCountry(strval($billing->getCountry()))
145: ->setXPhone(strval($billing->getTelephone()))
146: ->setXFax(strval($billing->getFax()))
147: ->setXCustId(strval($billing->getCustomerId()))
148: ->setXCustomerIp(strval($order->getRemoteIp()))
149: ->setXCustomerTaxId(strval($billing->getTaxId()))
150: ->setXEmail(strval($order->getCustomerEmail()))
151: ->setXEmailCustomer(strval($paymentMethod->getConfigData('email_customer')))
152: ->setXMerchantEmail(strval($paymentMethod->getConfigData('merchant_email')));
153: }
154:
155: $shipping = $order->getShippingAddress();
156: if (!empty($shipping)) {
157: $this->setXShipToFirstName(strval($shipping->getFirstname()))
158: ->setXShipToLastName(strval($shipping->getLastname()))
159: ->setXShipToCompany(strval($shipping->getCompany()))
160: ->setXShipToAddress(strval($shipping->getStreet(1)))
161: ->setXShipToCity(strval($shipping->getCity()))
162: ->setXShipToState(strval($shipping->getRegion()))
163: ->setXShipToZip(strval($shipping->getPostcode()))
164: ->setXShipToCountry(strval($shipping->getCountry()));
165: }
166:
167: $this->setXPoNum(strval($payment->getPoNumber()));
168:
169: return $this;
170: }
171:
172: 173: 174: 175: 176: 177:
178: public function signRequestData()
179: {
180: $fpTimestamp = time();
181: $hash = $this->generateRequestSign(
182: $this->getXLogin(),
183: $this->_getTransactionKey(),
184: $this->getXAmount(),
185: $this->getXCurrencyCode(),
186: $this->getXFpSequence(),
187: $fpTimestamp
188: );
189: $this->setXFpTimestamp($fpTimestamp);
190: $this->setXFpHash($hash);
191: return $this;
192: }
193: }
194: