How to preview upcoming invoices using the Zuora SOAP API? - zuora

From the Zuora user interface I can preview a subscription using a convenient "Preview" button. I'd like to perform the same action using the Zuora SOAP API so that I can preview what the upcoming invoices and write some tests.
So far, my theory is to create an empty Amendment with PreviewOptions.enablePreviewMode=true to get the results of the previewed invoice.
Relevant SOAP docs:
Amend docs
InvoiceData docs
This has partially worked, but it has returned one InvoiceData object, but two would be expected.
public List<InvoiceData> getInvoicePreview(String subscriptionId){
ID id = new ID();
id.setID(subscriptionId);
PreviewOptions previewOptions = new PreviewOptions();
previewOptions.setEnablePreviewMode(true);
previewOptions.setPreviewThroughTermEnd(true);
Calendar forever = new DateTime().plusDays(1000).toCalendar(Locale.getDefault());
// A null amendment
Amendment amendment = new Amendment();
amendment.setName("Draft amendment for preview");
amendment.setStatus("Draft");
amendment.setContractEffectiveDate(forever);
amendment.setSubscriptionId(id);
amendment.setType("TermsAndConditions");
AmendOptions amendOptions = new AmendOptions();
AmendRequest amendRequest = new AmendRequest();
amendRequest.setAmendments(new Amendment[]{amendment});
amendRequest.setAmendOptions(amendOptions);
amendRequest.setPreviewOptions(previewOptions);
Amend amend = new Amend();
amend.setRequests(new AmendRequest[]{amendRequest});
AmendResponse amendResults;
try {
ZuoraCredentials creds = credentialProvider.get();
ZuoraServiceStub stub = new ZuoraServiceStub(creds.zuoraApiEndpoint);
Login login = new Login();
login.setUsername(creds.zuoraApiUser);
login.setPassword(creds.zuoraApiPassword);
LoginResponse loginResponse = stub.login(login);
String session = loginResponse.getResult().getSession();
SessionHeader sessionHeader = new SessionHeader();
sessionHeader.setSession(session);
amendResults = stub.amend(amend, sessionHeader);
} catch (RemoteException e) {
throw new RuntimeException("Error executing Zuora API.", e);
} catch (UnexpectedErrorFault e) {
throw new RuntimeException("Error executing Zuora API.", e);
} catch (LoginFault e) {
throw new RuntimeException("Error executing Zuora API.", e);
}
AmendResult[] amendResult = amendResults.getResults();
printOut(amendResult);
return new ArrayList<InvoiceData>(Arrays.asList(amendResult[0].getInvoiceDatas()));
}
Here's what the data looks like from the Zuora UI

I have been trying to achieve something similar myself for a subscribe() call and ended up raising a ticket with Zuora. They said that it was not supported but you could achieve the same thing by aggregating the InvoiceItem records.
What I have done is manually group them together based on the start of the service period (ServiceStartDate) and sum the totals (e.g. ChargeAmount, TaxAmount). It appears to work out correctly and you can get the total due at the start of each period.
I suspect this will work for amendments in the same way because an InvoiceData record is returned in the same was as for subscribe().
Update
We do now use this for amendments and it does work well in the same way as for subscribe calls.
One problem we have found is that you cannot preview beyond the end of a termed subscription, but we can set the term type to EVERGREEN if needed when previewing (even if the subscription is to be TERMED when created).

Related

GMail OAUTH not asking for permission when published

I've started using the GMail API and it's working fine on my local machine; it will open the Google permissions page and I can select my account. It then stores the return json token and only asks again if this token is removed.
When I publish to the server, the OAUTH page is never displayed and the application appears to timeout with a 'Thread was being aborted' exception.
My code;
try
{
using (var stream = new FileStream(HttpContext.Current.Server.MapPath("~/credentials/client_id.json"), FileMode.Open, FileAccess.Read))
{
string credPath = HttpContext.Current.Server.MapPath("~/credentials/gmail_readonly_token.json");
_credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
db.writeLog("INFO", "Gmail Credentials Saved","Credential file saved to: " + credPath);
}
// Create Gmail API service.
service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = _credential,
});
}
catch (Exception e)
{
db.writeLog("Error", "Failure when creating Gmail class", e.Message, null, _username, null);
}
Is there something I need to change within the 'client_id.json' (formally client_secret.json) file? The only thing I have altered is the redirect_uris line.
Any other suggestions would be welcome, the only other question I could find that is similar is here but there is no answer.
Thanks,
Danny.
The first one worked because you followed the intended use case, which is client-side. But, to implement authorization on the server, follow the Implementing Server-Side AUthorization guide.

Geolocator.RequestAccessAsync throws "method called at unexpected time" exception

I have a chunk of code I use to get device location. This is done in a monogame windows uwp project. The following code worked before (I was on VS2015 Community). I recent did a fresh OS and install VS2017 Community. I finally got my project to build and run. The only thing not working is the Geolocator.RequestAccessAsync is throwing a "method called at unexpected time" exception. Any ideas?
public async Task<Vector2> GetDeviceGps()
{
var accessStatus = await Geolocator.RequestAccessAsync();
switch (accessStatus)
{
case GeolocationAccessStatus.Allowed:
// If DesiredAccuracy or DesiredAccuracyInMeters are not set (or value is 0), DesiredAccuracy.Default is used.
Geolocator geolocator = new Geolocator { DesiredAccuracyInMeters = 10 };
// Subscribe to the StatusChanged event to get updates of location status changes.
//geolocator.StatusChanged += OnStatusChanged;
// Carry out the operation.
Geoposition pos = await geolocator.GetGeopositionAsync();
return new Vector2((float)pos.Coordinate.Point.Position.Longitude, (float)pos.Coordinate.Point.Position.Latitude);
case GeolocationAccessStatus.Denied:
//Do something!
break;
case GeolocationAccessStatus.Unspecified:
//Murp
break;
}
return Vector2.Zero;
}
This is invoked and handled like so (called from update method of game):
mapper.GetDeviceGps().ContinueWith(pos =>
{
Vector2 bob = pos.Result;
});
When you run the RequestAccessAsync for the first time in your app, the user will be asked to grant your app the location permission. Therefore the method needs to be executed in the UI thread.

Deleted files status unreliably reported in the new Google Drive Android API (GDAA)

This issue has been bugging me since the inception of the new Google Drive Android Api (GDAA).
First discussed here, I hoped it would go away in later releases, but it is still there (as of 2014/03/19). The user-trashed (referring to the 'Remove' action in 'drive.google.com') files/folders keep appearing in both the
Drive.DriveApi.query(_gac, query), and
DriveFolder.queryChildren(_gac, query)
as well as
DriveFolder.listChildren(_gac)
methods, even if used with
Filters.eq(SearchableField.TRASHED, false)
query qualifier, or if I use a filtering construct on the results
for (Metadata md : result.getMetadataBuffer()) {
if ((md == null) || (!md.isDataValid()) || md.isTrashed()) continue;
dMDs.add(new DrvMD(md));
}
Using
Drive.DriveApi.requestSync(_gac);
has no impact. And the time elapsed since the removal varies wildly, my last case was over 12 HOURS. And it is completely random.
What's worse, I can't even rely on EMPTY TRASH in 'drive.google.com', it does not yield any predictable results. Sometime the file status changes to 'isTrashed()' sometimes it disappears from the result list.
As I kept fiddling with this issue, I ended up with the following superawfulhack:
find file with TRASH status equal FALSE
if (file found and is not trashed) {
try to write content
if ( write content fails)
create a new file
}
Not even this helps. The file shows up as healthy even if the file is in the trash (and it's status was double-filtered by query and by metadata test). It can even be happily written into and when inspected in the trash, it is modified.
The conclusion here is that a fix should get higher priority, since it renders multi-platform use of Drive unreliable. It will be discovered by developers right away in the development / debugging process, steering them away.
While waiting for any acknowledgement from the support team, I devised a HACK that allows a workaround for this problem. Using the same principle as in SO 22295903, the logic involves falling back to RESTful API. Basically, dropping the LIST / QUERY functionality of GDAA.
The high level logic is:
query the RESTful API to retrieve the ID/IDs of file(s) in question
use retrieved ID to get GDAA's DriveId via 'fetchDriveId()'
here are the code snippets to document the process:
1/ initialize both GDAA's 'GoogleApiClient' and RESTful's 'services.drive.Drive'
GoogleApiClient _gac;
com.google.api.services.drive.Drive _drvSvc;
void init(Context ctx, String email){
// build GDAA GoogleApiClient
_gac = new GoogleApiClient.Builder(ctx).addApi(com.google.android.gms.drive.Drive.API)
.addScope(com.google.android.gms.drive.Drive.SCOPE_FILE).setAccountName(email)
.addConnectionCallbacks(ctx).addOnConnectionFailedListener(ctx).build();
// build RESTFul (DriveSDKv2) service to fall back to
GoogleAccountCredential crd = GoogleAccountCredential
.usingOAuth2(ctx, Arrays.asList(com.google.api.services.drive.DriveScopes.DRIVE_FILE));
crd.setSelectedAccountName(email);
_drvSvc = new com.google.api.services.drive.Drive.Builder(
AndroidHttp.newCompatibleTransport(), new GsonFactory(), crd).build();
}
2/ method that queries the Drive RESTful API, returning GDAA's DriveId to be used by the app.
String qry = "title = 'MYFILE' and mimeType = 'text/plain' and trashed = false";
DriveId findObject(String qry) throws Exception {
DriveId dId = null;
try {
final FileList gLst = _drvSvc.files().list().setQ(query).setFields("items(id)").execute();
if (gLst.getItems().size() == 1) {
String sId = gLst.getItems().get(0).getId();
dId = Drive.DriveApi.fetchDriveId(_gac, sId).await().getDriveId();
} else if (gLst.getItems().size() > 1)
throw new Exception("more then one folder/file found");
} catch (Exception e) {}
return dId;
}
The findObject() method above (again I'm using the 'await()' flavor for simplicity) returns the the Drive objects correctly, reflecting the trashed status with no noticeable delay (implement in non-UI thread).
Again, I would strongly advice AGAINST leaving this in code longer than necassary since it is a HACK with unpredictable effect on the rest of the system.

Windows phone 8 notification hub unregister

Can someone show me or tell some example how to unregister from notification hub in windows phone 8. I tried on this way but it doesn't work.
public void registerForNotifications(string[] tags)
{
var channel = HttpNotificationChannel.Find("xxx");
if (channel == null)
{
channel = new HttpNotificationChannel("xxx");
channel.Open();
channel.BindToShellToast();
}
string[] tagsToSubscribeTo = tags;
channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(async (o, args) =>
{
var hub = new NotificationHub("xxx", "xxx");
await hub.RegisterNativeAsync(args.ChannelUri.ToString(), tagsToSubscribeTo);
});
}
public async void unregisterFromNotifications()
{
var channel = HttpNotificationChannel.Find("xxx");
var hub = new NotificationHub("xxx", "xxx");
await hub.UnregisterAllAsync(channel.ChannelUri.ToString());
}
You didn't say what "it didn't work" means. Did you get an error message? Did it report success but actually fail? In your questions, it really helps more if you share those things. But I'll take a stab at this anyway.
I suspect that you might be using the DefaultListenSharedAccessSignature endpoint from your Windows Phone 8 app.
According to http://msdn.microsoft.com/en-us/library/dn495373.aspx, the Listen access level grants permission to:
Create/Update registration.
Read registration.
Read all registrations for a handle.
Delete registration.
Reading that last one, I wonder if the UnregisterAllAsync method might require a higher access level to delete all registrations, rather than just one.
But rather than use the DefaultFullSharedAccessSignature endpoint, I would rather just try the UnregisterAsync method instead of UnregisterAllAsync.
Disclaimer: I have not tried this out. It may not help at all.

Linq to SQL TransactionScope

I have the following scenario:
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted },EnterpriseServicesInteropOption.Automatic))
{
using (DataContext db = new DataContext())
{
db.Connection.Open();
db.Transaction = db.Connection.BeginTransaction();
try
{
bool outcome = InvokeInTransaction<string, object>(inputDict, out outputDict);
db.Transaction.Commit();
}
catch (Exception ex)
{
response.Outcome = BusinessEntityResponse.SystemError;
db.Transaction.Rollback();
}
}
}
Inside the InvokeInTransaction call are a number of calls made to a LTS repository to perform various data changes. The problem is that inside the repository there is another
using (var db = new DataContext())
Inside which is the persistence code. Inspecting the context in the repository shows Transaction = null and I suspect that the "inner" context has no knowledge of the Ambient transaction. Can this be done? I understand that EF manages this under the covers, and the constraint is that the repository code cannot be changed. Any help?
We use LinqToSql and TransactionScope for multiple database transactions. You should really control your connection/context/transaction lifecycles if you're going to attempt it.
We control DataContext instances by the rule: If you new one up, you do that with a using statement.
We control connection lifecycles by the rule: If you open it, you must close it (but generally let the DataContext instances manage that).
We control transaction lifecycles by the rule: let DataContext manage what goes on in SubmitChanges and let TransactionScope manage what goes on within its using block.
Here's a code sample:
using (OuterDataContext outerDataContext = GetOuterDataContext())
{
using (InnerDataContext innerDataContext = GetInnerDataContext())
{
try
{
OuterRepository outerRepository = new OuterRepository();
// may read records into memory for updating/deleting.
outerRepository.WorkWithOuterRecords(outerRecords, outerDataContext);
InnerRepository innerRepository = new InnerRepository();
// may read records into memory for updating/deleting.
innerRepository.WorkWithInnerRecords(innerRecords, innerDataContext);
using (TransactionScope scope = new TransactionScope())
{
//starts a local tranaction in outerDB, held open by scope
outerDataContext.SubmitChanges();
//promotes the transaction to distributed, still held open by scope
innerDataContext.SubmitChanges();
// and done
scope.Complete();
}
}
catch (Exception ex)
{
LoggerClient.Log(ex);
response.Message = "It didn't save anything.";
}
}
}