|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var ApiContracts = require('authorizenet').APIContracts; |
| 4 | +var ApiControllers = require('authorizenet').APIControllers; |
| 5 | +var utils = require('../utils.js'); |
| 6 | +var constants = require('../constants.js'); |
| 7 | + |
| 8 | +function createChasePayTransaction(callback) { |
| 9 | + var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType(); |
| 10 | + merchantAuthenticationType.setName(constants.apiLoginKey); |
| 11 | + merchantAuthenticationType.setTransactionKey(constants.transactionKey); |
| 12 | + |
| 13 | + var creditCard = new ApiContracts.CreditCardType(); |
| 14 | + creditCard.setCardNumber('4242424242424242'); |
| 15 | + creditCard.setExpirationDate('0822'); |
| 16 | + creditCard.setCardCode('999'); |
| 17 | + // Set the token specific info |
| 18 | + creditCard.setIsPaymentToken(true); |
| 19 | + creditCard.setCryptogram('EjRWeJASNFZ4kBI0VniQEjRWeJA='); |
| 20 | + creditCard.setTokenRequestorName('CHASE_PAY'); |
| 21 | + creditCard.setTokenRequestorId('12345678901'); |
| 22 | + creditCard.setTokenRequestorEci('07'); |
| 23 | + |
| 24 | + var paymentType = new ApiContracts.PaymentType(); |
| 25 | + paymentType.setCreditCard(creditCard); |
| 26 | + |
| 27 | + var orderDetails = new ApiContracts.OrderType(); |
| 28 | + orderDetails.setInvoiceNumber('INV-12345'); |
| 29 | + orderDetails.setDescription('Product Description'); |
| 30 | + |
| 31 | + var tax = new ApiContracts.ExtendedAmountType(); |
| 32 | + tax.setAmount('4.26'); |
| 33 | + tax.setName('level2 tax name'); |
| 34 | + tax.setDescription('level2 tax'); |
| 35 | + |
| 36 | + var duty = new ApiContracts.ExtendedAmountType(); |
| 37 | + duty.setAmount('8.55'); |
| 38 | + duty.setName('duty name'); |
| 39 | + duty.setDescription('duty description'); |
| 40 | + |
| 41 | + var shipping = new ApiContracts.ExtendedAmountType(); |
| 42 | + shipping.setAmount('8.55'); |
| 43 | + shipping.setName('shipping name'); |
| 44 | + shipping.setDescription('shipping description'); |
| 45 | + |
| 46 | + var billTo = new ApiContracts.CustomerAddressType(); |
| 47 | + billTo.setFirstName('Ellen'); |
| 48 | + billTo.setLastName('Johnson'); |
| 49 | + billTo.setCompany('Souveniropolis'); |
| 50 | + billTo.setAddress('14 Main Street'); |
| 51 | + billTo.setCity('Pecan Springs'); |
| 52 | + billTo.setState('TX'); |
| 53 | + billTo.setZip('44628'); |
| 54 | + billTo.setCountry('USA'); |
| 55 | + |
| 56 | + var shipTo = new ApiContracts.CustomerAddressType(); |
| 57 | + shipTo.setFirstName('China'); |
| 58 | + shipTo.setLastName('Bayles'); |
| 59 | + shipTo.setCompany('Thyme for Tea'); |
| 60 | + shipTo.setAddress('12 Main Street'); |
| 61 | + shipTo.setCity('Pecan Springs'); |
| 62 | + shipTo.setState('TX'); |
| 63 | + shipTo.setZip('44628'); |
| 64 | + shipTo.setCountry('USA'); |
| 65 | + |
| 66 | + var lineItem_id1 = new ApiContracts.LineItemType(); |
| 67 | + lineItem_id1.setItemId('1'); |
| 68 | + lineItem_id1.setName('vase'); |
| 69 | + lineItem_id1.setDescription('cannes logo'); |
| 70 | + lineItem_id1.setQuantity('18'); |
| 71 | + lineItem_id1.setUnitPrice(45.00); |
| 72 | + |
| 73 | + var lineItem_id2 = new ApiContracts.LineItemType(); |
| 74 | + lineItem_id2.setItemId('2'); |
| 75 | + lineItem_id2.setName('vase2'); |
| 76 | + lineItem_id2.setDescription('cannes logo2'); |
| 77 | + lineItem_id2.setQuantity('28'); |
| 78 | + lineItem_id2.setUnitPrice('25.00'); |
| 79 | + |
| 80 | + var lineItemList = []; |
| 81 | + lineItemList.push(lineItem_id1); |
| 82 | + lineItemList.push(lineItem_id2); |
| 83 | + |
| 84 | + var lineItems = new ApiContracts.ArrayOfLineItem(); |
| 85 | + lineItems.setLineItem(lineItemList); |
| 86 | + |
| 87 | + var userField_a = new ApiContracts.UserField(); |
| 88 | + userField_a.setName('A'); |
| 89 | + userField_a.setValue('Aval'); |
| 90 | + |
| 91 | + var userField_b = new ApiContracts.UserField(); |
| 92 | + userField_b.setName('B'); |
| 93 | + userField_b.setValue('Bval'); |
| 94 | + |
| 95 | + var userFieldList = []; |
| 96 | + userFieldList.push(userField_a); |
| 97 | + userFieldList.push(userField_b); |
| 98 | + |
| 99 | + var userFields = new ApiContracts.TransactionRequestType.UserFields(); |
| 100 | + userFields.setUserField(userFieldList); |
| 101 | + |
| 102 | + var transactionSetting1 = new ApiContracts.SettingType(); |
| 103 | + transactionSetting1.setSettingName('duplicateWindow'); |
| 104 | + transactionSetting1.setSettingValue('120'); |
| 105 | + |
| 106 | + var transactionSetting2 = new ApiContracts.SettingType(); |
| 107 | + transactionSetting2.setSettingName('recurringBilling'); |
| 108 | + transactionSetting2.setSettingValue('false'); |
| 109 | + |
| 110 | + var transactionSettingList = []; |
| 111 | + transactionSettingList.push(transactionSetting1); |
| 112 | + transactionSettingList.push(transactionSetting2); |
| 113 | + |
| 114 | + var transactionSettings = new ApiContracts.ArrayOfSetting(); |
| 115 | + transactionSettings.setSetting(transactionSettingList); |
| 116 | + |
| 117 | + var transactionRequestType = new ApiContracts.TransactionRequestType(); |
| 118 | + transactionRequestType.setTransactionType(ApiContracts.TransactionTypeEnum.AUTHCAPTURETRANSACTION); |
| 119 | + transactionRequestType.setPayment(paymentType); |
| 120 | + transactionRequestType.setAmount(utils.getRandomAmount()); |
| 121 | + transactionRequestType.setLineItems(lineItems); |
| 122 | + transactionRequestType.setUserFields(userFields); |
| 123 | + transactionRequestType.setOrder(orderDetails); |
| 124 | + transactionRequestType.setTax(tax); |
| 125 | + transactionRequestType.setDuty(duty); |
| 126 | + transactionRequestType.setShipping(shipping); |
| 127 | + transactionRequestType.setBillTo(billTo); |
| 128 | + transactionRequestType.setShipTo(shipTo); |
| 129 | + transactionRequestType.setTransactionSettings(transactionSettings); |
| 130 | + |
| 131 | + var createRequest = new ApiContracts.CreateTransactionRequest(); |
| 132 | + createRequest.setMerchantAuthentication(merchantAuthenticationType); |
| 133 | + createRequest.setTransactionRequest(transactionRequestType); |
| 134 | + |
| 135 | + //pretty print request |
| 136 | + console.log(JSON.stringify(createRequest.getJSON(), null, 2)); |
| 137 | + |
| 138 | + var ctrl = new ApiControllers.CreateTransactionController(createRequest.getJSON()); |
| 139 | + |
| 140 | + ctrl.execute(function(){ |
| 141 | + |
| 142 | + var apiResponse = ctrl.getResponse(); |
| 143 | + |
| 144 | + var response = new ApiContracts.CreateTransactionResponse(apiResponse); |
| 145 | + |
| 146 | + //pretty print response |
| 147 | + console.log(JSON.stringify(response, null, 2)); |
| 148 | + |
| 149 | + if(response != null){ |
| 150 | + if(response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK){ |
| 151 | + if(response.getTransactionResponse().getMessages() != null){ |
| 152 | + console.log('Successfully created transaction with Transaction ID: ' + response.getTransactionResponse().getTransId()); |
| 153 | + console.log('Response Code: ' + response.getTransactionResponse().getResponseCode()); |
| 154 | + console.log('Message Code: ' + response.getTransactionResponse().getMessages().getMessage()[0].getCode()); |
| 155 | + console.log('Description: ' + response.getTransactionResponse().getMessages().getMessage()[0].getDescription()); |
| 156 | + } |
| 157 | + else { |
| 158 | + console.log('Failed Transaction.'); |
| 159 | + if(response.getTransactionResponse().getErrors() != null){ |
| 160 | + console.log('Error Code: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorCode()); |
| 161 | + console.log('Error message: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorText()); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + else { |
| 166 | + console.log('Failed Transaction. '); |
| 167 | + if(response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null){ |
| 168 | + |
| 169 | + console.log('Error Code: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorCode()); |
| 170 | + console.log('Error message: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorText()); |
| 171 | + } |
| 172 | + else { |
| 173 | + console.log('Error Code: ' + response.getMessages().getMessage()[0].getCode()); |
| 174 | + console.log('Error message: ' + response.getMessages().getMessage()[0].getText()); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + else { |
| 179 | + console.log('Null Response.'); |
| 180 | + } |
| 181 | + |
| 182 | + callback(response); |
| 183 | + }); |
| 184 | +} |
| 185 | + |
| 186 | +if (require.main === module) { |
| 187 | + createChasePayTransaction(function(){ |
| 188 | + console.log('createchasepay call complete.'); |
| 189 | + }); |
| 190 | +} |
| 191 | + |
| 192 | +module.exports.createChasePayTransaction = createChasePayTransaction; |
0 commit comments