The below capability will allow a merchant to fetch the user location by calling the below method. A merchant can import the PhonePe Switch JS SDK to call the below method. The JS SDK can be imported using the npm package as mentioned here.

getCurrentLocation()

Description: Call this method to get the current location of the user

Usage

//Get the location permission from PhonePe first
//See the Ask for Permissions section above to see the structure of res object
let result = await sdk.seekPermission([PhonePe.Constants.Permission.LOCATION])

//If the permission has been given for location
//Then get the current location
//Else getCurrentLocation() would reject
sdk.getCurrentLocation()
  .then((location) => { 
    console.log("Location received"+ String ) 
  })
  .catch((err) => { 
    console.log("Error found when fetching location = " + err) 
  })

Response Parameters

Promise<any>
A promise of getting the current location. Promise rejection will contain the error message if we were unable to fetch the location.

The promise rejection reason can be as follows:

Code

Description

LOCATION_FETCH_FAILURE

Unable to fetch user's location

Promise resolve:
{
  String : which needs to be JSON.PARSE by merchant to fetch the below details. 
  'latitude': 123.123, 
  'longitude': 345.345
}

Promise reject:
{
  'error_code': 'LOCATION_FETCH_FAILURE'
}