J2Pay –完整示例
介紹
在本節(jié)中,我們將詳細探討如何使用網(wǎng)關(guān)并成功調(diào)用所有四種方法,即購買,退款,作廢和重新計費。
對于此示例,我們將使用授權(quán)網(wǎng)關(guān)。 讓我們開始。
首先,我們將獲得Authorize網(wǎng)關(guān)對象。
Gateway gateway = GatewayFactory.getGateway(AvailableGateways.AUTHORIZE);但是,如果您想動態(tài)獲取授權(quán)網(wǎng)關(guān),例如從數(shù)據(jù)庫中獲取其名稱,該怎么辦。
這是您可以執(zhí)行的操作。
Gateway gateway = GatewayFactory.getGateway(AvailableGateways.valueOf("AUTHORIZE"));知道您可以了解如何獲取所需網(wǎng)關(guān)對象的兩種方法。
由于我們在測試環(huán)境中工作,因此第二件事就是啟用測試模式。
gateway.setTestMode(true);注意:測試模式僅在網(wǎng)關(guān)支持的情況下才起作用,否則它將被庫忽略。
接下來最重要的是API參數(shù),這些是我的商家服務(wù)提供商提供的唯一值,即API用戶名和密碼,必須包含在所有請求中,并且對于所有網(wǎng)關(guān)而言,它們始終是不同的。
由于我們使用的是J2pay,因此無需閱讀任何文檔即可授權(quán)網(wǎng)關(guān)變量。
這是您將使用樣本參數(shù)方法的位置(請參閱樣本參數(shù)部分)
這是您將如何做的。
JSONObject apiSampleParameters = gateway.getApiSampleParameters();現(xiàn)在我們將打印它以查看參數(shù)是什么。
JSONObject apiSampleParameters = gateway.getApiSampleParameters();System.out.println(apiSampleParameters);//output{ "name" : "also called api user name / api login id", "transactionKey" : "the transaction key" }如您所見,對于Authorize API,參數(shù)是name和transactionKey。 我們將填充這些值并傳遞給購買方法。
apiSampleParameters.put("name", "<your acount's user name here>"); apiSampleParameters.put("transactionKey", "<your account's transaction key here>");采購
購買方法需要五個參數(shù)。
我們已經(jīng)在上面設(shè)置了apiParameters。
現(xiàn)在創(chuàng)建客戶和客戶卡對象。
注意:客戶和客戶卡類支持鏈設(shè)置器方法,并且下面使用的所有字段都是必需的。
Customer customer = new Customer();customer.setFirstName("test first name").setLastName("test last name").setCountry(Country.US).setState("TX").setCity("test city").setAddress("test address").setZip("12345").setPhoneNumber("1234567890").setEmail("email@domain.com").setIp("127.0.0.1");CustomerCard customerCard = new CustomerCard();customerCard.setName("test card name").setNumber("5424000000000015").setCvv(123).setExpiryMonth("01").setExpiryYear("2022");注意:第4和第5參數(shù)不需要任何解釋。
現(xiàn)在所有參數(shù)都準(zhǔn)備好了,我們可以將它們傳遞給購買方法
HTTPResponse response = gateway.purchase(apiSampleParameters, customer, customerCard, Currency.USD, 45);您可以通過調(diào)用isSuccessful方法來檢查購買請求的狀態(tài),還可以通過調(diào)用getJSONResponse方法來獲取JSON響應(yīng)。
response.isSuccessful();response.getJSONResponse();讓我們將所有代碼放在一起。
Gateway gateway = GatewayFactory.getGateway(AvailableGateways.AUTHORIZE);JSONObject apiSampleParameters = gateway.getApiSampleParameters();apiSampleParameters.put("name", "");apiSampleParameters.put("transactionKey", "");Customer customer = new Customer();customer.setFirstName("test first name").setLastName("test last name").setCountry(Country.US).setState("TX").setCity("test city").setAddress("test address").setZip("12345").setPhoneNumber("1234567890");CustomerCard customerCard = new CustomerCard();customerCard.setName("test card name").setNumber("5424000000000015").setCvv(123).setExpiryMonth("01").setExpiryYear("2022");gateway.setTestMode(true);HTTPResponse response = gateway.purchase(apiSampleParameters, customer, customerCard, Currency.USD, 45);System.out.println (response.isSuccessful());System.out.println (response.getJSONResponse());讓我們看看收到的回復(fù)。 考慮我們將響應(yīng)保存在響應(yīng)變量中。
JSONObject response = response.getJSONResponse();打印響應(yīng)后,這就是我們得到的。
{"lr": {"amount": 2.5,"cardExpiryYear": "2017","message": "This transaction has been approved.","cardFirst6": "542400","cardExpiryMonth": "12","transactionId": "60036012175","maskedCard": "542400******0015","rebillParams": {"customerProfileId": "1813844918","paymentProfileId": "1808509554"},"success": true,"voidParams": {"transactionId": "60036012175"},"currencyCode": "USD","cardLast4": "0015","refundParams": {"transactionId": "60036012175","cardLast4": "0015"}},"gr": { //long gateway response }}如您所見,對于進一步的交易(如退款,作廢或重新開票),圖書館本身創(chuàng)建了必需的參數(shù)
重新開票
"rebillParams": {"customerProfileId": "1813844918","paymentProfileId": "1808509554"},虛無
"voidParams": {"transactionId": "60036012175"},退款
"refundParams": {"transactionId": "60036012175","cardLast4": "0015"}注意:您可以將這些參數(shù)保存在數(shù)據(jù)庫中,并將它們傳遞給合適的方法。
重新開票
對于重新計費,我們將調(diào)用getRebillSampleParameters方法。
JSONObject rebillSampleParameters = gateway.getRebillSampleParameters();打印后,您將看到。
{"customerProfileId":"the customer profile id","paymentProfileId":"the customer payment profile id"}如果將其與上面的購買響應(yīng)rebillParams密鑰相匹配,您將看到實際上沒有任何區(qū)別。 購買響應(yīng)已經(jīng)包含這些參數(shù)以及填充的值。
因此,我們不會像上面的getApiSampleParameters那樣創(chuàng)建它們,但是如果您尚未從該庫中執(zhí)行購買交易,則可以使用第二個選項來創(chuàng)建這些參數(shù)并將它們傳遞給rebill方法。 下面我們描述了兩種方法,因此您可以使用更適合自己的方法。
第一種方法
這種方法是快速前進的。 我們將使用庫生成的參數(shù)(rebillParams)。
由于重新開票方法需要三個參數(shù)
我們已經(jīng)討論了apiParameters,只是提醒您我們將網(wǎng)關(guān)對象保存在網(wǎng)關(guān)變量中,并將購買響應(yīng)保存在響應(yīng)變量中。
這是我們可以輕松調(diào)用rebill方法的方法。
JSONObject rebillParams = response.getJSONObject("lr").getJSONObject("rebillParams")HTTPResponse rebillResponse = gateway.rebill(apiSampleParameters, rebillParams, 105);僅僅兩行就不是那么簡單嗎?
第二種方法
第二種方法與我們創(chuàng)建的apiParameters類似。
JSONObject rebillParams = gateway.getRebillSampleParameters();打印rebillParams之后,我們得到了。
System.out.println(rebillParams);//output{"customerProfileId":"the customer profile id","paymentProfileId":"the customer payment profile id"}現(xiàn)在,我們將填充這些值。
rebillParams.put("customerProfileId", "1813844918");rebillParams.put("paymentProfileId", "1808509554");現(xiàn)在我們可以調(diào)用rebill方法。
HTTPResponse rebillResponse = gateway.rebill(apiSampleParameters, rebillParams, 105);如上所示,您可以調(diào)用rebillResponse。 getJSONResponse()方法獲取響應(yīng)。 您還可以通過調(diào)用rebillResponse.isSuccessful()方法來檢查事務(wù)是否成功。
您還可以注意到這兩種方法都非常簡單,可以隨意使用更適合自己的方法,但是建議您使用第一種方法,因為這也非常簡單,并且排除了任何可能的錯誤。
注意:在本示例的其余部分,我們將使用第一種方法。
退款
退款方式需要三個參數(shù)
這與退款非常相似。 這就是我們稱為退款方式的方式。
JSONObject refundParams = response.getJSONObject("lr").getJSONObject("refundParams")HTTPResponse refundResponse = gateway.refund(apiSampleParameters, refundParams, 2.5);注意:其余工作將保持不變returnResponse包含實際的響應(yīng)。
空洞
voidTransaction方法需要兩個參數(shù)。
下面是示例代碼。
JSONObject voidParams= response.getJSONObject("lr").getJSONObject("voidParams")HTTPResponse voidResponse = gateway.voidTransaction (apiSampleParameters, voidParams);注意:其余工作將保持不變voidResponse包含實際的響應(yīng)。
祝賀您完成示例。 您已經(jīng)完全了解該庫。
翻譯自: https://www.javacodegeeks.com/2018/11/j2pay-complete-example.html
總結(jié)
以上是生活随笔為你收集整理的J2Pay –完整示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oauth2和jwt_使用具有OAuth
- 下一篇: gddr6显卡是独立显卡吗(gddr6和