Skip to content
This repository has been archived by the owner on Jul 9, 2022. It is now read-only.

Commit

Permalink
In-App Purchase operations
Browse files Browse the repository at this point in the history
  • Loading branch information
habuma committed Mar 28, 2012
1 parent 0495039 commit 7fb32b1
Show file tree
Hide file tree
Showing 21 changed files with 697 additions and 0 deletions.
@@ -0,0 +1,44 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mobile.urbanairship;

public class ContentReceipt {

private final String transactionReceipt;

private final String udid;

private final String version;

public String getTransactionReceipt() {
return transactionReceipt;
}

public String getUdid() {
return udid;
}

public String getVersion() {
return version;
}

public ContentReceipt(String transactionReceipt, String udid, String version) {
this.transactionReceipt = transactionReceipt;
this.udid = udid;
this.version = version;
}

}
@@ -0,0 +1,30 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mobile.urbanairship;

import java.util.List;

public interface InAppPurchaseOperations {

List<InventoryItem> getInventory();

String getDownloadUrl(String productId);

String getDownloadUrl(String productId, ContentReceipt receipt);

List<Product> checkForUpdates(List<Product> products);

}
@@ -0,0 +1,38 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mobile.urbanairship;

@SuppressWarnings("serial")
public class InvalidContentReceiptException extends RuntimeException {

private final Integer receiptStatus;

private final String serverResponse;

public InvalidContentReceiptException(Integer receiptStatus, String serverResponse) {
this.receiptStatus = receiptStatus;
this.serverResponse = serverResponse;
}

public Integer getReceiptStatus() {
return receiptStatus;
}

public String getServerResponse() {
return serverResponse;
}

}
@@ -0,0 +1,93 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mobile.urbanairship;

public class InventoryItem {

private final String name;

private final String url;

private final String iconUrl;

private final String previewUrl;

private final String downloadUrl;

private final String currentRevision;

private final String productId;

private final boolean free;

private final int fileSize;

private final String description;

private InventoryItem(String name, String url, String iconUrl, String previewUrl, String downloadUrl, String currentRevision, String productId, boolean free, int fileSize, String description) {
this.name = name;
this.url = url;
this.iconUrl = iconUrl;
this.previewUrl = previewUrl;
this.downloadUrl = downloadUrl;
this.currentRevision = currentRevision;
this.productId = productId;
this.free = free;
this.fileSize = fileSize;
this.description = description;
}

public String getName() {
return name;
}

public String getUrl() {
return url;
}

public String getIconUrl() {
return iconUrl;
}

public String getPreviewUrl() {
return previewUrl;
}

public String getDownloadUrl() {
return downloadUrl;
}

public String getCurrentRevision() {
return currentRevision;
}

public String getProductId() {
return productId;
}

public boolean isFree() {
return free;
}

public int getFileSize() {
return fileSize;
}

public String getDescription() {
return description;
}

}
@@ -0,0 +1,43 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mobile.urbanairship;

public class Product {

private final String productId;

private final int currentRevision;

private String downloadUrl;

public Product(String productId, int currentRevision) {
this.productId = productId;
this.currentRevision = currentRevision;
}

public String getProductId() {
return productId;
}

public int getCurrentRevision() {
return currentRevision;
}

public String getDownloadUrl() {
return downloadUrl;
}

}
Expand Up @@ -21,6 +21,8 @@ public interface UrbanAirship {

FeedOperations feedOperations();

InAppPurchaseOperations inAppPurchaseOperations();

PartnerOperations partnerOperations();

PushOperations pushOperations();
Expand Down
@@ -0,0 +1,44 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mobile.urbanairship.impl;

public class DownloadUrl {

private String contentUrl;

private Integer receiptStatus;

private String serverResponse;

public DownloadUrl(String contentUrl, Integer receiptStatus, String serverResponse) {
this.contentUrl = contentUrl;
this.receiptStatus = receiptStatus;
this.serverResponse = serverResponse;
}

public String getContentUrl() {
return contentUrl;
}

public Integer getReceiptStatus() {
return receiptStatus;
}

public String getServerResponse() {
return serverResponse;
}

}
@@ -0,0 +1,75 @@
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.mobile.urbanairship.impl;

import static org.springframework.http.HttpMethod.*;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.mobile.urbanairship.ContentReceipt;
import org.springframework.mobile.urbanairship.InAppPurchaseOperations;
import org.springframework.mobile.urbanairship.InvalidContentReceiptException;
import org.springframework.mobile.urbanairship.InventoryItem;
import org.springframework.mobile.urbanairship.Product;
import org.springframework.web.client.RestOperations;

public class InAppPurchaseTemplate implements InAppPurchaseOperations {

private final RestOperations restOperations;

public InAppPurchaseTemplate(RestOperations restOperations) {
this.restOperations = restOperations;
}

public List<InventoryItem> getInventory() {
return restOperations.getForObject(IAP_URL + "content", Inventory.class);
}

public String getDownloadUrl(String productId) {
DownloadUrl downloadUrl = restOperations.postForObject(IAP_URL + "content/" + productId + "/download", "", DownloadUrl.class);
return downloadUrl.getContentUrl();
}

public String getDownloadUrl(String productId, ContentReceipt receipt) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<ContentReceipt> requestEntity = new HttpEntity<ContentReceipt>(receipt, headers);
DownloadUrl response = restOperations.exchange(IAP_URL + "content/" + productId + "/download", POST, requestEntity, DownloadUrl.class).getBody();
if (response.getContentUrl() == null || response.getReceiptStatus() != null) {
throw new InvalidContentReceiptException(response.getReceiptStatus(), response.getServerResponse());
}
return response.getContentUrl();
}

public List<Product> checkForUpdates(List<Product> products) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<List<Product>> requestEntity = new HttpEntity<List<Product>>(products, headers);
return restOperations.exchange(IAP_URL + "updates", POST, requestEntity, ProductList.class).getBody();
}

private static final String IAP_URL = "https://go.urbanairship.com/api/app/";

@SuppressWarnings("serial")
private static class Inventory extends ArrayList<InventoryItem> {}

@SuppressWarnings("serial")
private static class ProductList extends ArrayList<Product> {}
}

0 comments on commit 7fb32b1

Please sign in to comment.