i having problems invoking paypal ipn. dont know url give or url meant give. have looked on internet there not seem available hence why have come here.
so firstly, have paymentwithpaypal action
public actionresult paymentwithpaypal(int? id, page page) { //getting apicontext earlier apicontext apicontext = models.configuration.getapicontext(); try { string payerid = request.params["payerid"]; if (string.isnullorempty(payerid)) { string baseuri = request.url.scheme + "://" + request.url.authority + "/controllername/paymentwithpaypal?"; var guid = guid.newguid().tostring(); //createpayment function gives payment approval url //on payer redirected paypal acccount payment var createdpayment = this.createpayment(apicontext, baseuri + "guid=" + guid); //get links returned paypal in response create function call var links = createdpayment.links.getenumerator(); string paypalredirecturl = null; while (links.movenext()) { links lnk = links.current; if (lnk.rel.tolower().trim().equals("approval_url")) { //saving payapalredirect url user redirected payment paypalredirecturl = lnk.href; } } // saving paymentid in key guid session.add(guid, createdpayment.id); return redirect(paypalredirecturl); } else { // section executed when have received payments parameters // previous call function create // executing payment var guid = request.params["guid"]; var executedpayment = executepayment(apicontext, payerid, session[guid] string); if (executedpayment.state.tolower() != "approved") { return view("failureview"); } } } catch (exception ex) { logger.log("error" + ex.message); return view("failureview"); } return view("successview"); }
this code ipn.
[httppost] public httpstatuscoderesult receive() { //store ipn received paypal logrequest(request); //fire , forget verification task task.run(() => verifytask(request)); //reply 200 code return new httpstatuscoderesult(httpstatuscode.ok); } private void verifytask(httprequestbase ipnrequest) { var verificationresponse = string.empty; try { var verificationrequest = (httpwebrequest)webrequest.create("https://www.sandbox.paypal.com/cgi-bin/webscr"); //set values verification request verificationrequest.method = "post"; verificationrequest.contenttype = "application/x-www-form-urlencoded"; var param = request.binaryread(ipnrequest.contentlength); var strrequest = encoding.ascii.getstring(param); //add cmd=_notify-validate payload strrequest = "cmd=_notify-validate&" + strrequest; verificationrequest.contentlength = strrequest.length; //attach payload verification request var streamout = new streamwriter(verificationrequest.getrequeststream(), encoding.ascii); streamout.write(strrequest); streamout.close(); //send request paypal , response var streamin = new streamreader(verificationrequest.getresponse().getresponsestream()); verificationresponse = streamin.readtoend(); streamin.close(); } catch (exception exception) { logger.log("error" + exception.message); //capture exception manual investigation } processverificationresponse(verificationresponse); } private void logrequest(httprequestbase request) { // persist request values database or temporary data store } private void processverificationresponse(string verificationresponse) { if (verificationresponse.equals("verified")) { logger.log("verified"); // check payment_status=completed // check txn_id has not been processed // check receiver_email primary paypal email // check payment_amount/payment_currency correct // process payment } else if (verificationresponse.equals("invalid")) { logger.log(verificationresponse); } else { //log error } }
now clear things up. understanding of ipn when customer purchases item, seller email telling them have sold product , can access transactionid etc.
so in view have form button looks this.
@html.actionlink("buy now", "paymentwithpaypal", new { id = model.id, @class = "" })
this takes customer paypal can purchase stuck because im not sure how call ipn or if needs own view.
any clarity of @ moment in time.
Comments
Post a Comment