Skip to content

Instantly share code, notes, and snippets.

@eltimn
Created April 23, 2012 19:04
Show Gist options
  • Save eltimn/2473119 to your computer and use it in GitHub Desktop.
Save eltimn/2473119 to your computer and use it in GitHub Desktop.
Lift-MongoDB PayPalTransaction
import java.math.MathContext
import net.liftweb.record.field._
import net.liftweb._
import common._
import json._
import json.ext.{EnumSerializer, JsonBoxSerializer}
import mongodb.record._
import mongodb.record.field._
import paypal._
import util.Helpers.tryo
class PaypalTransaction extends MongoRecord[PaypalTransaction] with StringPk[PaypalTransaction] {
def meta = PaypalTransaction
object itemName extends StringField(this, 256)
object itemNumber extends StringField(this, 256)
object custom extends StringField(this, 256)
object status extends EnumField(this, PaypalTransactionStatus)
object mcGross extends DecimalField(this, MathContext.DECIMAL32, 2)
object mcFee extends DecimalField(this, MathContext.DECIMAL32, 2)
object mcCurrency extends StringField(this, 2)
object paymentDate extends DateField(this)
object parentTxnId extends StringField(this, 256) {
override def optional_? = true
}
//object receiptId extends StringField(this, 256)
object payerEmail extends EmailField(this, 256)
object payerId extends StringField(this, 13)
object paymentType extends EnumField(this, PaypalPaymentType)
object pendingReason extends StringField(this, 256)
object reasonCode extends StringField(this, 256)
}
object PaypalTransaction extends PaypalTransaction with MongoMetaRecord[PaypalTransaction] with Loggable {
override def collectionName = "paypal.transactions"
def setFromPaypalInfo(ppi: PayPalInfo): PaypalTransaction = {
val inst = createRecord
setFieldsFromPaypalInfo(inst, ppi)
inst
}
def setFieldsFromPaypalInfo(inst: PaypalTransaction, ppi: PayPalInfo): Unit = {
inst.id(ppi.txnId openOr ObjectId.get.toString)
inst.itemName.setBox(ppi.itemName)
inst.itemNumber.setBox(ppi.itemNumber)
inst.custom.setBox(ppi.custom)
inst.status.setBox(ppi.paymentStatus)
inst.mcGross.setFromString(ppi.mcGross openOr "")
inst.mcFee.setFromString(ppi.mcFee openOr "")
inst.mcCurrency.setBox(ppi.mcCurrency)
inst.paymentDate.setFromString(ppi.paymentDate openOr "")
inst.parentTxnId.setBox(ppi.parentTxnId)
//inst.receiptId.setBox(ppi.receiptId)
inst.payerEmail.setBox(ppi.payerEmail)
inst.payerId.setBox(ppi.payerId)
inst.paymentType.setBox(ppi.paymentType.flatMap(pt => PaypalPaymentType.find(pt)))
inst.pendingReason.setBox(ppi.pendingReason)
inst.reasonCode.setBox(ppi.reasonCode)
}
}
object PaypalPaymentType extends Enumeration {
val Instant = Value(1, "Instant")
val ECheck = Value(2, "eCheck")
def find(name: String): Box[Value] = {
val n = name.trim.toLowerCase
this.values.find(v => v.toString.toLowerCase == n)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment