Skip to content

Commit

Permalink
add multiple environment init for newmall
Browse files Browse the repository at this point in the history
  • Loading branch information
weixuefeng committed Feb 12, 2019
1 parent 989602a commit 392c117
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 10 deletions.
2 changes: 1 addition & 1 deletion NewPaySDK/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ publish {
userOrg = 'testfw'
groupId = 'org.newtonproject.newpay.android.sdk'
artifactId = 'NewPaySDK'
publishVersion = '0.0.10'
publishVersion = '1.0.0'
desc = 'third application request profile from newpay'
website = 'https://github.com/weixuefeng/NewMallDemo.git'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.newtonproject.newpay.android.sdk.bean.Action;
import org.newtonproject.newpay.android.sdk.bean.Order;
import org.newtonproject.newpay.android.sdk.bean.SigMessage;
import org.newtonproject.newpay.android.sdk.constant.Constant;
import org.newtonproject.newpay.android.sdk.constant.Environment;
import org.web3j.crypto.ECKeyPair;
import org.web3j.crypto.Sign;
import org.web3j.utils.Numeric;
Expand Down Expand Up @@ -43,17 +45,58 @@ public class NewPayApi {
private static final String CONTENT = "CONTENT";
private static final String SIGNATURE = "SIGNATURE";

private static String SHARE_URL = RELEASE_SHARE_URL;
private static String authorize_pay = Constant.MainNet.authorize_pay;
private static String authorize_login_place = Constant.MainNet.authorize_login_place;

private NewPayApi() {}


/**
* @param context context
* @param appKey appkey for third application
* @param appid app id which registered in newton api.
*/
public static void init(Application context, String appKey, String appid) {
mApplication = context;
privateKey = appKey;
appId = appid;
gson = new Gson();
}

/**
* @param context context
* @param appKey appkey for third application
* @param appid app id which registered in newton api.
* @param environment eg: Environment.MAINNET, Environment.TESTNET ...
*/
public static void init(Application context, String appKey, String appid, Environment environment) {
mApplication = context;
privateKey = appKey;
appId = appid;
gson = new Gson();
switch (environment) {
case DEVNET:
authorize_pay = Constant.DevNet.authorize_pay;
authorize_login_place = Constant.DevNet.authorize_login_place;
break;
case BETANET:
authorize_pay = Constant.BetaNet.authorize_pay;
authorize_login_place = Constant.BetaNet.authorize_login_place;
break;
case TESTNET:
authorize_pay = Constant.TestNet.authorize_pay;
authorize_login_place = Constant.TestNet.authorize_login_place;
break;
case MAINNET:
authorize_pay = Constant.MainNet.authorize_pay;
authorize_login_place = Constant.MainNet.authorize_login_place;
break;
}
}

public static void requestProfileFromNewPay(Activity activity) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("newpay://org.newtonproject.newpay.android.authorize"));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authorize_login_place));
intent.putExtra(ACTION, Action.REQUEST_PROFILE);
intent.putExtra(APPID, appId);
intent.putExtra(SIGNATURE, gson.toJson(getSigMessage(privateKey)));
Expand All @@ -69,7 +112,7 @@ public static void requestProfileFromNewPay(Activity activity) {

public static void requestPayForThirdApp(Activity activity, String address, BigInteger account){
String unitStr = "NEW";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("newpay://org.newtonproject.newpay.android.pay"));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authorize_pay));
intent.putExtra("SYMBOL", unitStr);
intent.putExtra("ADDRESS", address);
intent.putExtra("AMOUNT", account.toString(10));
Expand All @@ -84,7 +127,7 @@ public static void requestPayForThirdApp(Activity activity, String address, BigI
}

public static void requestPushOrder(Activity activity, ArrayList<Order> orders) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("newpay://org.newtonproject.newpay.android.authorize"));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authorize_login_place));
intent.putExtra(ACTION, Action.PUSH_ORDER);
intent.putExtra(APPID, appId);
intent.putExtra(SIGNATURE, gson.toJson(getSigMessage(privateKey)));
Expand All @@ -98,7 +141,7 @@ public static void requestPushOrder(Activity activity, ArrayList<Order> orders)
}

public static void requestProfileFromNewPay(Fragment activity) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("newpay://org.newtonproject.newpay.android.authorize"));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authorize_login_place));
intent.putExtra(ACTION, Action.REQUEST_PROFILE);
intent.putExtra(APPID, appId);
intent.putExtra(SIGNATURE, gson.toJson(getSigMessage(privateKey)));
Expand All @@ -114,7 +157,7 @@ public static void requestProfileFromNewPay(Fragment activity) {

public static void requestPayForThirdApp(Fragment activity, String address, BigInteger account){
String unitStr = "NEW";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("newpay://org.newtonproject.newpay.android.pay"));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authorize_pay));
intent.putExtra("SYMBOL", unitStr);
intent.putExtra("ADDRESS", address);
intent.putExtra("AMOUNT", account.toString(10));
Expand All @@ -129,7 +172,7 @@ public static void requestPayForThirdApp(Fragment activity, String address, BigI
}

public static void requestPushOrder(Fragment activity, ArrayList<Order> orders) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("newpay://org.newtonproject.newpay.android.authorize"));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authorize_login_place));
intent.putExtra(ACTION, Action.PUSH_ORDER);
intent.putExtra(APPID, appId);
intent.putExtra(SIGNATURE, gson.toJson(getSigMessage(privateKey)));
Expand All @@ -155,7 +198,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
.setPositiveButton(R.string.download_newpay, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse(RELEASE_SHARE_URL);
Uri uri = Uri.parse(SHARE_URL);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intent);
dialogInterface.dismiss();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.newtonproject.newpay.android.sdk.constant;

/**
* @author [email protected]
* @version $Rev$
* @time: 2019/2/12--4:51 PM
* @description
* @copyright (c) 2018 Newton Foundation. All rights reserved.
*/
public class Constant {

public interface MainNet {
String authorize_pay = "newpay://org.newtonproject.newpay.android.release.pay";
String authorize_login_place = "newpay://org.newtonproject.newpay.android.release.authorize";
}

public interface TestNet {
String authorize_pay = "newpay://org.newtonproject.newpay.android.pay";
String authorize_login_place = "newpay://org.newtonproject.newpay.android.authorize";
}

public interface BetaNet {
String authorize_pay = "newpay://org.newtonproject.newpay.android.beta.pay";
String authorize_login_place = "newpay://org.newtonproject.newpay.android.beta.authorize";
}

public interface DevNet {
String authorize_pay = "newpay://org.newtonproject.newpay.android.dev.pay";
String authorize_login_place = "newpay://org.newtonproject.newpay.android.dev.authorize";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.newtonproject.newpay.android.sdk.constant;

/**
* @author [email protected]
* @version $Rev$
* @time: 2019/2/12--5:01 PM
* @description
* @copyright (c) 2018 Newton Foundation. All rights reserved.
*/
public enum Environment {
MAINNET, TESTNET, BETANET, DEVNET
}
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

# 1.add gradle dependency on your app build.gradle
```
implementation 'org.newtonproject.newpay.android.sdk:NewPaySDK:0.0.10'
implementation 'org.newtonproject.newpay.android.sdk:NewPaySDK:1.0.0'
```
# 2. init NewPayApi on application
```
// in release environment
NewPayApi.init(getApplication(), yourPrivateKey, $yourtransaferId);
// in testnet, beta, dev and etc. environment
NewPayApi.init(getApplication(), yourPrivateKey, $yourtransaferId, Environment.DEVNET);
```
# 3. request profile information
```
Expand Down Expand Up @@ -58,3 +62,8 @@ if(requestCode == NewPayApi.REQUEST_CODE_NEWPAY && resultCode == RESULT_OK) {
```



### Change log

# 1.0.0 add multiple environment init for NewMall.
36 changes: 36 additions & 0 deletions app/src/main/java/com/feng/newmalldemo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.newtonproject.newpay.android.sdk.bean.Order;
import org.newtonproject.newpay.android.sdk.bean.ProfileInfo;
import org.newtonproject.newpay.android.sdk.bean.SigMessage;
import org.newtonproject.newpay.android.sdk.constant.Environment;

import java.math.BigInteger;
import java.util.ArrayList;
Expand All @@ -35,6 +36,13 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private ImageView imageView;
private String TAG = "Activity";

TextView dev;
TextView beta;
TextView testnet;
TextView mainnet;
TextView evn;


private static final int REQUEST_CODE_NEWPAY = 1000;
private static final String privateKey = "0xbc6162af5677bc108fc227a1b1178aede933d05979cc5c6154078c2eae068dac";
private static final String publicKey = "0xe5f001b70a3911c1b6dcb857add080beab3ceff7278a012678dbb7e869c787cfd5739cf980adebd59a826eb78eb4d10e746a1fadb49f0245a87270d5a817cda0";
Expand All @@ -59,8 +67,17 @@ private void initView() {
newidTextView = findViewById(R.id.newidTextView);
imageView = findViewById(R.id.avatarImageView);
request20Bt = findViewById(R.id.request20Bt);
dev = findViewById(R.id.dev);
beta = findViewById(R.id.beta);
testnet = findViewById(R.id.testnet);
mainnet = findViewById(R.id.mainnet);
evn = findViewById(R.id.env);
profileLinearLayout.setOnClickListener(this);
request20Bt.setOnClickListener(this);
dev.setOnClickListener(this);
beta.setOnClickListener(this);
mainnet.setOnClickListener(this);
testnet.setOnClickListener(this);
single = findViewById(R.id.pushSingle);
multiple = findViewById(R.id.pushMultiple);
single.setOnClickListener(this);
Expand All @@ -82,6 +99,25 @@ public void onClick(View v) {
case R.id.pushSingle:
pushSingle();
break;
case R.id.dev:
evn.setText("Dev");
NewPayApi.init(getApplication(), privateKey, "9a674d65c945569a9071b31b07f3bc52", Environment.DEVNET);

break;
case R.id.beta:
evn.setText("Beta");
NewPayApi.init(getApplication(), privateKey, "9a674d65c945569a9071b31b07f3bc52", Environment.BETANET);

break;
case R.id.testnet:
evn.setText("testnet");
NewPayApi.init(getApplication(), privateKey, "9a674d65c945569a9071b31b07f3bc52", Environment.TESTNET);

break;
case R.id.mainnet:
evn.setText("main");
NewPayApi.init(getApplication(), privateKey, "9a674d65c945569a9071b31b07f3bc52", Environment.MAINNET);
break;
}
}

Expand Down
53 changes: 52 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,56 @@
android:text="上链多个商品"
/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="20dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
>
<TextView
android:id="@+id/dev"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Devnet"
android:gravity="center"
/>
<TextView
android:id="@+id/beta"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Beta"
android:gravity="center"
/>
<TextView
android:id="@+id/testnet"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TestNet"
android:gravity="center"
/>
<TextView
android:id="@+id/mainnet"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="MainNet"
android:gravity="center"
/>
</LinearLayout>
<TextView
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:gravity="center"
android:id="@+id/env"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="env"
/>
</android.support.constraint.ConstraintLayout>

0 comments on commit 392c117

Please sign in to comment.