この記事では、Alipayを使用してJavaの特定のコードを共有して支払いのためにコードをスキャンします。参照のために、特定のコンテンツは次のとおりです。
準備
まず、Alipay Sandboxテストアカウントを開きます。これには、消費者アカウントと受取人アカウントが含まれます
QRコードをスキャンして、モバイルアプリをダウンロードします
基本的な構成
必要な瓶パッケージ
AlipayConfig
パッケージcom.alipay.config;インポートjava.io.filewriter;インポートjava.io.io.ioexception; import java.util.resourcebundle;/ * * *クラス名:alipayconfig *機能:基本的な構成クラス *詳細:アカウント関連情報と戻りパスと返品パスの修正日付デート商人は、自分のウェブサイトのニーズに従って、および技術文書に従って書くことができ、このコードを使用する必要はありません。 *このコードは、Alipayインターフェイスのみを学習および調査するためのものであり、参照としてのみ提供されます。 */public class alipayconfig {//↓↓↓ある萎病↓いい懲いい表示たら、ここで基本情報を構成してください//アプリケーションID、appid、および支払いアカウントは対応するalipay account public static string app_id = "2016080403162340"; // merchant秘密キー、PKCS8形式RSA2秘密キーpublic static string merchant_key = "mieivaid2tulssmawg5+f4nzbexpnxi8nkqjpzeeaa =="; // Alipay公開キー、アドレスをご覧ください:https://openhome.alipay.com/platform/keymanage.htm Appidの下の対応するAlipay公開キー。 public static string alipay_public_key = "miibijt26tltkar8s1erdwi25vibcmz7plmxvvumhf5tdbwfbmhus3qidaqab"; //サーバー非同期通知ページパスは、http://の形式でフルパスを必要とし、?id = 123のようなカスタムパラメーターを追加できません。通常、外部ネットワークからアクセスする必要があります。 public static string notify_url = "http:// localhost:8080/alipay.trade.page.pay-java-utf-8/notify_url.jsp"; //ページジャンプ同期通知ページパスには、http://形式の完全なパスが必要です。 ID = 123などのカスタムパラメーターを追加できません。 public static string return_url = "http:// localhost:8080/exam/index/goumai"; //署名メソッドpublic static string sign_type = "rsa2"; //文字エンコードフォーマットpublic static string charset = "utf-8"; // Alipay Gateway public Static String Gatewayurl = "https://openapi.alipaydev.com/gateway.do"; // alipayゲートウェイpublic static string log_path = "e://"; //↑↑↑↑ヒ」ヒ」ヒ」ヒ」ヒ」ヒ」遺」遺」点明らか」から基本情報を構成してください/***簡単なテストのためにログを書き込みます(ウェブサイトの要件を参照して、データベースにレコードを保存することもできます) try {writer = new filewriter(log_path + "alipay_log_" + system.currenttimemillis() + "。txt"); writer.write(sword); } catch(Exception e){e.printstacktrace(); }最後に{if(writer!= null){try {writer.close(); } catch(ioexception e){e.printstacktrace(); }}}}}}コントローラ
// Generate a page with a QR code that can be used to scan the code to pay for payment @RequestMapping(value = "aliPay") public String aliPay(HttpServletResponse response,ModelMap map,String chapterId,HttpServletRequest request, String WIDout_trade_no,String WIDtotal_amount,String WIDsubject,String WIDbody) throws IOException, alipayapiexception {// string a、string urlname、string couname ...+"&a ="+a+"&urlname ="+urlname+"&couname //初期化されたalipayclient alipayclient = new defaultalipayclient alipayconfig.merchant_private_key、 "json"、alipayconfig.charset、alipayconfig.alipay_public_key、alipayconfig.sign_type); //リクエストパラメーターを設定しますalipaytradepagepayrequest alipayrequest = new AlipaytradepagePayrequest(); alipayrequest.setreturnurl(alipayconfig.return_url+"?chapterid ="+chapterid); alipayrequest.setNotifyurl(alipayconfig.notify_url); //支払いID、必須文字列out_trade_no = widout_trade_no; //支払額、必要な文字列attotal_amount = widtotal_amount; total_amount = urldecoder.decode(total_amount、 "utf-8"); // transcoding //注文名、必要な文字列件名= widsubject; subject = urldecoder.decode(subject、 "utf-8"); //製品の説明、null string body = widbody; alipayrequest.setBizContent( "{/" out_trade_no/":/" + out_trade_no + "/"、 + "/" total_amount/":/" + total_amount + "/"、 " +"/"subject/":/" + subject +"/" +" "/" timeout_express/":/" 1m/"、" + "/" product_code/":/" fast_instant_trade_pay/"}"); //文字列result = alipayclient.pageexecute(alipayrequest).getbody(); Response.setContentType( "text/html; charset = utf-8"); printwriter out = response.getWriter(); out.println(result); nullを返します。 }正常に支払い、ページを返します(return_url)
成功後のリターンパス、コントローラーに移動し、詳細についてはAlipayConfigの構成を参照してください
//クリックして購入テーブルでコースを購入して保存します@requestMapping(value = "goumai")@responsebodyパブリックモデルアンドビューgoumai(string chaptherid、httpservletrequest req、httpservletrequest req、string a、string urlname){modelandview mav = new modelandview(); map <string、string> map1 = new hashmap <string、string>(); // sysusertab login_user = sysuserservice.getsysuserbyid(userid); httpsession session = req.getsession(); sysusertab login_user1 =(sysusertab)session.getattribute( "login_user"); string userid = login_user1.getuserid(); // session.setattribute( "login_user"、login_user); map1.put( "userid"、userid); Map1.put( "ChapterId"、ChapterID); int num = sysbuyservice.getBuycount(mapp1); if(num == 0){mapp1.put( "buyid"、uuid.randomuuid()。toString()。置換( " - "、 "")); sysbuysersertbuy(mapp1); } //クエリコースコンテンツ// string fanhui = showfh(req、chapterid、urlname、couname、map、a); mav.setViewName( "jsp/pay/paysuccess"); mavを返します。 }支払いが成功した後、ページはpaysuccess.jspページにジャンプします。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。