Page 1 of 1

DoCapture not getting called

Posted: Sun Mar 02, 2014 11:26 am
by hacrodrigues
Hi all, I've setup a custom Payment Gateway which is very simple:
1. On the DoAuthorize, I send my provider authentication information and order data to get a redirect url and a token;
2. Before redirecting the user I associate the token with the payment;
3. Once I finish the payment on the provider's site It redirects the user to a page of mine with the same token;
4. At this last page, I should be able to "fire" the DoCapture so that, within my custom PaymentProvider, I could ask for the provider if the payment was successful, etc.

The issue is precisely this last step. What should I do on this page?

Already tried, creating a Capture Transaction, associate it to the payment and save but nothing happens. Also tried "payment.Order.Save(true)" but also nothing.

I know I could call payment.Capture() but this won't get my DoCapture called and I need it...I have to confirm with my provider the payment for that token before considering it Paid.

Please help!!!

Re: DoCapture not getting called

Posted: Mon Mar 03, 2014 2:33 am
by hacrodrigues
I'm not sure how the Paypal provider works but I'm pretty sure that what I'm looking for is something very similar, process wise at least.

I believe the main difference is that I'm not supposed to implement an IPN as Paypal. The logic that is implemented on the IPN (handler, page or whatever) is the logic I need implemented on the page the user gets redirected.

Re: DoCapture not getting called

Posted: Mon Mar 03, 2014 7:19 am
by jguengerich
Are you sure payment.Capture() doesn't get your DoCapture called? When I follow the source code, it does payment.Capture -> PaymentEngine.Capture -> instance.DoCapture (where instance is an instance of the Payment Gateway).

Re: DoCapture not getting called

Posted: Mon Mar 03, 2014 1:30 pm
by hacrodrigues
Hi Jay, thank you so much for you reply.

I'm using Ablecommerce Gold and can't found a Capture() method without parameters. I have to send the amount but at this time, I have no certain that the buyer has indeed payed the full amount of the order.

Right now I was trying to do this (assume this code is inside a Page_Load(...) or a ProcessRequest of an HttpHandler):

if (!string.IsNullOrEmpty(this.Request["token"]))
{
string token = this.Request["token"];

ICriteria criteria = CommerceBuilder.DomainModel.NHibernateHelper.CreateCriteria<Payment>();
criteria.Add(Restrictions.Eq("ReferenceNumber", token));
IList<Payment> payments = PaymentDataSource.LoadForCriteria(criteria);

if (payments.Count > 0)
{
Payment payment = payments[0];
if (payment.PaymentStatus != PaymentStatus.Captured)
{
Transaction capture = new Transaction();
capture.Payment = payment;
capture.TransactionDate = DateTime.Now;
capture.RemoteIP = this.Request.ServerVariables["REMOTE_ADDR"].ToString();
capture.TransactionType = TransactionType.Capture;
capture.Save();

payment.Save(true);
}

this.Response.Redirect(string.Format("~/Checkout/Receipt.aspx?OrderNumber={0}&status=cmp", payment.Order.OrderNumber.ToString()), true);
}
}
else
{
Response.Redirect("~/Default.aspx");
}

Any ideas?

Regards.

Re: DoCapture not getting called

Posted: Mon Mar 03, 2014 2:46 pm
by jguengerich
It's been a while since I worked with that part of my site, but I think you could just replace your code:

Code: Select all

Transaction capture = new Transaction();
capture.Payment = payment;
capture.TransactionDate = DateTime.Now;
capture.RemoteIP = this.Request.ServerVariables["REMOTE_ADDR"].ToString();
capture.TransactionType = TransactionType.Capture;
capture.Save();
With:

Code: Select all

payment.Capture(payment.Order.TotalCharges);
As I mentioned, this will call PaymentEngine.Capture, which will call your custom Payment Gateway's DoCapture, which must return a Transaction object. You would check with your provider and build your capture Transaction object there. Your DoCapture receives a parameter of type CaptureTranasactionRequest, which has the Payment you are working with as one of it's properties. PaymentEngine.Capture takes care of attaching the capture Transaction you return from your DoCapture method to the Payment and saves the Order, which I think saves the payment and the transaction as well. I think the payment events are triggered, but I'm not sure. Maybe someone from AbleCommerce can verify (or clarify).

EDIT: I looked at this a little more, and there may be more involved. PaymentEngine.Capture will call your custom Payment Gateway's DoAuthorize if there isn't an Authorization Transaction attached to the payment yet. From your description, it sounds like you don't have any transaction records yet. You may just have to call your DoCapture directly. You'll have to load your PaymentGateway, call it's GetInstance() method, create a CaptureTransactionRequest, then call instance.DoCapture(...). Sorry, I hope I haven't made things even more confusing.

Re: DoCapture not getting called

Posted: Tue Mar 04, 2014 3:42 am
by hacrodrigues
Jay, you're the best. Thank you so much for your help. You saw it just right.

Initially I started out by simply making the payment.Capture(totalCharges) as you mentioned but since it kept failing I thought the problem was on the way I was triggering the DoCapture. It turns out I didn't have a Authorized transaction but a Authorize Pending transaction. Once I got that working I got stuck, again, with the way I was trigerring the DoCapture. You cleared it up really nicely.

Thanks again.