[back to
Anatomy of a Domino e-commerce Web site (Part 3)
]
The (CreditCard) agent
Here is the code for the (CreditCard) agent:
Option Declare
'This 'Uselsx' statement would be uncommented, provided you were using Commerce Accelerator
'Uselsx "*Commerce"
Public Const CC_ORDER_ID = "order-id"
Public Const CC_NOTE = "note"
Public Const CC_AMOUNT = "amount"
Public Const CC_CARD_NAME = "card-name"
Public Const CC_CARD_STATE = "card-state"
Public Const CC_CARD_COUNTRY = "card-country"
Public Const CC_CARD_NBR = "card-number"
Public Const CC_CARD_CITY = "card-city"
Public Const CC_CARD_ADDR = "card-address"
Public Const CC_CARD_EXP = "card-exp"
Public Const CC_CARD_ZIP = "card-zip"
Public Const CC_TXN_TYPE = "txn-type"
Public Const CC_MSG_TYPE = "message-type"
Public Const CC_MERCHANT_MSG = "merchant-msg"
Public Const CC_MERCH_TXN = "merch-txn"
Sub Initialize
Dim s As New NotesSession, db As NotesDatabase
Dim doc As NotesDocument, odoc As NotesDocument
Set doc = s.DocumentContext
Set db = doc.ParentDatabase
Dim vCartID As Variant, vSessionID As Variant, vOrderID As Variant
Dim vPath As Variant
'Set URL path for return
vPath=Evaluate({@ReplaceSubstring (@Subset (@DbName; -1); "\\" : " "; "/" : "+")})
'Get the cart and order IDs for this doc
vCartID = Evaluate ( {@Middle (Query_String + "&"; "&CartID="; "&") }, doc)
vOrderID = Evaluate ( {@Middle (Query_String + "&"; "&r="; "&") }, doc)
'Get the Order document based on OrderID from Query_String
Set odoc = db.GetView ("(OrdersByID)").GetDocumentByKey (vOrderID(0), True)
Dim vAmount As Variant, strParm As String
'The commented code throughout this agent are those pieces specific to Commerce Accelerator
'that will cause errors when the LSX is not loaded on the local machine or server. Under regular
'operating conditions, this code would of course be uncommented
'Dim args As New CaNameValueList
odoc.AgentCybercashRan = Now
Dim num As Integer
'args.addPair CC_ORDER_ID , "KRG-LF-"+ odoc.ID(0)
'args.addPair CC_AMOUNT, Cstr (odoc.OrderPriceTotal(0))
'args.addPair CC_CARD_NAME, odoc.Name(0)
'args.addPair CC_CARD_ADDR, odoc.Street1(0)
'args.addPair CC_CARD_CITY, odoc.City(0)
'args.addPair CC_CARD_STATE, odoc.State(0)
'args.addPair CC_CARD_ZIP, odoc.Zip(0)
'args.addPair CC_CARD_COUNTRY, odoc.Country(0)
'args.addPair CC_CARD_NBR, odoc.ccAcctNum(0)
'args.addPair CC_CARD_EXP, odoc.CCExpMn(0) + "/" + odoc.CCExpYr(0)
'Initialize a direct connection to CyberCash using your Cybercash ID and Merchant Key
'You can also supply a Proxy server and Timeout here
'Dim cc As CACybercashConnection
'Dim result As New CaNameValueList
If odoc.CCAcctNum(0) = "4111111111111111" Then
' test transaction
'Set cc = New CaCybercashConnection("test-vvv", "asdfaeer2r4faef") ' Test One
'Call cc.Send("mauthcapture", args, result)
End If
'odoc.MStatus = result.getval ("MStatus")
'odoc.Mdump = result.dump
'If the CyberCash response with "succss", finalize the Order status, email the
'customer a receipt, and clear the Order Items out of their cart. Otherwise, we'll
'simply mark the Order status as failed. strParm will be used by the OrderThankYou
'form to display the appropriate message to the customer. Unconditionally save
'the Order document to reflect the Order's new status.
If odoc.Mstatus(0) = "success" Then
odoc.Status = "Charged"
Call SendReceipt (s, db, odoc)
Call ClearCart (s, db, odoc)
strParm = "Yes"
Else
odoc.Status = "ChargeFailed"
strParm = "No"
End If
Call odoc.save (False, True)
'Display the OrderThankYou form
Print "[" + vpath(0) + "/OrderThankYou?ReadForm&CartID=" + vCartID(0) + "&OrderID=" + vOrderID(0) + "&Success=" + strParm + "]"
End Sub
Sub SendReceipt (s As NotesSession, db As NotesDatabase, odoc As NotesDocument)
Dim v As NotesView
Dim cdoc As NotesDocument, mdoc As NotesDocument
'Build the receipt document general information
Set mDoc = db.CreateDocument
mDoc.Form = "Memo"
mDoc.Principal = "Catalog"
mDoc.SendTo =odoc.Email
mDoc.Subject = "Your order receipt."
'This next bit of code shows a trick. First, a rich text field is created
'on the receipt document. Then, we'll actually switch the form associated
'with the Order document, replacing the Order form with the OrderReceipt
'form and saving it. Next, the Order (now viewed via the OrderReceipt form) is
'rendered to richtext onto the receipt doc, which then has all the
'Order information, but it's displayed through the OrderReceipt form. Before
'sending the customer the receipt, the Order doc's form is changed back to
'Order.
Dim rt As New NotesRichTextItem (mdoc, "Body")
odoc.Form = "OrderReceipt"
Call odoc.Save (False, True)
Call odoc.RenderToRTItem ( rt )
odoc.Form = "Order"
Call odoc.RemoveItem ("OrdersDBW")
Call odoc.Save (False, True)
Call mDoc.Send (False)
End Sub
Sub ClearCart (s As NotesSession, db As NotesDatabase, odoc As NotesDocument)
Dim CartColl As NotesDocumentCollection
Dim LibraryDB As NotesDatabase
Dim strCartID As String
Dim strDBPath As String
'Find the Library database
strDBPath=db.GetProfileDocument("ApplicationSettings").LibraryDB(0)
Set LibraryDB = New NotesDatabase(db.Server,strDBPath)
'Reach back into the Library database and get all the Order Item
'documents for this cart. Mark their status as 'Ordered' and also
'give each the OrderID
Set CartColl = LibraryDB.GetView ("(CartDetailByID)").GetAllDocumentsByKey (odoc.CartID(0))
Call CartColl.StampAll ("Status", "Ordered")
Call CartColl.StampAll ("OrderID", odoc.ID(0))
End Sub