Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create callback endpoint #111

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public InitiatePaymentOutput initiatePayment(@Argument InitiatePaymentInput inpu
return output;
}

/*
Calls PhonePe Pay API using sdk
https://developer.phonepe.com/v1/reference/pay-api-1
https://developer.phonepe.com/v1/reference/java-standard-pay-page
*/
private String initiate() {
String merchantId = "M1FPXNBFIGDG";
String saltKey = "e7a5693b-0ccf-47ac-a258-c5d77e701e2c";
Expand All @@ -42,14 +47,14 @@ private String initiate() {

String merchantTransactionId = UUID.randomUUID().toString().substring(0,34);
long amount = 200;
String callbackurl = "https://www.merchant.com/callback";
String callbackUrl = "https://service.flickmatch.in:8443/platform-0.0.1-SNAPSHOT/payment";
String merchantUserId = "merchantUserId";

PgPayRequest pgPayRequest = PgPayRequest.PayPagePayRequestBuilder()
.amount(amount)
.merchantId(merchantId)
.merchantTransactionId(merchantTransactionId)
.callbackUrl(callbackurl)
.callbackUrl(callbackUrl)
.callbackMode("REDIRECT")
.redirectUrl("https://play.flickmatch.in/")
.redirectMode("REDIRECT")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.flickmatch.platform.rest;

import lombok.Data;

@Data
public class CallBackResponse {
private String response;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.flickmatch.platform.rest;

import lombok.extern.log4j.Log4j2;
import org.springframework.boot.json.GsonJsonParser;
import org.springframework.boot.json.JacksonJsonParser;
import org.springframework.boot.json.JsonParser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;
import java.util.Map;

/**
* Exposes POST endpoint for phonepe callback.
* https://developer.phonepe.com/v1/reference/server-to-server-callback-5
*/
@RestController
@Log4j2
public class PhonePeCallBackController {

private static final JsonParser jsonParser = new JacksonJsonParser();

@PostMapping("/payment")
void processCallBack(@RequestBody CallBackResponse callBackResponse,
@RequestHeader("x_verify") String xVerify) {
log.info(xVerify); //We need to use this to verify callback https://developer.phonepe.com/v1/reference/java-callback-verification
String decodedResponse = new String(Base64.getDecoder().decode(callBackResponse.getResponse()));
Map<String, Object> phonePeResponse = jsonParser.parseMap(decodedResponse);
if ("PAYMENT_SUCCESS".equals(phonePeResponse.get("code"))) {
log.info("store data in DB");
} else {
log.error(decodedResponse);
}
}
}
Loading