Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed issue #29 and issue #74, I corrected the sample-code in recurri… #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 68 additions & 50 deletions RecurringBilling/update-subscription.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,80 @@
/* eslint-disable no-console */
'use strict';

var ApiContracts = require('authorizenet').APIContracts;
var ApiControllers = require('authorizenet').APIControllers;
var utils = require('../utils.js');
var constants = require('../constants.js');
var constants = require("../constants.js");

function updateSubscription(subscriptionId, callback) {
var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType();
merchantAuthenticationType.setName(constants.apiLoginKey);
merchantAuthenticationType.setTransactionKey(constants.transactionKey);

var orderType = new ApiContracts.OrderType();
orderType.setInvoiceNumber(utils.getRandomString('Inv:'));
orderType.setDescription(utils.getRandomString('Description'));

var arbSubscriptionType = new ApiContracts.ARBSubscriptionType();
arbSubscriptionType.setOrder(orderType);

var updateRequest = new ApiContracts.ARBUpdateSubscriptionRequest();
updateRequest.setMerchantAuthentication(merchantAuthenticationType);
updateRequest.setSubscriptionId(subscriptionId);
updateRequest.setSubscription(arbSubscriptionType);

console.log(JSON.stringify(updateRequest.getJSON(), null, 2));

var ctrl = new ApiControllers.ARBUpdateSubscriptionController(updateRequest.getJSON());

ctrl.execute(function(){

var apiResponse = ctrl.getResponse();

var response = new ApiContracts.ARBUpdateSubscriptionResponse(apiResponse);

console.log(JSON.stringify(response, null, 2));

if(response != null){
if(response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK){
console.log('Message Code : ' + response.getMessages().getMessage()[0].getCode());
console.log('Message Text : ' + response.getMessages().getMessage()[0].getText());
}
else{
console.log('Result Code: ' + response.getMessages().getResultCode());
console.log('Error Code: ' + response.getMessages().getMessage()[0].getCode());
console.log('Error message: ' + response.getMessages().getMessage()[0].getText());
}
}
else{
console.log('Null Response.');
}

callback(response);
});
var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType();
merchantAuthenticationType.setName(constants.apiLoginKey);
merchantAuthenticationType.setTransactionKey(constants.transactionKey);

var updatedCreditCardInfo = new ApiContracts.CreditCardType();
updatedCreditCardInfo.setCardNumber('4111111111111111');
updatedCreditCardInfo.setExpirationDate('2038-12');

var payment = new ApiContracts.PaymentType();
payment.setCreditCard(updatedCreditCardInfo);

var orderType = new ApiContracts.OrderType();
orderType.setInvoiceNumber(utils.getRandomString('Inv:'));
orderType.setDescription(utils.getRandomString('Description'));

var arbSubscriptionType = new ApiContracts.ARBSubscriptionType();
arbSubscriptionType.setOrder(orderType);
arbSubscriptionType.setPayment(payment);

var updateRequest = new ApiContracts.ARBUpdateSubscriptionRequest();
updateRequest.setMerchantAuthentication(merchantAuthenticationType);
updateRequest.setSubscriptionId(subscriptionId);
updateRequest.setSubscription(arbSubscriptionType);

console.log(JSON.stringify(updateRequest.getJSON(), null, 2));

var ctrl = new ApiControllers.ARBUpdateSubscriptionController(
updateRequest.getJSON()
);

ctrl.execute(function () {
var apiResponse = ctrl.getResponse();

var response = new ApiContracts.ARBUpdateSubscriptionResponse(apiResponse);

console.log(JSON.stringify(response, null, 2));

if (response != null) {
if (
response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK
) {
console.log(
'Message Code : ' + response.getMessages().getMessage()[0].getCode()
);
console.log(
'Message Text : ' + response.getMessages().getMessage()[0].getText()
);
} else {
console.log('Result Code: ' + response.getMessages().getResultCode());
console.log(
'Error Code: ' + response.getMessages().getMessage()[0].getCode()
);
console.log(
'Error message: ' + response.getMessages().getMessage()[0].getText()
);
}
} else {
console.log('Null Response.');
}

callback(response);
});
}

if (require.main === module) {
updateSubscription('4058648', function(){
console.log('getSubscription call complete.');
});
updateSubscription('4058648', function () {
console.log('getSubscription call complete.');
});
}

module.exports.updateSubscription = updateSubscription;
module.exports.updateSubscription = updateSubscription;