如何整合Iamport韩国支付网关?

时间:2017-03-25 10:12:07

标签: android android-studio payment

我创建了一个应用。现在我想在我的应用中整合Iamport韩国支付之旅。我在开发者门户网站上阅读了整个documentation。我已经检查了github中已经存在URL的代码,但我仍然没有得到确切的解决方案。

问题: - 我没有得到如何与我的应用程序集成,因为在该演示中只有一个<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.zennaxx.iamportpay.MainActivity"> <WebView android:id="@+id/mainWebView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </RelativeLayout> 。意味着我没有得到确切的支付流网关。任何人都可以一步一步解决问题。所以我完全可以理解。我正试图在过去的两周内实施,现在我坚持了。

我完成了以下代码。

main.xml中

public class MainActivity extends Activity {

    private WebView mainWebView;
    private static final String APP_SCHEME = "iamporttest://";

    @SuppressLint("NewApi") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super . onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        mainWebView = (WebView) findViewById(R.id.mainWebView);
        mainWebView . setWebViewClient ( new  InicisWebViewClient ( this ));
        WebSettings settings = mainWebView . getSettings ();
        settings.setJavaScriptEnabled(true);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager . setAcceptCookie ( true );
            cookieManager . setAcceptThirdPartyCookies (mainWebView, true );
        }

        Intent intent = getIntent();
        Uri intentData = intent.getData();

        if ( intentData == null ) {
            mainWebView.loadUrl("myUrl");
        } else {
            // When returning after payment follow-up Certification isp
            String url = intentData.toString();
            if ( url.startsWith(APP_SCHEME) ) {
                String redirectURL = url.substring(APP_SCHEME.length()+3);
                mainWebView.loadUrl(redirectURL);
            }
        }
    }

    @Override
    protected  void  onNewIntent ( Intent  intent ) {
        String url = intent.toString();
        if ( url.startsWith(APP_SCHEME) ) {
            String redirectURL = url.substring(APP_SCHEME.length()+3);
            mainWebView.loadUrl(redirectURL);
        }
    }

}

MainActivity.java

{


 "routes": {
    "3d3d6f6c-5d05-4a4e-a17e-1b128df78c20-20170324": {
      "id": "3d3d6f6c-5d05-4a4e-a17e-1b128df78c20-20170324",
      "revision": 457,
      "date": "20170324",
      "vehicleId": "3d3d6f6c-5d05-4a4e-a17e-1b128df78c20",
      "driverId": "f8ddf3fc-8e70-4edd-b0b4-5e41cf86d72a",
      "steps": [
        {
          "type": "departure",
          "idleTimeSec": 0,
          "perStopTimeSec": 0,
          "arrivalSec": 0,
          "startSec": 0,
          "endSec": 7200,
          "driveToNextSec": -1,
          "distanceToNextMt": -1,
          "displayLabel": ""
        },
        {
          "type": "pickup",
          "orderId": "cd0711f2-f43e-48e9-97db-d76822fbcd54",
          "idleTimeSec": 0,
          "perStopTimeSec": 0,
          "arrivalSec": 8460,
          "startSec": 8460,
          "endSec": 9000,
          "driveToNextSec": -1,
          "distanceToNextMt": -1,
          "displayLabel": "1.1",
          "trackingData": {
            "driverId": "f8ddf3fc-8e70-4edd-b0b4-5e41cf86d72a",
            "vehicleId": "3d3d6f6c-5d05-4a4e-a17e-1b128df78c20",
            "timeInSec": 8460,
            "timeInLatLng": [
              28550470,
              -81211578
            ],
            "timeOutSec": 9000,
            "timeOutLatLng": [
              28550470,
              -81211578
            ],
            "status": "done",
            "statusSec": 8761,
            "statusLatLng": [
              28550470,
              -81211578
            ],
            "timeInDetectedSec": -1,
            "timeInDetectedLatLng": null,
            "timeOutDetectedSec": -1,
            "timeOutDetectedLatLng": null,
            "pods": null
          }
        },
        {
          "type": "delivery",
          "orderId": "cd0711f2-f43e-48e9-97db-d76822fbcd54",
          "idleTimeSec": 0,
          "perStopTimeSec": 0,
          "arrivalSec": 9060,
          "startSec": 9060,
          "endSec": 9120,
          "driveToNextSec": -1,
          "distanceToNextMt": -1,
          "displayLabel": "1.2",
          "trackingData": {
            "driverId": "f8ddf3fc-8e70-4edd-b0b4-5e41cf86d72a",
            "vehicleId": "3d3d6f6c-5d05-4a4e-a17e-1b128df78c20",
            "timeInSec": 9060,
            "timeInLatLng": [
              28472604,
              -81398655
            ],
            "timeOutSec": 9120,
            "timeOutLatLng": [
              28472604,
              -81398655
            ],
            "status": "done",
            "statusSec": 9314,
            "statusLatLng": [
              28472604,
              -81398655
            ],
            "timeInDetectedSec": -1,
            "timeInDetectedLatLng": null,
            "timeOutDetectedSec": -1,
            "timeOutDetectedLatLng": null,
            "pods": null
          }
        }
      ]
    }
  },
  "orders": {},
  "vehicles": {},
  "depots": {},
  "drivers": {}
}

1 个答案:

答案 0 :(得分:2)

如果你读得很完美,那么已经指明了如何整合支付网关。

让我一步一步地向你解释::

  

Step-1 ::

首先使用此URL,然后创建自己的帐户并登录。 然后,您将获得API KEYAPI SECRET KEY,然后在打开PG setting (authentication method payment)标签后将其保留在SendBox上(开启表示您的付款网关处于测试模式,OFF表示您的付款网关是实时的。)

  

Step-2 ::

HERE复制IamPort SDK并与您的项目集成。

  

Step-3 ::

将此代码改为:

public class MainActivity extends Activity {

    private WebView mainWebView;
    private static final String APP_SCHEME = "iamporttest://";

    @SuppressLint("NewApi") @Override
    protected void onCreate(Bundle savedInstanceState) {
        super . onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        mainWebView = (WebView) findViewById(R.id.mainWebView);
        mainWebView . setWebViewClient ( new  InicisWebViewClient ( this ));
        WebSettings settings = mainWebView . getSettings ();
        settings.setJavaScriptEnabled(true);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager . setAcceptCookie ( true );
            cookieManager . setAcceptThirdPartyCookies (mainWebView, true );
        }

        Intent intent = getIntent();
        Uri intentData = intent.getData();

        if ( intentData == null ) {
            //payment.html file is below
            mainWebView.loadUrl("https://yourdomain/API/payment.html");

        } else {
            // When returning after payment follow-up Certification isp
            String url = intentData.toString();
            if ( url.startsWith(APP_SCHEME) ) {
                String redirectURL = url.substring(APP_SCHEME.length()+3);
                mainWebView.loadUrl(redirectURL);
            }
        }
    }

    @Override
    protected  void  onNewIntent ( Intent  intent ) {
        String url = intent.toString();
        if ( url.startsWith(APP_SCHEME) ) {
            String redirectURL = url.substring(APP_SCHEME.length()+3);
            mainWebView.loadUrl(redirectURL);
        }
    }

}
  

Step-4 ::

覆盖onPageFinished()这样的方法,并在阅读下面的代码后进行相关更改。

public class InicisWebViewClient extends WebViewClient {


    public static final String TAG = "[InicisWebViewClient]";

    private Activity activity;


    String imp_uid = "", merchant_uid = "", imp_success = "", error_msg = "", paymentStatusRecUrl = "",
         apply_num = "";

    public InicisWebViewClient(Activity activity) {
        this.activity = activity;

    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        if (!url.startsWith("http://") && !url.startsWith("https://") && !url.startsWith("javascript:"))
        {
            Intent intent = null;

            try {
                //Intent URI process
                Log.e(TAG, "in side try block...");
                intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
                Uri uri = Uri.parse(intent.getDataString());

                //Run the appropriate Activity
                activity.startActivity(new Intent(Intent.ACTION_VIEW, uri));
                return true;
            } catch (URISyntaxException ex) {
                return false;
            } catch (ActivityNotFoundException e) {
                if ( intent == null )
                    return false;

                // Pre-processing for non-installed apps (such as moving Google Play)
                if ( handleNotFoundPaymentScheme(intent.getScheme()) )
                    return true;



                String packageName = intent.getPackage();
                if (packageName != null) {
                    // If you have packageName, search on Google Play
                    activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName)));
                    return true;
                }

                return false;
            }
        }


        return false;
    }

    /**
     * @param scheme
     * @return Whether to process the scheme directly
     * 
     * Processes if ActivityNotFoundException occurs because 3rd party app for billing is not yet installed.
     * If you can extract package information from an intent for a scheme that is not handlered here, then move to market as packageName from the following.
     * 
     */
    protected boolean handleNotFoundPaymentScheme(String scheme) {

        //If you do not run the market after an ActivityNotFoundException occurs because there is no package information in the url that the PG calls
        if ( PaymentScheme.ISP.equalsIgnoreCase(scheme) ) {
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + PaymentScheme.PACKAGE_ISP)));
            return true;
        } else if ( PaymentScheme.BANKPAY.equalsIgnoreCase(scheme) ) {
            activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + PaymentScheme.PACKAGE_BANKPAY)));
            return true;
        }

        return false;
    }

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

        Log.e("[InicisWebViewClient]", "onPageFinished() url = " + url);

        String receivedFinishURL = url;

        String paymentStatusUrl = null;
        try {
            paymentStatusUrl = getDomainName(receivedFinishURL);
            Log.e(TAG, "paymentStatusUrl = " + paymentStatusUrl.toString());

            if (paymentStatusUrl.equals("https://yourdomain/API/paymentStatus.html")) // Write here redirected URL which you have set in JavaScript File
            {

                paymentStatusRecUrl = receivedFinishURL;
                List<NameValuePair> params = URLEncodedUtils.parse(new URI(receivedFinishURL), "UTF-8");

                for (NameValuePair param : params) {
                    if ("imp_uid".equals(param.getName()))
                    {
                        imp_uid = param.getValue();
                    }
                    else if ("merchant_uid".equals(param.getName()))
                    {
                        merchant_uid = param.getValue();
                    }
                    else if ("imp_success".equals(param.getName()))
                    {
                        imp_success = param.getValue();
                    }
                    else if ("error_msg".equals(param.getName()))
                    {
                        error_msg = param.getValue();
                    }
                    else if ("apply_num".equals(param.getName()));
                    {
                        apply_num = param.getValue();
                    }
                }

                Log.e(TAG, "received paged data...........");
                Log.i(TAG, "imp_uid = " + imp_uid);
                Log.i(TAG, "merchant_uid = " + merchant_uid);
                Log.i(TAG, "imp_success = " + imp_success);
                Log.i(TAG, "error_msg = " + error_msg);
                Log.i(TAG, "apply_num = " + apply_num);

                if (imp_success.equals("true"))
                {
                    // If your payment will success then your cursore comes here
                   //if your payment is successful then you can start your new activity and design as per your requirement.
                }
                else if (imp_success.equals("false"))
                {
                    // If your payment will cancelled then your cursore comes here
                }



        } catch (URISyntaxException e) {
            e.printStackTrace();
        }



    }

}
  

Step-5 ::

<强> payment.html

您必须像下面那样实现您的脚本文件,但这是您必须根据您的要求实施的示例文件。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Payment Demo</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js" ></script>
<script type="text/javascript" src="https://service.iamport.kr/js/iamport.payment-1.1.2.js"></script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>

<style>
.goog-logo-link {
   display:none !important;
} 

.goog-te-gadget{
   color: transparent !important;
}
.goog-te-gadget .goog-te-combo{color:#000 !important;}
</style>

<script>


function callme()
{
    IMP.init('yourIMPCredential');
    payRequest();
}
function payRequest()
{

    //you can set your value accordinglly by using simple domcument.getElementById().value
    IMP.request_pay({
    pg : 'inicis', // version 1.1.0.
    pay_method : 'card',
    merchant_uid : 'merchant_' + new Date().getTime(),
    name : 'This is My name',
    amount : 1000,
    buyer_email : 'this_is_buyer_email@gmail.com',
    buyer_name : 'This is buyer name',
    buyer_tel : '010-1234-5678',
    buyer_addr : 'This is buyer email',
    buyer_postcode : '123-456',
    m_redirect_url : 'https://yourdomain/API/payment.html'//here you can specified your URL
}, function(rsp) {
    if ( rsp.success ) {
        var msg = 'Succeess message.';
        msg += 'response_id: ' + rsp.imp_uid;
        msg += 'response_merchant_id : ' + rsp.merchant_uid;
        msg += 'response_paid_amount: ' + rsp.paid_amount;
        msg += 'rsp.apply_num: ' + rsp.apply_num;
    } else {
        var msg = 'Failure message.';
        msg += 'rsp.error_msg: ' + rsp.error_msg;
    }
    alert(msg);
});
}

function googleTranslateElementInit() {
  new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
  //goog-te-combo  
}

</script>
</head>

<body>
<div id="google_translate_element" style="text-align:center;"></div>
<div class="container">
    <div class="row">
<div class="col-md-12" id="alertmsg"></div>
        <!-- panel preview -->
        <div class="col-sm-3"></div>
        <div class="col-sm-6">
            <h4>I'mport; Payment module DEMO</h4>
            <div class="panel panel-default">
            <form name="frm" method="post" id="frm">
                <div class="panel-body form-horizontal payment-form">

                    <div class="form-group">
                        <label for="date" class="col-sm-3 control-label">Payment Name</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="name" name="name" value="paymentName">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="order_number" class="col-sm-3 control-label">Order Number</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="order_number" name="order_number" value="1234">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="ticket_name" class="col-sm-3 control-label">Ticket Name</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="ticket_name" name="ticket_name" value="ticket_1234">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="amount" class="col-sm-3 control-label">Amount</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="amount" name="amount" value ="1000">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="buyer_email" class="col-sm-3 control-label">Buyer Email</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="buyer_email" name="buyer_email" value="text@gmail.com">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="buyer_name" class="col-sm-3 control-label">Buyer Name</label>
                        <div class="col-sm-9">
                            <input type="text" class="form-control" id="buyer_name" name="buyer_name" value="Tester">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="buyer_phone_number" class="col-sm-3 control-label">Buyer Phone Number</label>
                        <div class="col-sm-9">
                            <input type="tel" class="form-control" id="buyer_phone_number" name="buyer_phone_number" value="0129384756">
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="buyer_tel" class="col-sm-3 control-label">Virtual account deposit date</label>
                        <div class="col-sm-9">
                            <input type="date" class="form-control" id="vbank_due" name="vbank_due">
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-12 text-right">
                        <div class="col-sm-3"></div>
                        <div class="col-sm-6">
                            <button class='form-control btn btn-primary' type='submit' onclick="callme();"> Pay Now </button>   
                        </div>
                        <div class="col-sm-3"></div>
                        </div>
                    </div>
                </div>
                </form>
            </div>            
        </div> 
        <div class="col-sm-3"></div>
    </div>
</div>
</body>
</html>

创建另一个文件,您可以在其中收到付款状态。

<强> paymentStaus.html

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Payment status</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js" ></script>
<style>

</style>

<script>

</script>
</head>

<body>
<div style="text-align:center;"></div>
<div class="container">
    <div class="row">
<div class="col-md-12" id="alertmsg"></div>
        <!-- panel preview -->
        <div class="col-sm-3"></div>
        <div class="col-sm-6">
            <h4>I'mport; Payment module Finish</h4>
            <div class="panel panel-default">
            <form name="frm" method="post" id="frm">
                <div class="panel-body form-horizontal payment-form">                                                      
                    <div class="form-group">
                        <div class="col-sm-12 text-right">
                        <div class="col-sm-3"></div>
                        <div class="col-sm-6">
                            <button class='form-control btn btn-primary' type='submit' > Finish </button>   
                        </div>
                        <div class="col-sm-3"></div>
                        </div>
                    </div>
                </div>
                </form>
            </div>            
        </div> 
        <div class="col-sm-3"></div>
    </div>
</div>
</body>
</html>
  

Step-6 ::

如果您需要使用API​​查看付款,请检查here。  使用API_KEYAPI SECRET_KEY获取令牌。然后,在使用该令牌和imp_uid号码后,您将获得付款的所有详细信息。

我希望你能得到你的解决方案。我知道你是否需要任何东西。