asp.net mvc 4 - PayPal with MVC -
i have following sample call paypal's rest api make payment. works fine i'd use new mvc app. guess needs adapted make use of mvc helper methods eg redirecttoaction rather server.transfer etc.
has converted sample run in mvc4 controller action?
// ###payer // resource representing payer funds payment // payment method // `paypal` payer payr = new payer(); payr.payment_method = "paypal"; random rndm = new random(); var guid = convert.tostring(rndm.next(100000)); string baseuri = request.url.scheme + "://" + request.url.authority + "/paymentwithpaypal.aspx?"; // # redirect urls redirecturls redirurls = new redirecturls(); redirurls.cancel_url = baseuri + "guid=" + guid; redirurls.return_url = baseuri + "guid=" + guid; // ###details // let's specify details of payment amount. details details = new details(); details.tax = "15"; details.shipping = "10"; details.subtotal = "75"; // ###amount // let's specify payment amount. amount amnt = new amount(); amnt.currency = "usd"; // total must equal sum of shipping, tax , subtotal. amnt.total = "100"; amnt.details = details; // ###transaction // transaction defines contract of // payment - payment , // fulfilling it. transaction created // `payee` , `amount` types list<transaction> transactionlist = new list<transaction>(); transaction tran = new transaction(); tran.description = "transaction description."; tran.amount = amnt; // payment creation api requires list of // transaction; add created `transaction` // list transactionlist.add(tran); // ###payment // payment resource; create 1 using // above types , intent 'sale' pymnt = new payment(); pymnt.intent = "sale"; pymnt.payer = payr; pymnt.transactions = transactionlist; pymnt.redirect_urls = redirurls; try { // ###accesstoken // retrieve access token // oauthtokencredential passing in // clientid , clientsecret // not mandatory generate access token on per call basis. // typically access token can generated once , // reused within expiry window string accesstoken = new oauthtokencredential(configmanager.instance.getproperties()["clientid"], configmanager.instance.getproperties()["clientsecret"]).getaccesstoken(); // ### api context // pass in `apicontext` object authenticate // call , send unique request id // (that ensures idempotency). sdk generates // request id if not pass 1 explicitly. apicontext apicontext = new apicontext(accesstoken); // use variant if want pass in request id // meaningful in application, ideally // order id. // string requestid = long.tostring(system.nanotime(); // apicontext apicontext = new apicontext(accesstoken, requestid )); // create payment posting apiservice // using valid accesstoken // return object contains status; payment createdpayment = pymnt.create(apicontext); currcontext.items.add("responsejson", jobject.parse(createdpayment.converttojson()).tostring(formatting.indented)); var links = createdpayment.links.getenumerator(); while (links.movenext()) { links lnk = links.current; if (lnk.rel.tolower().trim().equals("approval_url")) { currcontext.items.add("redirecturl", lnk.href); } } session.add(guid, createdpayment.id); } catch (paypal.exception.paypalexception ex) { currcontext.items.add("error", ex.message); } } currcontext.items.add("requestjson", jobject.parse(pymnt.converttojson()).tostring(formatting.indented)); server.transfer("~/response.aspx");
depending on need, finished putting basic mvc implementation supports authorize, capture, , void paypal payments (non-credit card) via rest api.
Comments
Post a Comment