|
| 1 | +package example; |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import com.pingplusplus.Pingpp; |
| 6 | +import com.pingplusplus.exception.APIConnectionException; |
| 7 | +import com.pingplusplus.exception.APIException; |
| 8 | +import com.pingplusplus.exception.AuthenticationException; |
| 9 | +import com.pingplusplus.exception.InvalidRequestException; |
| 10 | +import com.pingplusplus.model.App; |
| 11 | +import com.pingplusplus.model.Transfer; |
| 12 | +import com.pingplusplus.model.TransferCollection; |
| 13 | + |
| 14 | +import java.text.SimpleDateFormat; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Date; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +/** |
| 22 | + * Created by sunkai on 15/5/11. |
| 23 | + */ |
| 24 | +public class TransferExample { |
| 25 | + |
| 26 | + /** |
| 27 | + * pingpp 管理平台对应的API key |
| 28 | + */ |
| 29 | + public static String apiKey = "YOUR-KEY"; |
| 30 | + /** |
| 31 | + * pingpp 管理平台对应的应用ID |
| 32 | + */ |
| 33 | + public static String appId = "YOUR-APPID"; |
| 34 | + |
| 35 | + public static void main(String args[]) { |
| 36 | + |
| 37 | + Pingpp.apiKey = apiKey; |
| 38 | + TransferExample transferExample = new TransferExample(); |
| 39 | + System.out.println("---------创建Transfer"); |
| 40 | + Transfer transfer = transferExample.transfer(); |
| 41 | + System.out.println("---------查询Transfer"); |
| 42 | + transferExample.retrieve(transfer.getId()); |
| 43 | + System.out.println("---------查询Transfer列表"); |
| 44 | + transferExample.all(); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * 创建企业转账 |
| 50 | + * @return |
| 51 | + */ |
| 52 | + public Transfer transfer() { |
| 53 | + Transfer transfer = null; |
| 54 | + String orderNo = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()); |
| 55 | + Map<String, Object> transferMap = new HashMap<String, Object>(); |
| 56 | + transferMap.put("channel", "wx_pub"); |
| 57 | + transferMap.put("order_no", orderNo); |
| 58 | + transferMap.put("amount", "10"); |
| 59 | + transferMap.put("type", "b2c"); |
| 60 | + transferMap.put("currency", "cny"); |
| 61 | + transferMap.put("recipient", "o9zpMs5MW2-62GAy5hRrjdYVCktU"); |
| 62 | + transferMap.put("description", "your description"); |
| 63 | + Map<String, String> app = new HashMap<String, String>(); |
| 64 | + app.put("id", appId); |
| 65 | + transferMap.put("app", app); |
| 66 | + |
| 67 | + try { |
| 68 | + transfer = Transfer.create(transferMap); |
| 69 | + System.out.println(transfer); |
| 70 | + } catch (AuthenticationException e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } catch (InvalidRequestException e) { |
| 73 | + e.printStackTrace(); |
| 74 | + } catch (APIConnectionException e) { |
| 75 | + e.printStackTrace(); |
| 76 | + } catch (APIException e) { |
| 77 | + e.printStackTrace(); |
| 78 | + } |
| 79 | + return transfer; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * 根据 ID 查询 |
| 84 | + * @param id |
| 85 | + */ |
| 86 | + public void retrieve(String id) { |
| 87 | + Map<String, Object> param = new HashMap<String, Object>(); |
| 88 | + List<String> expande = new ArrayList<String>(); |
| 89 | + expande.add("app"); |
| 90 | + param.put("expand", expande); |
| 91 | + try { |
| 92 | + Transfer transfer = Transfer.retrieve(id, param); |
| 93 | + //Transfer transfer = Transfer.retrieve(id); |
| 94 | + //if you expand app properties transfer.getApp() will return a App Object. |
| 95 | + if (transfer.getApp() instanceof App) { |
| 96 | + App app = (App) transfer.getApp(); |
| 97 | + System.out.println("App Object ,appId = " + app.getId()); |
| 98 | + } else { |
| 99 | + System.out.println("String ,appId = " + transfer.getApp()); |
| 100 | + } |
| 101 | + System.out.println(transfer); |
| 102 | + } catch (AuthenticationException e) { |
| 103 | + e.printStackTrace(); |
| 104 | + } catch (InvalidRequestException e) { |
| 105 | + e.printStackTrace(); |
| 106 | + } catch (APIConnectionException e) { |
| 107 | + e.printStackTrace(); |
| 108 | + } catch (APIException e) { |
| 109 | + e.printStackTrace(); |
| 110 | + } |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + *批量查询 |
| 116 | + */ |
| 117 | + public void all() { |
| 118 | + Map<String, Object> parm = new HashMap<String, Object>(); |
| 119 | + parm.put("limit", 3); |
| 120 | +// List<String> expande = new ArrayList<String>(); |
| 121 | +// expande.add("app"); |
| 122 | +// parm.put("expand", expande); |
| 123 | + |
| 124 | + try { |
| 125 | + TransferCollection transferCollection = Transfer.all(parm); |
| 126 | + System.out.println(transferCollection); |
| 127 | + } catch (AuthenticationException e) { |
| 128 | + e.printStackTrace(); |
| 129 | + } catch (InvalidRequestException e) { |
| 130 | + e.printStackTrace(); |
| 131 | + } catch (APIConnectionException e) { |
| 132 | + e.printStackTrace(); |
| 133 | + } catch (APIException e) { |
| 134 | + e.printStackTrace(); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments