초 상세 자바 접속 ping + 결제 (취 합 결제)

62644 단어 공구.
package com.hecross.common.pay;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;

import com.hecross.common.crypt.Base64;
import com.pingplusplus.Pingpp;
import com.pingplusplus.exception.APIConnectionException;
import com.pingplusplus.exception.APIException;
import com.pingplusplus.exception.AuthenticationException;
import com.pingplusplus.exception.ChannelException;
import com.pingplusplus.exception.InvalidRequestException;
import com.pingplusplus.exception.RateLimitException;
import com.pingplusplus.model.Charge;
import com.pingplusplus.model.Refund;

/**
 *     
 *
 * @author zqli
 *
 */
public class PayUtil {
   public static String REFUNDSUCCESS = "pending";
   /**
    * Pingpp         API Keyapi_key [Dashboard](https://dashboard.pingxx.com)->             ->    -> Secret
    * Key
    */
   // private final static String apiKey = "sk_test_unXbjTPi9m5GXLWvfD8Wb140";
   private final static String apiKey = "sk_live_zzPu9GfPqjTKaL";
   private final static String apiKeytest = "sk_live_b9Ge1CPqDWfD";
   /**
    * Pingpp           IDapp_id [Dashboard](https://dashboard.pingxx.com)->        ->    ->   ID(App ID)
    */
   private final static String appId = "app_j9GCi118azv";
   private final static String appIdceshi = "app_uvrPO4fnD";

   /**
    * openssl     ,            :https://help.pingxx.com/article/123161    * (rsa_private_key.pem)    * [Dashboard](https://dashboard.pingxx.com)->         ->    ->    (        )                ->    Test
    *       ->        Live   
    */


   private final static String pubKey = "-----BEGIN PUBLIC KEY-----
" + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0pyjbt81nCR+HqEWBkT6
" + "DKz2amX6G/JJ7jX5IzxGmA2CoaEhgwA0IDZU9ft8TyTQK/BbmWasAFoZN2LV7u83
" + "eexWcb6gKgSr/PPblAmERgNrJ4PQqXnn+XFHGqmRrSaH+C2maNOtd4Sx6BYhSDKS
" + "gH7DbRGg2LyiLpc2C8qASj3/1kT8VVqqbIKOTA5N45YFfsAF
" + "dmIyaX6nimqtE2M6pknwQUtt8QtXht4+7Nd/op/GwftW6+80nF3bLGcj3
" + "fcNAM35zSqHptFUTUOjg5VknF5tP7FN6NO+h8GYUHmgwewUWKEUbVKJ6
" + "gwIDAQAB
" + "-----END PUBLIC KEY-----
"; private final static String pubKeyTest = "-----BEGIN PUBLIC KEY-----
" + "MIIBIjANBgkqhkiG9w0BAQEFAAOCgKCAQEApissF5ACwGRgrA7poo2F
" + "kDsAX3LAzs/8AXPQV8CAhGmMzYZGBx+NcurQdpB5j124ZUWvl8raAXnk
" + "nIyMe9OQZ3y0P7qCq//G+rp7SDqJV5hzuWChZSOFNs8wRtMGIfgLRKiO
" + "dEV4vQtsm9oDa4YMzePJDdHWCqQ7tswcnGdoShmsGbb+m+PVMC3eviQHb
" + "8EHl0jXH2x//bI1CQRAFbVd+mgwZj1TZPV/3AttjNGvKb9oYDZ
" + "vQMtbh2KzNsYxDQeQjRvNof+XASrDBUQahCQ+bGVrRh3AKEAA1fKhkTDu8XaK5FD
" + "1QIDAQAB
" + "-----END PUBLIC KEY-----
"; private final static String privateKey = "-----BEGIN RSA PRIVATE KEY-----
" + "MIICXgIBAAKBgQDa4iPHQoKjtoDkZ6bXRKtrLoMvCL3vSWqqsR7keS3RZNgaqMDC
" + "AGue8Lg3a6FGsp2S+B5RQ42vtyjxRwc3+oZg3P+KjEUMk2YL8BKaQIDAQAB
" + "AoGBAJT127WK3Q9/b76Bj/jmoeZilCKZR3GmvDIIMmb41SaxA6U4/V3KHXPgjO4i
" + "kHJlsnlH5JIXbgy4iB18UPrHWVatwx9Y2vWqPvt0wFfJIQlUbM5t2ChlFe9OSB6a
" + "U+y/2CNDHuWBWDXGF5iKZZcVsUvp3laaVFhUZhA9X+oeE0W1AkEA9747Ukc0Zs7/
" + "KlFyGIXrZz9SDhdyMDvPf0BwP1yxq2Q7UL0y1n4yMcsWxoyOCSQb17GQqWuKk2i6
" + "VSE5geLRbwJBAOItrIjL2DE6kjyB93ePubKZgJuwo4bm+E6NPBjPfdd
" + "h4c/z4EHJwhG58O/4bSWRCmjb1EWWt41hacCQQDab2hui0mwvCJPdbBb3vhH+JSo
" + "yBNuY834LSDLPTeKHr4N2kdQ+SxAhhfY+6wssHdLs7HaABzURAkAH
" + "9HVKBmlfKeOx3WCCrU659BFEw2lQ8rNtyHl9SAlOfZ6ZCdSHG7oFjr/8mMTNrLjA
" + "EMKeeF6DXGUw4+lkQVunAkEA+E8NAw/EQoF/xpn7vKORu5Lg7SYcQ
" + "6OzDoN1iua0RBjnjC/Qx2UhS2/ox9rhjhgd5oDxc0iXrag==
" + "-----END RSA PRIVATE KEY-----
"; private static PayUtil instance = new PayUtil(); private PayUtil() { } public static PayUtil getInstance() { return instance; } { // API Key // Pingpp.apiKey = apiKey; Pingpp.apiKey = apiKeytest; // // Pingpp.privateKeyPath = privateKeyFilePath; Pingpp.privateKey = privateKey; // Pingpp.privateKey = privateKeytest; } /** * /** map https://www.pingxx.com/api#api-c-new * * @param amount , : ( 1 100 * @param subject * @param body * @param orderNo * @param channel alipay wx * @param ip IP * @param extra ping++ * @param initialMetadata * @return */ // charge // { // "id": "ch_a5OinLGyzzjLXPSOy9rPKKiL", // "object": "charge", // "created": 1458186221, // "livemode": true, // "paid": false, // "refunded": false, // "app": "app_1Gqj58ynP0mHeX1q", // "channel": "alipay", // "order_no": "123456789", // "client_ip": "127.0.0.1", // "amount": 100, // "amount_settle": 100, // "currency": "cny", // "subject": "Your Subject", // "body": "Your Body", // "extra": {}, // "time_paid": null, // "time_expire": 1458272621, // "time_settle": null, // "transaction_no": null, // "refunds": { // "object": "list", // "url": "/v1/charges/ch_a5OinLGyzzjLXPSOy9rPKKiL/refunds", // "has_more": false, // "data": [] // }, // "amount_refunded": 0, // "failure_code": null, // "failure_msg": null, // "metadata": {}, // "credential": { // "object": "credential", // "alipay": { // "orderInfo": // "service=\"mobile.securitypay.pay\"&_input_charset=\"utf-8\"&notify_url=\"https%3A%2F%2Fapi.pingxx.com%2Fnotify%2Fcharges%2Fch_a5OinLGyzzjLXPSOy9rPKKiL\"&partner=\"2008010319263982\"&out_trade_no=\"123456789\"&subject=\"Your // Subject\"&body=\"Your // Body\"&total_fee=\"0.10\"&payment_type=\"1\"&seller_id=\"2088020116983982\"&it_b_pay=\"2016-03-18 // 11:43:41\"&sign=\"ODRJPReSwsH8om5fGTqvhia9453k4eUaaGMJTLMTnEYbBuceMyTathvKtdnUpsP6Q5%2F5jcEV887EdtBWi4tuMFHPQmm4dz1nG6b4Blafi6v2tvKaf8b0RiQTOycU4SxigugKoyfeR6E4AGA6uIzWUBRpkq%2BZf65eqT0qe712BJ0%3D\"&sign_type=\"RSA\"" // } // }, // "description": "Your Description" // } public Charge createCharge(int amount, String subject, String body, String channel, String ip, String orderNo, Map, Object> extra, Map, Object> initialMetadata) { Charge charge = null; Map, Object> chargeMap = new HashMap, Object>(); chargeMap.put("amount", amount);// , : ( 1 100 chargeMap.put("currency", "cny"); chargeMap.put("subject", subject); chargeMap.put("body", body); // String orderNo = new Date().getTime() + PayUtil.randomString(7); chargeMap.put("order_no", orderNo);// 8-20 , , chargeMap.put("channel", channel);// , :https://www.pingxx.com/api#api-c-new chargeMap.put("client_ip", ip); // IP IPV4: 127.0.0.1 Map, String> app = new HashMap, String>(); app.put("id", appId); chargeMap.put("app", app); if (extra == null) extra = new HashMap, Object>(); chargeMap.put("extra", extra); if (initialMetadata == null) initialMetadata = new HashMap, Object>(); // initialMetadata.put("color", "red"); chargeMap.put("metadata", initialMetadata); try { // charge = Charge.create(chargeMap); // .toString(), JSON String chargeString = charge.toString(); System.out.println(chargeString); } catch (APIConnectionException e) { e.printStackTrace(); } catch (ChannelException e) { e.printStackTrace(); } catch (RateLimitException e) { e.printStackTrace(); } catch (AuthenticationException e) { e.printStackTrace(); } catch (APIException e) { e.printStackTrace(); } catch (InvalidRequestException e) { e.printStackTrace(); } return charge; } public Charge createChargeForWx(int amount, String subject, String body, String channel, String ip, String orderNo, Map, Object> extra, Map, Object> initialMetadata) { Charge charge = null; Map, Object> chargeMap = new HashMap, Object>(); chargeMap.put("amount", amount);// , : ( 1 100 chargeMap.put("currency", "cny"); chargeMap.put("subject", subject); chargeMap.put("body", body); // String orderNo = new Date().getTime() + PayUtil.randomString(7); chargeMap.put("order_no", orderNo);// 8-20 , , chargeMap.put("channel", channel);// , :https://www.pingxx.com/api#api-c-new chargeMap.put("client_ip", ip); // IP IPV4: 127.0.0.1 Map, String> app = new HashMap, String>(); app.put("id", appIdceshi); chargeMap.put("app", app); if (extra == null) extra = new HashMap, Object>(); chargeMap.put("extra", extra); if (initialMetadata == null) initialMetadata = new HashMap, Object>(); chargeMap.put("metadata", initialMetadata); try { // charge = Charge.create(chargeMap); System.out.println(charge); // .toString(), JSON String chargeString = charge.toString(); System.out.println(chargeString); } catch (APIConnectionException e) { e.printStackTrace(); } catch (ChannelException e) { e.printStackTrace(); } catch (RateLimitException e) { e.printStackTrace(); } catch (AuthenticationException e) { e.printStackTrace(); } catch (APIException e) { e.printStackTrace(); } catch (InvalidRequestException e) { e.printStackTrace(); } return charge; } /** * * * charge , charge.getRefunds().create(); https://www.pingxx.com/api#api-r-new * , 。 * * @param amount , , : ( 1 100)。 , * @param chargeId * @param description * @return Refund json */ // { // "id": "re_y1u944PmfnrTHyvnL0nD0iD1", // "object": "refund", // "order_no": "y1u944PmfnrTHyvnL0nD0iD1", // "amount": 9, // "created": 1409634160, // "succeed": true, // "status": "succeeded", // "time_succeed": 1409634192, // "description": "Refund Description", // "failure_code": null, // "failure_msg": null, // "metadata": {}, // "charge": "ch_L8qn10mLmr1GS8e5OODmHaL4", // "charge_order_no": "123456789", // "transaction_no": "2004450349201512090096425284" // } public Refund refund(Integer amount, String chargeId, String description) { if (chargeId == null) { return null; } Refund refund = null; Map, Object> params = new HashMap, Object>(); params.put("description", description); params.put("amount", amount);// , , : ( 1 100)。 , try { refund = Refund.create(chargeId, params); System.out.println(refund); } catch (AuthenticationException e) { e.printStackTrace(); } catch (InvalidRequestException e) { e.printStackTrace(); } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIException e) { e.printStackTrace(); } catch (ChannelException e) { e.printStackTrace(); } catch (RateLimitException e) { e.printStackTrace(); } return refund; } /** * * * Id charge。 :https://www.pingxx.com/api#api-r-inquiry * * @param refundid ID * @param chargeId * @return json */ public String retrieve(String refundid, String chargeId) { if (chargeId == null) { return null; } Refund refund = null; try { refund = Refund.retrieve(chargeId, refundid); System.out.println(refund); } catch (AuthenticationException e) { e.printStackTrace(); } catch (InvalidRequestException e) { e.printStackTrace(); } catch (APIConnectionException e) { e.printStackTrace(); } catch (APIException e) { e.printStackTrace(); } catch (ChannelException e) { e.printStackTrace(); } catch (RateLimitException e) { e.printStackTrace(); } return refund.toString(); } /** * * * @return * @throws Exception */ public PublicKey getPubKey() throws Exception { // String pubKeyString = pubKey.replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\
|-+END PUBLIC KEY-+\\r?\
?)", "");
String pubKeyString = pubKeyTest.replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", ""); byte[] keyBytes = Base64.decode(pubKeyString); // generate public key X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(spec); return publicKey; } /** * * * @param dataString * @param signatureString * @param publicKey * @return * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws SignatureException */ public boolean verifyData(String dataString, String signatureString, PublicKey publicKey) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException { byte[] signatureBytes = Base64.decode(signatureString); Signature signature = Signature.getInstance("SHA256withRSA"); signature.initVerify(publicKey); signature.update(dataString.getBytes("UTF-8")); return signature.verify(signatureBytes); } private static SecureRandom random = new SecureRandom(); public static String randomString(int length) { String str = new BigInteger(130, random).toString(32); return str.substring(0, length); } public static int currentTimeSeconds() { return (int) (System.currentTimeMillis() / 1000); } public static void main(String[] args) { } }

좋은 웹페이지 즐겨찾기