安卓

设置账户 Server side Client side

首先,您需要一个 MoneyCollect 账户。立即注册

1.设置服务器端

可在GitHub 上Download 的模拟商家系统界面代码。

1.1 使用您自己的密钥替换 the public key MobilePayController.Java 文件中的和 private key

// 您的帐户 PUBLIC_SECRET("Bearer "+PUBLIC_SECRET)
private static final String PUBLIC_SECRET = "Bearer live_pu_OGJ0EidwEg4GjymEiRD7cUBk7IQIYmhwhJlUM****";
// 您的帐户 PRIVATE_SECRET("Bearer "+PRIVATE_SECRET)
private static final String PRIVATE_SECRET = "Bearer live_pr_OGJ0EidwEg4GjymEiRD4MRxBCo0OumdH6URv****";

PUBLIC\SECRET 和 PRIVATE\SECRET 的格式为("Bearer"+PUBLIC_SECRET)

1.2 修改服务器端口(默认为 9898)

server.port=9898

商家用自己的key信息替换掉代码中的 the public key private key 信息,然后打开服务器。(默认服务器端口为 9099,可修改)。(接口地址为本地 IP:9898)

2.设置客户端

导入 MoneyCollect Android SDK 并初始化 SDK.

2.1 在 build.gradle 文件中添加配置

要安装 SDK,请将以下内容添加 moneycollect-android 到文件的 build.gradle 依赖块中:

repositories {
   jcenter()
   maven{ url "https://raw.githubusercontent.com/MoneyCollect/moneycollect-api-android-demo/mcsdk" }
 }

2.2 将 ViewBinding 和 MoneyCollect 库添加到应用程序的主模块 build.gradle 中

buildFeatures{
        viewBinding = true
   }
dependencies {
   // 具体版本号根据您的需求确定
   implementation "com.moneycollect.payment:android_mc:0.0.1"
}

2.3 初始化 SDK

在项目应用程序中初始化 MoneyCollect Android SDK

/**
* context: Context,            (context)
* publishableKey: String,      (publishable key)
* customerServerUrl: String?   (Local ip:9898)
**/
MoneyCollectSdk.init(this, "live_pu_OGJ0EidwEg4GjymEiRD7cUBk7IQIYmhwhJlUM****","http://192.168.2.100:9898/")

3.构造发起交易的数据参数,然后启动支付活动

商家构造交易请求参数并点击结帐按钮以启动支付活动。( TestRequestData 是数据常量类,请查看MoneyCollect-API-Android-demo获取更多详细信息)

// ...
class PaymentSheetDemoActivity: AppCompatActivity() {

    // 实例化RequestCreatePayment对象
    var testRequestPayment = TestRequestData.testRequestPayment
    // 实例化RequestConfirmPayment对象
    var testConfirmPayment = TestRequestData.testConfirmPayment
    // 实例化RequestPaymentMethod对象
    var testRequestPaymentMethod = TestRequestData.testRequestPaymentMethod
    // support payment credit card
    var testBankIvList = TestRequestData.testBankIvList
    // customerId
    var customerId = TestRequestData.customerId
    // ...
    fun presentPaymentSheet() {
        // PayCardActivity包含SaveWithPaymentCardFragment和AddWithPaymentFragment,支持相互切换
        var intent = Intent(this, PayCardActivity::class.java)
        // 实例化Bundle对象
        var bundle = Bundle()
        // 传递 currentPaymentModel
        bundle.putSerializable(Constant.CURRENT_PAYMENT_MODEL, currentPaymentModel)
        // 传递 RequestCreatePayment
        bundle.putParcelable(Constant.CREATE_PAYMENT_REQUEST_TAG, testRequestPayment)
        // 传递 RequestConfirmPayment
        bundle.putParcelable(Constant.CONFIRM_PAYMENT_REQUEST_TAG, testConfirmPayment)
        // 传递 customerId
        bundle.putString(Constant.CUSTOMER_ID_TAG, TestRequestData.customerId)
        // 传递 default RequestPaymentMethod
        bundle?.putParcelable(Constant.CREATE_PAYMENT_METHOD_REQUEST_TAG, testRequestPaymentMethod)
        // 传递默认的 supportBankList
        bundle?.putSerializable(Constant.SUPPORT_BANK_LIST_TAG, testBankIvList)
        intent.putExtra(CURRENT_PAYMENT_BUNDLE, bundle)
        // 开始付款
        startActivityLauncher.launch(intent)
    }

    private val startActivityLauncher: ActivityResultLauncher<Intent> =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        }
}

客户通过单击“立即支付”按钮完成付款后,付款的相关窗口将会被关闭并返回到 PaymentSheetDemoActivity.同时,将回拨付款 _ 结果 。

// ...
class PaymentSheetDemoActivity: AppCompatActivity() {

     private val startActivityLauncher: ActivityResultLauncher<Intent> =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == Constant.PAYMENT_RESULT_CODE) {
                // 付款结果
                var payment =
                    it.data?.getParcelableExtra<Payment>(Constant.PAYMENT_RESULT_PAYMENT)
                if (payment != null) {
                    when (payment.status) {
                        Constant.PAYMENT_SUCCEEDED -> {
                            Log.e(TAG, Constant.PAYMENT_SUCCEEDED)
                        }
                        Constant.PAYMENT_FAILED -> {
                            payment?.errorMessage?.let { it1 ->
                                Log.e(TAG, it1)
                            }
                        }
                        Constant.PAYMENT_UN_CAPTURED -> {
                            Log.e(TAG, Constant.PAYMENT_UN_CAPTURED_MESSAGE)
                        }
                        Constant.PAYMENT_PENDING -> {
                            Log.e(TAG, Constant.PAYMENT_PENDING_MESSAGE)
                        }
                        Constant.PAYMENT_CANCELED -> {
                            Log.e(TAG, Constant.PAYMENT_CANCELED_MESSAGE)
                        }
                        else -> {
                            Log.e(TAG, Constant.PAYMENT_PENDING_MESSAGE)
                       }
                    }
                }
            }

        }
}

使用完成块处理付款结果。

如果付款因错误而失败,请向您的客户显示相应的消息,以便他们可以采取措施并重试。如果没有发生错误,请告诉您的客户付款成功。

// ...
public class PaymentSheetDemoActivity extends AppCompatActivity {

   // 实例化RequestCreatePayment对象
    RequestCreatePayment testRequestPayment = TestRequestData.Companion.getTestRequestPayment();
    // 实例化RequestConfirmPayment对象
    RequestConfirmPayment testConfirmPayment = TestRequestData.Companion.getTestConfirmPayment();
    // 实例化RequestPaymentMethod对象
    RequestPaymentMethod testRaymentMethod = TestRequestData.Companion.getTestRequestPaymentMethod();
    // 支持信用卡支付
    List<Integer>  testBankIvList = TestRequestData.Companion.getTestBankIvList();
    // customerId
    String customerId = TestRequestData.Companion.getCustomerId();
    // ...
    private void presentPaymentSheet() {
        // PayCardActivity包含SaveWithPaymentCardFragment和AddWithPaymentFragment,支持相互切换
        Intent intent = new Intent(this, PayCardActivity.class);
        // 实例化Bundle对象
        Bundle bundle = new Bundle();
        // 传递 currentPaymentModel
        bundle.putSerializable(
                Constant.CURRENT_PAYMENT_MODEL,
                currentPaymentModel
        );
        // 传递 RequestCreatePayment
        bundle.putParcelable(
                Constant.CREATE_PAYMENT_REQUEST_TAG,
                testRequestPayment
        );
        // 传递 RequestConfirmPayment
        bundle.putParcelable(
                Constant.CONFIRM_PAYMENT_REQUEST_TAG,
                testConfirmPayment
        );
        // 传递 currentId
        bundle.putString(
                Constant.CUSTOMER_ID_TAG,
                customerId
        );
        // 传递 RequestPaymentMethod
        bundle.putParcelable(Constant.CREATE_PAYMENT_METHOD_REQUEST_TAG, testRaymentMethod);
        // 传递默认的 supportBankList
        bundle.putSerializable(Constant.SUPPORT_BANK_LIST_TAG, TestRequestData.Companion.getTestBankIvList());
        intent.putExtra(CURRENT_PAYMENT_BUNDLE, bundle);
        startActivityLauncher.launch(intent);
    }

    private ActivityResultLauncher<Intent> startActivityLauncher =
            PaymentSheetDemoActivity.this.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
                @SuppressLint("LongLogTag")
                @Override
                public void onActivityResult(ActivityResult result) {
                }
            });
}

客户通过点击“立即支付”按钮完成付款后,付款活动将被取消并返回到 PaymentSheetDemoActivity.同时,将回拨付款 _ 结果 _ 付款。

// ...
public class PaymentSheetDemoActivity extends AppCompatActivity {

         private ActivityResultLauncher<Intent> startActivityLauncher =
            PaymentSheetDemoActivity.this.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
                @SuppressLint("LongLogTag")
                @Override
                public void onActivityResult(ActivityResult result) {
                    // 付款结果
                    if (result.getResultCode() == Constant.PAYMENT_RESULT_CODE) {
                        Payment payment =
                                result.getData().getParcelableExtra(Constant.PAYMENT_RESULT_PAYMENT);
                        if (payment != null) {
                            if(payment.getStatus() != null) {
                                switch (payment.getStatus()) {
                                    case Constant.PAYMENT_SUCCEEDED:
                                        Log.e(TAG, Constant.PAYMENT_SUCCESSFUL_MESSAGE);
                                        break;
                                    case Constant.PAYMENT_FAILED:
                                        Log.e(TAG, payment.getErrorMessage());
                                        break;
                                    case Constant.PAYMENT_UN_CAPTURED:
                                        Log.e(TAG, Constant.PAYMENT_UN_CAPTURED_MESSAGE);
                                        break;
                                    case Constant.PAYMENT_PENDING:
                                        Log.e(TAG, Constant.PAYMENT_PENDING_MESSAGE);
                                        break;
                                    case Constant.PAYMENT_CANCELED:
                                        Log.e(TAG, Constant.PAYMENT_CANCELED_MESSAGE);
                                        break;
                                    default:
                                        Log.e(TAG, Constant.PAYMENT_PENDING_MESSAGE);
                                        break;
                                }
                            }else {
                                Log.e(TAG, Constant.PAYMENT_PENDING_MESSAGE);
                            }
                        }
                    }
                }
            });
}

处理付款结果。

如果付款因错误而失败,请向您的客户显示相应的消息,以便他们可以采取措施并重试。如果没有发生错误,请告诉您的客户付款成功。

4.其他测试资源

您可以使用几个测试卡来确保您的集成已准备好投入生产。将它们与任何 CVC、邮政编码和未来到期日期一起使用。

Last updated