Created
January 2, 2013 11:24
-
-
Save dbathily/4433939 to your computer and use it in GitHub Desktop.
Recurly push notification handler in play framework using recurly-java-client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import com.ning.billing.recurly.model.Subscription; | |
import com.ning.billing.recurly.model.push.Notification; | |
import com.ning.billing.recurly.model.push.account.BillingInfoUpdatedNotification; | |
import com.ning.billing.recurly.model.push.account.CanceledAccountNotification; | |
import com.ning.billing.recurly.model.push.account.NewAccountNotification; | |
import com.ning.billing.recurly.model.push.payment.*; | |
import com.ning.billing.recurly.model.push.subscription.*; | |
import play.Logger; | |
import play.mvc.Controller; | |
import play.mvc.Result; | |
/** | |
* Created by User: bathily | |
* Date: 02/01/13 | |
* Time: 10:29 | |
*/ | |
public class Recurly extends Controller { | |
public static Result push() { | |
String payload = new String(request().body().asRaw().asBytes()); | |
if(Logger.isDebugEnabled()) | |
Logger.debug("Received from recurly :\n"+payload); | |
Notification.Type type = Notification.detect(payload); | |
if(type != null) { | |
Logger.debug("Treat it as "+type); | |
Notification notification = Notification.read(payload, type.getJavaType()); | |
switch (type) { | |
case BillingInfoUpdatedNotification: | |
onBillingInfoUpdatedNotification((BillingInfoUpdatedNotification)notification); | |
break; | |
case CanceledAccountNotification: | |
onCanceledAccountNotification((CanceledAccountNotification) notification); | |
break; | |
case CanceledSubscriptionNotification: | |
onCanceledSubscriptionNotification((CanceledSubscriptionNotification) notification); | |
break; | |
case ExpiredSubscriptionNotification: | |
onExpiredSubscriptionNotification((ExpiredSubscriptionNotification) notification); | |
break; | |
case FailedPaymentNotification: | |
onFailedPaymentNotification((FailedPaymentNotification) notification); | |
break; | |
case NewAccountNotification: | |
onNewAccountNotification((NewAccountNotification) notification); | |
break; | |
case NewSubscriptionNotification: | |
onNewSubscriptionNotification((NewSubscriptionNotification) notification); | |
break; | |
case ReactivatedAccountNotification: | |
onReactivatedAccountNotification((ReactivatedAccountNotification) notification); | |
break; | |
case RenewedSubscriptionNotification: | |
onRenewedSubscriptionNotification((RenewedSubscriptionNotification) notification); | |
break; | |
case SuccessfulPaymentNotification: | |
onSuccessfulPaymentNotification((SuccessfulPaymentNotification) notification); | |
break; | |
case SuccessfulRefundNotification: | |
onSuccessfulRefundNotification((SuccessfulRefundNotification) notification); | |
break; | |
case UpdatedSubscriptionNotification: | |
onUpdatedSubscriptionNotification((UpdatedSubscriptionNotification) notification); | |
break; | |
case VoidedPaymentNotification: | |
onVoidedPaymentNotification((VoidedPaymentNotification) notification); | |
break; | |
default: | |
Logger.info("Unsupported recurly notification "+type); | |
break; | |
} | |
}else{ | |
Logger.warn("Received an unknown notification from recurly"); | |
} | |
return ok(); | |
} | |
public static void onBillingInfoUpdatedNotification(BillingInfoUpdatedNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
Logger.debug("onBillingInfoUpdated for account "+account.getAccountCode()); | |
} | |
public static void onCanceledAccountNotification(CanceledAccountNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
Logger.debug("onCanceledAccount for account "+notification.getAccount().getAccountCode()); | |
} | |
public static void onCanceledSubscriptionNotification(CanceledSubscriptionNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
PushSubscription subscription = notification.getSubscription(); | |
Logger.debug("onCanceledSubscription for account "+account.getAccountCode()+" and subscription "+subscription.getUuid()); | |
} | |
public static void onExpiredSubscriptionNotification(ExpiredSubscriptionNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
PushSubscription subscription = notification.getSubscription(); | |
Logger.debug("onExpiredSubscription for account "+account.getAccountCode()+" and subscription "+subscription.getUuid()); | |
} | |
public static void onFailedPaymentNotification(FailedPaymentNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
PushTransaction transaction = notification.getTransaction(); | |
Logger.debug("onFailedPayment for account "+account.getAccountCode()+" and transaction "+transaction.getId()); | |
} | |
public static void onNewAccountNotification(NewAccountNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
Logger.debug("onNewAccount for account "+notification.getAccount().getAccountCode()); | |
} | |
public static void onNewSubscriptionNotification(NewSubscriptionNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
Subscription subscription = notification.getSubscription(); | |
Logger.debug("onNewSubscription for account "+account.getAccountCode()+" and subscription "+subscription.getUuid()); | |
} | |
public static void onReactivatedAccountNotification(ReactivatedAccountNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
PushSubscription subscription = notification.getSubscription(); | |
Logger.debug("onReactivatedAccount for account "+account.getAccountCode()+" and subscription "+subscription.getUuid()); | |
} | |
public static void onRenewedSubscriptionNotification(RenewedSubscriptionNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
PushSubscription subscription = notification.getSubscription(); | |
Logger.debug("onRenewedSubscription for account "+account.getAccountCode()+" and subscription "+subscription.getUuid()); | |
} | |
public static void onSuccessfulPaymentNotification(SuccessfulPaymentNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
PushTransaction transaction = notification.getTransaction(); | |
Logger.debug("onSuccessfulPayment for account "+account.getAccountCode()+" and transaction "+transaction.getId()); | |
} | |
public static void onSuccessfulRefundNotification(SuccessfulRefundNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
PushTransaction transaction = notification.getTransaction(); | |
Logger.debug("onSuccessfulRefund for account "+account.getAccountCode()+" and transaction "+transaction.getId()); | |
} | |
public static void onUpdatedSubscriptionNotification(UpdatedSubscriptionNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
PushSubscription subscription = notification.getSubscription(); | |
Logger.debug("onUpdatedSubscription for account "+account.getAccountCode()+" and subscription "+subscription.getUuid()); | |
} | |
public static void onVoidedPaymentNotification(VoidedPaymentNotification notification) { | |
com.ning.billing.recurly.model.Account account = notification.getAccount(); | |
PushTransaction transaction = notification.getTransaction(); | |
Logger.debug("onVoidedPayment for account "+account.getAccountCode()+" and transaction "+transaction.getId()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment