在Android中集成Dibs支付网关

时间:2013-04-26 07:51:19

标签: android payment-gateway payment payment-processing dibs

我正在开发Dibs Payment集成但无法取得成功。所有东西在演示模式下运行良好,但是当提供商家ID时,在打开卡片详细信息表单之前会出现错误“数据已被篡改。检查项无效”。我不知道它是什么。在谷歌搜索后,我发现它与MAC计算有关,但如何在我的代码中计算MAC。我的全班付款如下所示。

public class CheckOut extends Activity {

private static final String TAG = "DIBS." + CheckOut.class.getSimpleName();

private DibsPayment paymentWindow;
public static String total, resname, resid, userid, menunames, itemnames,
        itemquantity, ordertype, address, city, contactno, pincode,
        deliverttime, orderid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.checkout);
    RandomStringGenerator randomorderid = new RandomStringGenerator();
    Intent i = getIntent();
    // total=String.valueOf(1);
    total = i.getStringExtra("grandTotal");
    resname = i.getStringExtra("res_name");
    resid = i.getStringExtra("res_id");
    userid = i.getStringExtra("user_id");
    menunames = i.getStringExtra("menu_names");
    itemnames = i.getStringExtra("item_prices");
    itemquantity = i.getStringExtra("item_quantity");
    ordertype = i.getStringExtra("ordertype");
    address = i.getStringExtra("address");
    city = i.getStringExtra("city");
    contactno = i.getStringExtra("phone");
    pincode = i.getStringExtra("pin");
    deliverttime = i.getStringExtra("delivery_time");
    orderid = randomorderid.getAlphaNumeric(5);
    Toast.makeText(
            getApplicationContext(),
            orderid + "\n" + resname + "\n" + resid + "\n" + userid + "\n"
                    + ordertype + "\n" + address + "\n" + city + "\n"
                    + pincode + "\n" + contactno + "\n" + deliverttime
                    + "\n" + menunames + "\n" + itemnames + "\n"
                    + itemquantity + "\n" + total, Toast.LENGTH_SHORT)
            .show();
    /*
     * Intent intent=getIntent(); String []
     * arrayList=intent.getStringArrayExtra("payment_item"); // int l_itr =
     * arrayList.length; // while(l_itr.hasNext()) { for(int
     * i=0;i<=arrayList.length-1;i++){
     * 
     * @SuppressWarnings("rawtypes") //HashMap l_map = (HashMap)
     * l_itr.next(); String item=arrayList[i]; Log.v(item, "item"); String
     * item =(String)i.get(DatabaseHandler.KEY_ITEM); Log.v(item, "item");
     * String unicost= (String)l_map.get(DatabaseHandler.KEY_UNITCOST);
     * Log.v(unicost, "unicost"); String l_res_name = (String)
     * l_map.get(DatabaseHandler.KEY_QUANTITY); Log.v(l_res_name,
     * "quantity"); String l_street = (String)
     * l_map.get(DatabaseHandler.KEY_TOTAL); Log.v(l_street, "total"); }
     */

    paymentWindow = (DibsPayment) findViewById(R.id.DibsPayment);

    // Set your listener implementation, to get callbacks in the life-cycle
    // of a payment processing
    paymentWindow
            .setPaymentResultListener(new MyPaymentResultListener(this));

    // Load the payment window with the payment data that suits the payment
    // flow you need
    // Please be patient, when loading on the emulator
    paymentWindow.loadPaymentWindow(constructPaymentData());
}

/**
 * Shows a "cancel" action in the options menu on the phone, which shows how
 * to call cancel functionality into the payment window to cancel ongoing
 * payment processing.
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.payment_window_menu, menu);
    return true;
}

/**
 * If user chose "cancel" in options menu, we call "cancel" into payment
 * window.
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menuitem_payment_window_cancel:
        //
        // Calling cancel into payment window cancels the ongoing payment
        // processing.
        // Because cancelling is an asynchronous process, you will need to
        // wait for a callback
        // to paymentCancelled on your PaymentResultListener listener,
        // before being positive that
        // payment window is done cancelling.
        //
        paymentWindow.cancelPayment();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/**
 * @return the payment data instance that is needed as input to
 *         {@link DibsPayment#loadPaymentWindow(dk.dibs.android.library.PaymentData)}
 */
private PaymentData constructPaymentData() {
    // IMPORTANT: This needs to be set to YOUR merchant number, that you
    // have obtained through an
    // agreement with DIBS.
    // you can use the merchant "demo" for a demorun through the payment
    // window // read information about demo mode in the documentation

     String merchantId = "******";
    //String merchantId = "demo";

    // The currency the payment is to be processed in
    String currencyCode = "DKK";

    // You set this to your own orderId value
    String yourOrderId = orderid;

    // The amount to be paid, given in "least possible unit" (aka: "oerer")
    long amount = (new Double(total)).longValue();
    // The cards that is allowed to be used in payment window
    List<String> payTypes = new ArrayList<String>();
    payTypes.add("MC");
    payTypes.add("MTRO");
    payTypes.add("VISA");

    // this will add fee to the payment window.
    boolean calcfee = true;

    // In this example, we simply use "PurchasePaymentData", which is a
    // simple "buy-with-credit-card" flow,
    // where no pre-authorization is performed.
    //
    // Look to other subclasses of PaymentData for the other supported
    // flows.
    //
    PurchasePaymentData paymentData = new PurchasePaymentData(merchantId,
            currencyCode, yourOrderId, amount, payTypes);

    paymentData.setCalcfee(calcfee);

    // Set this flag to "true", if you want to be able to use test cards.
    // REMEMBER to reset this to false, in production !!!
    paymentData.setTest(true);

    // If you want checks (and payment failure) if the orderId you gave
    // already have been payed.
    paymentData.setUseUniqueOrderIdCheck(false);

    // If you want MAC security calculations, you will need to pre-calculate
    // a MAC value on your server,
    // based on the values you give to this payment window, and set this
    // pre-calculated MAC value like this.
    //
     paymentData.setCalculatedMAC("");

    // Payment window supports loading cancel or callback URLs based on
    // payment outcome.
    // Another, and maybe better, way to do this in an app, is to listen for
    // the proper callbacks
    // on the listener you set on the payment window, and then do your own
    // cancel or payment success
    // handling against your own servers.
    //

    try {
        paymentData.setCallbackUrl(new URL(
                "http://****.demoprojects.in/accept.php"));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        paymentData.setCancelUrl(new URL("http://****.demoprojects.in/accept.php"));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    paymentData.setLanguage("en_UK");
    paymentData.setTheme(Theme.ANDROID_DIBS);

    // If you need to, you can pass custom options to the payment window,
    // which will be posted back again.
    //
    // Map<String, String> yourCustomOptions = new HashMap<String,
    // String>();
    // yourCustomOptions.put("foo", "bar");
    // paymentData.setCustomOptions(yourCustomOptions);

    return paymentData;
}

/*
 * public void delete() { DatabaseHandler readDatabase = new
 * DatabaseHandler( getApplicationContext()); readDatabase.deleteAll(); }
 */
}

这是我第一次付款。请帮助我,因为安全是这里的主要关注点。 在此先感谢:)

1 个答案:

答案 0 :(得分:1)

Dib允许使用两种算法来计算MAC,算法的选择由您决定。当前,它处理MD5和SHA-1。 Dibs建议使用SHA-1,因为它可以提供更好的安全性。

SHA-1(data&currency&method&SecretKey&)

MD5(data&currency&method&SecretKey&)

数据是包含有关购买数量和数量的信息的字符串。

安全> DebiTech DefenderTM> MAC配置,您将在dibs仪表板中找到密钥。

点击here以获取更多参考: