A few days ago, due to the company's project requirements, we had to use the online recharge function, that is, click a web page in the browser to initiate a payment. now let's roughly look at the development process of public account payment: A few days ago, due to the needs of the company's projects, we had to use the online recharge function, that is, click a webpage in the browser to initiate the payment. now let's take a rough look at the payment.
Public account paymentDevelopment process:
First, your public account must beAuthentication service number,You need to activate the payment permission. before developing and writing code, we need to set some payment-related information to facilitate subsequent operations. after activation, we will send you an email, it contains information related to background login of your public account merchant platform. after logging on to the merchant platform, go to->Account Settings->API securitySet the secret, which will be used later;
The main interaction between the merchant system and the payment system:
1. the merchant server calls the unified order interface to request orders. For more information about the api, see Public api [unified order API]. before requesting the pre-payment order, we need to call the OAuth2.0 web page to authorize the user to obtain the OpenId, which is not described in detail here, the following is the code implementation of the pre-payment order:
String timeStamp = TenPayUtil. getTimestamp (); string nonceStr = TenPayUtil. getNoncestr (); string paySign = string. empty; // create the payment response object var packageReqHandler = new RequestHandler (null); string spbill_create_ip = Request. userHostAddress; // Initialization // packageReqHandler. init (); // packageReqHandler. setKey (TenPayInfo. key); // Set the package order parameter packageReqHandler. setParameter ("appid", appID); // public account ID packageReqHandler. setParameter ("body", StrUtil. getCutString (productName, 100); // cannot exceed 127 characters packageReqHandler. setParameter ("mch_id", mchid); // merchant ID packageReqHandler. setParameter ("nonce_str", nonceStr. toLower (); // random string packageReqHandler. setParameter ("yy_url", notifyUrl); // The URL packageReqHandler that receives the Caifu notification. setParameter ("openid", openId); // openid packageReqHandler. setParameter ("out_trade_no", sp_billno); // merchant Order Number // packageReqHandler. setParameter ("attach", ""); // additional data can be used to differentiate different payment services in the future packageReqHandler. setParameter ("spbill_create_ip", spbill_create_ip); // the user's public ip address, not the merchant's server IP packageReqHandler. setParameter ("total_eter", (onlinePayMoney * 100 ). toString ("0"); // item amount, in units (money * 100 ). toString () packageReqHandler. setParameter ("trade_type", "JSAPI"); // transaction type // Obtain the package string sign = packageReqHandler. createMd5Sign ("key", TenPayInfo. key); packageReqHandler. setParameter ("sign", sign); // transaction type string data = packageReqHandler. parseXML (); LoggerHelper. log (data); // call the unified order interface to request the order var result = TenPayV3Service. unifiedorder (data); LoggerHelper. log (result); var res = XDocument. parse (result); string prepayId = string. empty; if (res. element ("xml "). element ("return_code "). value = "SUCCESS") {prepayId = res. element ("xml "). element ("prepay_id "). value;} string package = string. format ("prepay_id = {0}", prepayId); timeStamp = TenPayUtil. getTimestamp (); // set the payment parameter var paySignReqHandler = new RequestHandler (null); paySignReqHandler. setParameter ("appId", appID); paySignReqHandler. setParameter ("timeStamp", timeStamp); paySignReqHandler. setParameter ("nonceStr", nonceStr); paySignReqHandler. setParameter ("package", package); paySignReqHandler. setParameter ("signType", "MD5"); paySign = paySignReqHandler. createMd5Sign ("key", TenPayInfo. key); // pass the information to the ViewBag on the payment page. appId = appID; ViewBag. timeStamp = timeStamp; ViewBag. nonceStr = nonceStr; ViewBag. package = package; ViewBag. paySign = paySign;
The following is the JavaScript code for the page: