|
| 1 | +package com.alipay.global.api.example; |
| 2 | + |
| 3 | +import com.alibaba.fastjson.JSONObject; |
| 4 | +import com.alipay.global.api.AlipayClient; |
| 5 | +import com.alipay.global.api.DefaultAlipayClient; |
| 6 | +import com.alipay.global.api.exception.AlipayApiException; |
| 7 | +import com.alipay.global.api.model.ams.*; |
| 8 | +import com.alipay.global.api.model.constants.EndPointConstants; |
| 9 | +import com.alipay.global.api.request.ams.pay.AlipayPayConsultRequest; |
| 10 | +import com.alipay.global.api.request.ams.pay.AlipayPayRequest; |
| 11 | +import com.alipay.global.api.response.ams.pay.AlipayPayConsultResponse; |
| 12 | +import com.alipay.global.api.response.ams.pay.AlipayPayResponse; |
| 13 | + |
| 14 | +import java.util.UUID; |
| 15 | + |
| 16 | +public class IsvPayDemo { |
| 17 | + |
| 18 | + /** |
| 19 | + * replace with your client id. |
| 20 | + * find your client id here: <a href="https://dashboard.alipay.com/global-payments/developers/quickStart">quickStart</a> |
| 21 | + */ |
| 22 | + public static final String CLIENT_ID = ""; |
| 23 | + |
| 24 | + /** |
| 25 | + * replace with your antom public key (used to verify signature). |
| 26 | + * find your antom public key here: <a href="https://dashboard.alipay.com/global-payments/developers/quickStart">quickStart</a> |
| 27 | + */ |
| 28 | + public static final String ANTOM_PUBLIC_KEY = ""; |
| 29 | + |
| 30 | + /** |
| 31 | + * replace with your private key (used to sign). |
| 32 | + * please ensure the secure storage of your private key to prevent leakage |
| 33 | + */ |
| 34 | + public static final String MERCHANT_PRIVATE_KEY = ""; |
| 35 | + |
| 36 | + /** |
| 37 | + * ISV agentToken |
| 38 | + */ |
| 39 | + public static final String AGENT_TOKEN = ""; |
| 40 | + |
| 41 | + /** |
| 42 | + * please replace with your endpoint. |
| 43 | + * find your endpoint here: <a href="https://dashboard.alipay.com/global-payments/developers/quickStart">quickStart</a> |
| 44 | + */ |
| 45 | + private final static AlipayClient CLIENT = new DefaultAlipayClient( |
| 46 | + EndPointConstants.SG, MERCHANT_PRIVATE_KEY, ANTOM_PUBLIC_KEY, CLIENT_ID, AGENT_TOKEN); |
| 47 | + |
| 48 | + public static void main(String[] args) { |
| 49 | + executeConsult(); |
| 50 | +// executePayWithBlik(); |
| 51 | + } |
| 52 | + |
| 53 | + public static void executeConsult() { |
| 54 | + AlipayPayConsultRequest alipayPayConsultRequest = new AlipayPayConsultRequest(); |
| 55 | + alipayPayConsultRequest.setProductCode(ProductCodeType.CASHIER_PAYMENT); |
| 56 | + |
| 57 | + // set amount |
| 58 | + Amount amount = Amount.builder().value("4200").currency("BRL").build(); |
| 59 | + alipayPayConsultRequest.setPaymentAmount(amount); |
| 60 | + |
| 61 | + // set env Info |
| 62 | + Env env = Env.builder().terminalType(TerminalType.WEB).build(); |
| 63 | + alipayPayConsultRequest.setEnv(env); |
| 64 | + |
| 65 | + AlipayPayConsultResponse alipayPayConsultResponse = null; |
| 66 | + |
| 67 | + try { |
| 68 | + alipayPayConsultResponse = CLIENT.execute(alipayPayConsultRequest); |
| 69 | + System.out.println(JSONObject.toJSON(alipayPayConsultResponse)); |
| 70 | + } catch (AlipayApiException e) { |
| 71 | + String errorMsg = e.getMessage(); |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * show how to finish a payment by Blik |
| 78 | + */ |
| 79 | + public static void executePayWithBlik() { |
| 80 | + AlipayPayRequest alipayPayRequest = new AlipayPayRequest(); |
| 81 | + alipayPayRequest.setProductCode(ProductCodeType.CASHIER_PAYMENT); |
| 82 | + |
| 83 | + // replace with your paymentRequestId |
| 84 | + String paymentRequestId = UUID.randomUUID().toString(); |
| 85 | + alipayPayRequest.setPaymentRequestId(paymentRequestId); |
| 86 | + |
| 87 | + // set amount |
| 88 | + Amount amount = Amount.builder().value("4200").currency("PLN").build(); |
| 89 | + alipayPayRequest.setPaymentAmount(amount); |
| 90 | + |
| 91 | + // set paymentMethod |
| 92 | + PaymentMethod paymentMethod = PaymentMethod.builder().paymentMethodType("BLIK").build(); |
| 93 | + alipayPayRequest.setPaymentMethod(paymentMethod); |
| 94 | + |
| 95 | + // replace with your orderId |
| 96 | + String orderId = UUID.randomUUID().toString(); |
| 97 | + |
| 98 | + // set buyer info |
| 99 | + Buyer buyer = Buyer.builder().referenceBuyerId("yourBuyerId").build(); |
| 100 | + |
| 101 | + // set order info |
| 102 | + Order order = Order.builder().referenceOrderId(orderId) |
| 103 | + .orderDescription("antom testing order").orderAmount(amount).buyer(buyer).build(); |
| 104 | + alipayPayRequest.setOrder(order); |
| 105 | + |
| 106 | + // set env info |
| 107 | + Env env = Env.builder().terminalType(TerminalType.WEB) |
| 108 | + .clientIp("1.2.3.4").build(); |
| 109 | + alipayPayRequest.setEnv(env); |
| 110 | + |
| 111 | + // replace with your notify url |
| 112 | + alipayPayRequest.setPaymentNotifyUrl("http://www.yourNotifyUrl.com"); |
| 113 | + |
| 114 | + // replace with your redirect url |
| 115 | + alipayPayRequest.setPaymentRedirectUrl("http://www.yourRedirectUrl.com"); |
| 116 | + |
| 117 | + // do payment |
| 118 | + AlipayPayResponse alipayPayResponse = null; |
| 119 | + try { |
| 120 | + alipayPayResponse = CLIENT.execute(alipayPayRequest); |
| 121 | + System.out.println(JSONObject.toJSON(alipayPayResponse)); |
| 122 | + } catch (AlipayApiException e) { |
| 123 | + String errorMsg = e.getMessage(); |
| 124 | + System.out.println(errorMsg); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + |
| 129 | +} |
0 commit comments