如何在Android中集成PayU资金网关? [关闭]

2023-12-27

我正在开发一个电子商务应用程序,我想在其中集成 Payumoney 支付网关。有人可以帮我提供一些程序、链接或教程吗?该怎么做?谢谢。


我已经做得很完美了:) :) 你必须编辑SuccessURL and FailureURL:

它非常适合我。

public class PayUMoneyActivity extends AppCompatActivity {

    /**
     * Adding WebView as setContentView
     */
    WebView webView;

    /**
     * Context for Activity
     */
    Context activity;
    /**
     * Order Id
     * To Request for Updating Payment Status if Payment Successfully Done
     */
    int mId; //Getting from Previous Activity
    /**
     * Required Fields
     */
    // Test Variables
    /*
    private String mMerchantKey = "FCyqqZ";
    private String mSalt = "sfBpGA8E";
    private String mBaseURL = "https://test.payu.in";
    */

    // Final Variables
    private String mMerchantKey = "Your Merchant Key";
    private String mSalt = "Salt";
    private String mBaseURL = "https://secure.payu.in";


    private String mAction = ""; // For Final URL
    private String mTXNId; // This will create below randomly
    private String mHash; // This will create below randomly
    private String mProductInfo = "Food Items"; //Passing String only
    private String mFirstName; // From Previous Activity
    private String mEmailId; // From Previous Activity
    private double mAmount; // From Previous Activity
    private String mPhone; // From Previous Activity
    private String mServiceProvider = "payu_paisa";
    private String mSuccessUrl = "your success URL";
    private String mFailedUrl = "Your Failure URL";


    boolean isFromOrder;
    /**
     * Handler
     */
    Handler mHandler = new Handler();

    /**
     * @param savedInstanceState
     */
    @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        super.onCreate(savedInstanceState);

        /**
        * Setting WebView to Screen
        */
        setContentView(R.layout.activity_webview_for_payumoney);

        /**
         * Creating WebView
         */
        webView = (WebView) findViewById(R.id.payumoney_webview);

        /**
         * Context Variable
         */
        activity = getApplicationContext();

        /**
         * Actionbar Settings
         */
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        // enabling action bar app icon and behaving it as toggle button
        ab.setHomeButtonEnabled(true);
        ab.setTitle(getString(R.string.title_activity_online_payment));

        /**
         * Getting Intent Variables...
         */
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {

            mFirstName = bundle.getString("name");
            mEmailId = bundle.getString("email");
            mAmount = bundle.getDouble("amount");
            mPhone = bundle.getString("phone");
            mId = bundle.getInt("id");
            isFromOrder = bundle.getBoolean("isFromOrder");

            Log.i(TAG, "" + mFirstName + " : " + mEmailId + " : " + mAmount + " : " + mPhone);

            /**
             * Creating Transaction Id
             */
            Random rand = new Random();
            String randomString = Integer.toString(rand.nextInt()) + (System.currentTimeMillis() / 1000L);
            mTXNId = hashCal("SHA-256", randomString).substring(0, 20);

            mAmount = new BigDecimal(mAmount).setScale(0, RoundingMode.UP).intValue();

            /**
             * Creating Hash Key
             */
            mHash = hashCal("SHA-512", mMerchantKey + "|" +
                    mTXNId + "|" +
                    mAmount + "|" +
                    mProductInfo + "|" +
                    mFirstName + "|" +
                    mEmailId + "|||||||||||" +
                    mSalt);

            /**
             * Final Action URL...
             */
            mAction = mBaseURL.concat("/_payment");

            /**
             * WebView Client
             */
            webView.setWebViewClient(new WebViewClient() {

                @Override
                public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                    super.onReceivedError(view, request, error);
                    Toast.makeText(activity, "Oh no! " + error, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onReceivedSslError(WebView view,
                                               SslErrorHandler handler, SslError error) {
                    Toast.makeText(activity, "SSL Error! " + error, Toast.LENGTH_SHORT).show();
                    handler.proceed();
                }

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    return super.shouldOverrideUrlLoading(view, url);
                }

                @Override
                public void onPageFinished(WebView view, String url) {

                    if (url.equals(mSuccessUrl)) {
                        Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class);
                        intent.putExtra("status", true);
                        intent.putExtra("transaction_id", mTXNId);
                        intent.putExtra("id", mId);
                        intent.putExtra("isFromOrder", isFromOrder);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    } else if (url.equals(mFailedUrl)) {
                        Intent intent = new Intent(PayUMoneyActivity.this, PaymentStatusActivity.class);
                        intent.putExtra("status", false);
                        intent.putExtra("transaction_id", mTXNId);
                        intent.putExtra("id", mId);
                        intent.putExtra("isFromOrder", isFromOrder);
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                    }
                    super.onPageFinished(view, url);
                }
            });

            webView.setVisibility(View.VISIBLE);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setCacheMode(2);
            webView.getSettings().setDomStorageEnabled(true);
            webView.clearHistory();
            webView.clearCache(true);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setSupportZoom(true);
            webView.getSettings().setUseWideViewPort(false);
            webView.getSettings().setLoadWithOverviewMode(false);
            webView.addJavascriptInterface(new PayUJavaScriptInterface(PayUMoneyActivity.this), "PayUMoney");

            /**
             * Mapping Compulsory Key Value Pairs
             */
            Map<String, String> mapParams = new HashMap<>();

            mapParams.put("key", mMerchantKey);
            mapParams.put("txnid", mTXNId);
            mapParams.put("amount", String.valueOf(mAmount));
            mapParams.put("productinfo", mProductInfo);
            mapParams.put("firstname", mFirstName);
            mapParams.put("email", mEmailId);
            mapParams.put("phone", mPhone);
            mapParams.put("surl", mSuccessUrl);
            mapParams.put("furl", mFailedUrl);
            mapParams.put("hash", mHash);
            mapParams.put("service_provider", mServiceProvider);

            webViewClientPost(webView, mAction, mapParams.entrySet());
        } else {
            Toast.makeText(activity, "Something went wrong, Try again.", Toast.LENGTH_LONG).show();
        }
    }

    /**
     * Posting Data on PayUMoney Site with Form
     *
     * @param webView
     * @param url
     * @param postData
     */
    public void webViewClientPost(WebView webView, String url,
                                  Collection<Map.Entry<String, String>> postData) {
        StringBuilder sb = new StringBuilder();

        sb.append("<html><head></head>");
        sb.append("<body onload='form1.submit()'>");
        sb.append(String.format("<form id='form1' action='%s' method='%s'>", url, "post"));

        for (Map.Entry<String, String> item : postData) {
            sb.append(String.format("<input name='%s' type='hidden' value='%s' />", item.getKey(), item.getValue()));
        }
        sb.append("</form></body></html>");

        Log.d("TAG", "webViewClientPost called: " + sb.toString());
        webView.loadData(sb.toString(), "text/html", "utf-8");
    }

    /**
     * Hash Key Calculation
     *
     * @param type
     * @param str
     * @return
     */
    public String hashCal(String type, String str) {
        byte[] hashSequence = str.getBytes();
        StringBuffer hexString = new StringBuffer();
        try {
            MessageDigest algorithm = MessageDigest.getInstance(type);
            algorithm.reset();
            algorithm.update(hashSequence);
            byte messageDigest[] = algorithm.digest();

            for (int i = 0; i < messageDigest.length; i++) {
                String hex = Integer.toHexString(0xFF & messageDigest[i]);
                if (hex.length() == 1)
                    hexString.append("0");
                hexString.append(hex);
            }
        } catch (NoSuchAlgorithmException NSAE) {
        }
        return hexString.toString();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home) {
            onPressingBack();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        onPressingBack();
    }

    /**
     * On Pressing Back
     * Giving Alert...
     */
    private void onPressingBack() {

        final Intent intent;

        if(isFromOrder)
            intent = new Intent(PayUMoneyActivity.this, ProductInCartList.class);
        else
            intent = new Intent(PayUMoneyActivity.this, MainActivity.class);

        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(PayUMoneyActivity.this);

        // Setting Dialog Title
        alertDialog.setTitle("Warning");

        // Setting Dialog Message
        alertDialog.setMessage("Do you cancel this transaction?");

        // On pressing Settings button
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
                startActivity(intent);
            }
        });

        // on pressing cancel button
        alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        // Showing Alert Message
        alertDialog.show();
    }

    public class PayUJavaScriptInterface {
        Context mContext;

        /**
         * Instantiate the interface and set the context
         */
        PayUJavaScriptInterface(Context c) {
            mContext = c;
        }

        public void success(long id, final String paymentId) {
            mHandler.post(new Runnable() {

                public void run() {
                    mHandler = null;
                    Toast.makeText(PayUMoneyActivity.this, "Payment Successfully.", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}

EDITED:

您可以使用https://www.payumoney.com/dev-guide/webcheckout/redirect.html https://www.payumoney.com/dev-guide/webcheckout/redirect.html now.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在Android中集成PayU资金网关? [关闭] 的相关文章

随机推荐

  • 在游乐场导入神无

    有没有办法添加 Kanna https github com tid kijyun Kanna https github com tid kijyun Kanna 到 XCode 中的 Playground 我尝试过手动安装并通过 Coco
  • 使用 C# 保护内存中的敏感数据

    我有一个 C 应用程序 它用敏感数据初始化一些变量 它们不是密码 但我们认为它们很敏感 我要保护的变量值都是字符串类型 我想做的是找到一种机制 我知道没有 100 的机制 让我能够保护内存中的变量值 或者至少让攻击者更难读取它们 因此 我可
  • 如何在 php 中使用通过 header() 传递的变量

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我在用着header 传递变量use
  • 如何为 Bootstrap 制作响应式(行流体)Mixin

    我可以用以下代码替换此代码 div class row div class span10 div div class span2 div div 有了这个 使其更加语义化 div class article div class main s
  • Visual Studio 2010 Qt 链接问题

    我花了整个周末试图解决这个问题 现在我已经迈出了最后一步 我的目标 让 Visual Studio 2010 和 Qt 4 7 3 一起工作 我从源代码安装了 Qt 指定使用以下配置进行构建 configure exe debug and
  • 使用 SimpleDateFormat 解析奇怪的日期和时间结果

    使用 SimpleDateFormat 解析 ISO8601 日期和时间时遇到一个奇怪的问题 相关代码是 public class DateHelper private static SimpleDateFormat iso8601form
  • Drone.io 在本地构建,无需推送到存储库

    io团队和爱好者 我在本地测试了 Drone io 我喜欢它 有没有办法在本地使用 Drone io 而不推送到存储库 我想在开发人员环境中使用它 这意味着开发人员可以在推送到存储库之前测试他们的代码 可以这样做吗 你可以运行 drone
  • Delphi v10.2.2 升级到 v10.2.3 后 Android 库“将系统文件恢复为默认值”破坏了编译

    将 Delphi 从 v10 2 2 升级到 v10 2 3 后 TBannerAd 横幅在尝试调用 LoadAd 方法时引发异常 在了解到 Tokyo 10 2 3 已更新 Google Play 服务及其库 后 升级到Delphi v1
  • 使用 Bloch 的构建器模式是否会对内存和性能造成影响?

    与仅使用构造函数创建对象相比 内存和性能使用情况如何 这里的用法是创建一个Set or List它可能包含数百万个以上的条目 我担心使用布洛赫的构建器模式的开销 我以前用过它 但从未在这么大的范围内使用过 参考 第 2 项 在面对许多构造函
  • 时间戳的正则表达式

    显然我对正则表达式很糟糕 这对我来说毫无意义 我想要一个匹配时间的表达式 例如01 23 45在一个字符串内 我试过这个 r 0 9 2 2 0 9 2 但它不起作用 我需要能够获取整个时间戳 我尝试过的其他人只找到了大约 2 位数字 恐怕
  • 在 Java 中声明项目常量的正确方法是什么?

    对于 Java 开发人员来说 这似乎是一个愚蠢的问题 但是 我是 Java 新手 而且我的背景是低级 c 语言 我曾经包含一个头文件 其中包含与我的项目相关的所有常量 通常是 define 我现在正在开发一个大型 Java 项目 我需要将一
  • .NET MVC 3 部署包缺少引用

    在 ASP NET MVC3 项目中 我有这样的结构 Core csproj gt 3rdparty1 dll gt 3rdpartyreference dll gt 3rdparty2 dll Web csproj gt core dll
  • Java发生在线程启动之前

    我在某处读到 启动线程对发生之前的关系有一些特殊的影响 现在我不确定我的代码是否保证发生在关系之前 所以请赐教 我有一个 Dispatcher 线程和一个实现的 Worker 类Runnable界面 Dispatcher 线程创建一个新的
  • 如何在 JavaScript 中记录获取的网络资源?

    有没有办法访问浏览器请求的资源列表 在 Chrome 检查器的网络面板中找到的资源 我希望能够迭代这些获取的资源以显示已访问的域 例如 for var i 0 i lt window navigator resources length i
  • Spring Boot 和 Spring Cloud Security OAUTH 2 SSO 在最新版本中失败

    我正在尝试使用 OAuth 将示例 Spring Boot 和 Spring Cloud Security 从 Spring Boot 1 4 1 Brixton RELEASE 升级到 Spring Boot 1 5 3 Dalston
  • 提交后钩子以 svn 中的用户身份运行

    Apache 在 Windows 机器上作为 SYSTEM 运行 post commit bat 应该使用什么用户和密码 我正在尝试将内容复制到目录以在网络服务器上的开发人员版本上进行测试 但似乎无法正常工作 挂钩脚本将由服务器启动 因此您
  • Android View.onDraw() 总是有一个干净的 Canvas

    我正在尝试画动画 为此 我扩展了 View 并重写了 onDraw 方法 我期望的是 每次调用 onDraw 时 画布都会处于我保留它的状态 我可以选择清除它或只绘制它的一部分 这就是我使用 SurfaceView 时的工作方式 但每次画布
  • PhoneGap/Cordova Android 应用程序无法仅通过 3g/4g 访问互联网

    我遇到了一个问题 只有少数用户报告他们无法通过移动网络互联网使用我的应用程序 需要互联网访问 他们根本无法访问网络来登录应用程序 但该应用程序可以通过 Wi Fi 连接完美运行 该请求显然是跨域的 并且服务器API被编程为接受跨域请求 登录
  • Enum.hashCode() 背后的原因是什么?

    Enum 类中的 hashCode 方法是最终方法 定义为 super hashCode 这意味着它返回一个基于实例地址的数字 该数字是来自程序员 POV 的随机数 定义它例如作为ordinal getClass getName hashC
  • 如何在Android中集成PayU资金网关? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我正在开发一个电子商务应用程序 我想在其中集成 Payumoney 支付网关 有人可以帮我提供一些程序 链接或教程吗 该怎么做 谢谢