<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by dhaval mashruwala on Medium]]></title>
        <description><![CDATA[Stories by dhaval mashruwala on Medium]]></description>
        <link>https://medium.com/@er.dhavi777?source=rss-6bc8d4976b3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*wj-NhYdctJ50p9BG.</url>
            <title>Stories by dhaval mashruwala on Medium</title>
            <link>https://medium.com/@er.dhavi777?source=rss-6bc8d4976b3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 14:12:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@er.dhavi777/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to integrate UPI Payment feature in react-native app (android)?]]></title>
            <link>https://medium.com/@er.dhavi777/how-to-integrate-upi-payment-feature-in-react-native-app-android-a7577458a7b7?source=rss-6bc8d4976b3------2</link>
            <guid isPermaLink="false">https://medium.com/p/a7577458a7b7</guid>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[dhaval mashruwala]]></dc:creator>
            <pubDate>Thu, 05 Jul 2018 06:47:08 GMT</pubDate>
            <atom:updated>2018-07-05T06:47:08.376Z</atom:updated>
            <content:encoded><![CDATA[<p>Before I start, just to let you know that there exist no (at least not till 14th june, 2018!) direct module which we can install using npm/yarn or any other package module tool that ease integration of UPI payments in react native app. It took me about a week of trial error, reading boring docs/ blogs and native reference to get things going smoothly. Hence, I decided to write this blog to save time for my fellow react native developer. Before you follow this blog, I recommend you to go through <a href="https://www.npci.org.in/sites/all/themes/npcl/images/PDF/UPI_Linking_Specs_ver_1.5.1.pdf">https://www.npci.org.in/sites/all/themes/npcl/images/PDF/UPI_Linking_Specs_ver_1.5.1.pdf</a> for detail documentation about working of UPI payments.</p><p>There are many ways to implement UPI but it will walk you through how UPI works with <strong>hyperlinks. </strong>A typical UPI hyperlink looks like this: -</p><p>upi://pay?pa=someName@okicici&amp;pn=dhaval&amp;tr=kdahskjahs27575fsdfasdas&amp;am=10&amp;mam=null&amp;cu=INR</p><p>Consider app with one text-input and one button (as per image given below). Our goal is to make payment on click of button and receive response as per transaction status.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*OaQ-a-Yi41tA8wzV5SBFDA.png" /></figure><p>For now the view file, App.js, will look something like this.</p><pre>import React, {Component} from &#39;react&#39;;</pre><pre>import {Platform, StyleSheet, Text, View, TextInput, TouchableOpacity} from &#39;react-native&#39;;</pre><pre>export default class App extends Component {</pre><pre>constructor(props){</pre><pre>  super(props);</pre><pre>  this.state = {};</pre><pre>}</pre><pre>render() {</pre><pre>  return (</pre><pre>  &lt;View style={styles.container}&gt;</pre><pre>    &lt;TextInput</pre><pre>    placeholder={&#39;Amount&#39;}</pre><pre>    style={styles.TextInput}</pre><pre>    onChangeText={(amount) =&gt; this.setState({amount})}</pre><pre>    value={this.state.amount}</pre><pre>  /&gt;</pre><pre>  &lt;TouchableOpacity style={styles.paymentButton} onPress={()=&gt;{}}&gt;</pre><pre>    &lt;Text style={{color:&#39;white&#39;}}&gt;Make Payment&lt;/Text&gt;</pre><pre>  &lt;/TouchableOpacity&gt;</pre><pre>&lt;/View&gt;</pre><pre>);<br>}</pre><pre>}</pre><pre>const styles = StyleSheet.create({</pre><pre>container: {paddingVertical:150,flex: 1,justifyContent: &#39;space-around&#39;,alignItems: &#39;center&#39;,backgroundColor: &#39;#F5FCFF&#39;},</pre><pre>TextInput:{width:&#39;80%&#39;,height: 40, borderColor: &#39;gray&#39;, borderWidth: 1},</pre><pre>paymentButton:{width:200,height:50,borderRadius:10,alignItems:&#39;center&#39;,justifyContent:&#39;center&#39;,backgroundColor:&#39;red&#39;},</pre><pre>});</pre><p>For successful integration of UPI payments, we need to walk extra mile by writing the native android as alone JS code is not enough to implement this feature.Later we will bridge the native code and JS with ‘NativeModules’</p><p>Go to AndroidManifest.xml which you should be able to find in ‘android’ folder of your project —let say, android&gt;app&gt;src&gt;main&gt;AndroidManifest.xml and following line under &lt;activity&gt; tag:</p><pre>&lt;intent-filter android:label=&quot;MyUPIApp&quot;&gt;</pre><pre>  &lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;</pre><pre>  &lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;</pre><pre>  &lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;</pre><pre>  &lt;data android:scheme=&quot;https&quot; android:host=&quot;MyUPIApp&quot; /&gt;</pre><pre>&lt;/intent-filter&gt;</pre><p>Add new Java file at same level of MainApplication.java called UPIModule.java and add following code:-</p><pre>package &lt;package-name&gt;;</pre><pre>import android.app.Activity;<br>import android.content.Intent;<br>import android.net.Uri;<br>import android.util.Log;<br>import android.widget.Toast;</pre><pre>import com.facebook.react.bridge.ActivityEventListener;<br>import com.facebook.react.bridge.BaseActivityEventListener;<br>import com.facebook.react.bridge.ReactApplicationContext;<br>import com.facebook.react.bridge.ReactContextBaseJavaModule;<br>import com.facebook.react.bridge.ReactMethod;<br>import com.facebook.react.bridge.Promise;</pre><pre>import static android.content.ContentValues.TAG;</pre><pre>public class UPIModule extends ReactContextBaseJavaModule {</pre><pre>private Promise promiseUpi;</pre><pre>private final ActivityEventListener mActivityEventListener = new BaseActivityEventListener() {<br>  //listener for activity<br>  <a href="http://twitter.com/Override">@Override</a><br>  public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {<br>   Log.d(TAG, &quot;onActivityResult: requestCode: &quot; + requestCode);<br>   Log.d(TAG, &quot;onActivityResult: resultCode: &quot; + resultCode);<br>   if (promiseUpi != null) {<br>    if (data != null) {<br>     String res = data.getStringExtra(&quot;response&quot;);<br>     promiseUpi.resolve(res);<br>    } else {<br>     promiseUpi.reject(&quot;Failed&quot;, data.toString());<br>    }<br>   }<br>  }<br> };</pre><pre>public UPIModule(ReactApplicationContext reactContext) {<br>  super(reactContext);<br>  // register the listener for `onActivityResult`<br>  reactContext.addActivityEventListener(mActivityEventListener);<br> }</pre><pre><a href="http://twitter.com/Override">@Override</a><br> public String getName() {<br>  return &quot;UPI&quot;; //this will be name of key we can fetch from NativeModules to use this module<br> }</pre><pre>//this will be method called back javascript via NativeModules<br> <a href="http://twitter.com/ReactMethod">@ReactMethod</a><br> public void openLink(String url, final Promise promise) {<br>  Activity currentActivity = getCurrentActivity();<br>  promiseUpi = promise;<br>  Uri uri = Uri.parse(url);<br>  Intent intent = new Intent(Intent.ACTION_VIEW, uri);<br>  //start activity with mentioned intent and as we need result we will use startActivityForResult<br>  currentActivity.startActivityForResult(intent, 1);<br> }</pre><pre>}</pre><p>We have our code ready for module. Now we need ReactPackage to add this module we made to NativeModule list. For that, add Java file again at same level of MainApplication.java called UPIPackge.java and add following code:-</p><pre>package &lt;package-name&gt;;</pre><pre>import com.facebook.react.ReactPackage;<br>import com.facebook.react.bridge.NativeModule;<br>import com.facebook.react.bridge.ReactApplicationContext;<br>import com.facebook.react.uimanager.ViewManager;</pre><pre>import java.util.ArrayList;<br>import java.util.Collections;<br>import java.util.List;</pre><pre>public class UPIPackage implements ReactPackage {</pre><pre><a href="http://twitter.com/Override">@Override</a><br> public List &lt; ViewManager &gt; createViewManagers(ReactApplicationContext reactContext) {<br>  return Collections.emptyList();<br> }</pre><pre><a href="http://twitter.com/Override">@Override</a><br> public List &lt; NativeModule &gt; createNativeModules(<br>  ReactApplicationContext reactContext) {<br>  List &lt; NativeModule &gt; modules = new ArrayList &lt; &gt; ();</pre><pre>modules.add(new UPIModule(reactContext));</pre><pre>return modules;<br> }</pre><pre>}</pre><p>final nail in the coffin on native side is to add this UPIPackage as one of the react-native package to be used in our app. To do so, add following line in MainApplication.java in ‘getPackages()’ method with other packages:-</p><pre><a href="http://twitter.com/Override">@Override</a><br>protected List &lt; ReactPackage &gt; getPackages() {<br> return Arrays. &lt; ReactPackage &gt; asList(<br>  ...,<br>  new UPIPackage(),<br>  ...</pre><pre>);<br>}</pre><p>Now Open your View file (App.js) and import ‘NativeModules’ module:-</p><pre>import {NativeModules} from &#39;react-native&#39;;<br>const UPI = NativeModules.UPI; // &#39;UPI&#39; was module name given</pre><p>Now all we need to do is write a method to open UPI hyperlink on press on button, which we can achieved by following:-</p><pre>....<br>openLink = async ()=&gt;{</pre><pre>let {amount} = this.state;</pre><pre>let UpiUrl = &quot;upi://pay?pa=someName@okicici&amp;pn=dhaval&amp;tr=kdahskjahs27575fsdfasdas&amp;am=&quot;+amount+&quot;&amp;mam=null&amp;cu=INR&amp;url=https://MyUPIApp&amp;refUrl=https://MyUPIApp&quot;;</pre><pre>let response = await UPI.openLink(UpiUrl);</pre><pre>console.log(response); //however you want to handle response</pre><pre>}</pre><p>NOTE: your transaction reference (tr) should be unique</p><p>Your final view file should look something like this:-</p><pre>import React, { Component } from &quot;react&quot;;<br>import {<br>  Platform,<br>  StyleSheet,<br>  Text,<br>  View,<br>  TextInput,<br>  TouchableOpacity,<br>  NativeModules<br>} from &quot;react-native&quot;;</pre><pre>const UPI = NativeModules.UPI; // &#39;UPI&#39; was module name given</pre><pre>export default class App extends Component {<br>  constructor(props) {<br>    super(props);<br>    this.state = {};<br>  }</pre><pre>openLink = async () =&gt; {<br>    let { amount } = this.state;<br>    let UpiUrl =<br>      &quot;upi://pay?pa=someName@okicici&amp;pn=dhaval&amp;tr=kdahskjahs27575fsdfasdas&amp;am=&quot; +<br>      amount +<br>      &quot;&amp;mam=null&amp;cu=INR&amp;url=<a href="https://myupiapp&amp;refurl=https//MyUPIApp">https://MyUPIApp&amp;refUrl=https://MyUPIApp</a>&quot;;<br>    let response = await UPI.openLink(UpiUrl);<br>    console.log(response); //however you want to handle response<br>  };</pre><pre>render() {<br>    return (<br>      &lt;View style={styles.container}&gt;<br>        &lt;TextInput<br>          placeholder={&quot;Amount&quot;}<br>          style={styles.TextInput}<br>          onChangeText={amount =&gt; this.setState({ amount })}<br>          value={this.state.amount}<br>        /&gt;<br>        &lt;TouchableOpacity style={styles.paymentButton} onPress={this.openLink}&gt;<br>          &lt;Text style={{ color: &quot;white&quot; }}&gt;Make Payment&lt;/Text&gt;<br>        &lt;/TouchableOpacity&gt;<br>      &lt;/View&gt;<br>    );<br>  }<br>}</pre><pre>const styles = StyleSheet.create({<br>  container: {<br>    paddingVertical: 150,<br>    flex: 1,<br>    justifyContent: &quot;space-around&quot;,<br>    alignItems: &quot;center&quot;,<br>    backgroundColor: &quot;#F5FCFF&quot;<br>  },<br>  TextInput: { width: &quot;80%&quot;, height: 40, borderColor: &quot;gray&quot;, borderWidth: 1 },<br>  paymentButton: {<br>    width: 200,<br>    height: 50,<br>    borderRadius: 10,<br>    alignItems: &quot;center&quot;,<br>    justifyContent: &quot;center&quot;,<br>    backgroundColor: &quot;red&quot;<br>  },<br>  welcome: {<br>    fontSize: 20,<br>    textAlign: &quot;center&quot;,<br>    margin: 10<br>  },<br>  instructions: {<br>    textAlign: &quot;center&quot;,<br>    color: &quot;#333333&quot;,<br>    marginBottom: 5<br>  }<br>});</pre><p>…and now you should be able to open any UPI enabled app installed in your android phone prefilled with given details and ready to make payment. You will be log of response on your console(I am assuming you know how to check logs on developer console :D).</p><p>I hope this helps!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a7577458a7b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>