﻿# Customer Portal Management

The Customer Portal is a self-service management page provided by PayerMax for subscription merchants, aimed at end users. Merchants only need to place an entry point in their own system and create an access link when the user clicks it, and the user can then enter the portal. Users can view subscriptions, change subscription payment methods, cancel subscriptions, view bills, and maintain their personal information.

## 1. Before You Start

Please complete the following preparations:

- A subscription plan has been created via "Create Subscription", and there are manageable subscriptions under the username;

- A Portal configuration has been created in MMC and the `portalId` has been obtained;

- The merchant system already has a user login system and can provide a fixed `userId` for each user, which is consistent with the `userId` passed when creating the subscription;

- Server-side authentication information (`appId`, `merchantNo`, `signing key`, etc.) is ready;

- Callback notifications for subscription status changes have been integrated.

## 2. Integration Flow Overview

The Customer Portal integration is generally divided into three stages: **Configure Portal**, **Create Portal Session**, and **Self-Service Management and Result Notification**.

```mermaid
%%{init: {'theme': 'base', 'themeVariables': {
    'primaryColor': '#ffffff',
    'edgeLabelBackground':'#ffffff',
    'actorBkg': '#e8f0fe',
    'actorBorder': '#0066cc',
    'actorTextColor': '#000000',
    'activationBkgColor': '#cce5ff',
    'sequenceNumberColor': '#000000',
    'noteBkgColor': '#005bb5',
    'noteTextColor': '#ffffff',
    'noteBorderColor': '#005bb5'
}}}%%
sequenceDiagram
    participant U as End User
    participant M as Merchant System
    participant P as PayerMax Customer Portal

    rect rgb(234, 244, 255)
        Note over U,P: 1. Enter Portal
        U->>M: 1. Log in to the merchant system and click the portal entry
        M->>P: 2. Create Portal Session (portalId, userId, returnUrl)
        P-->>M: 3. Return session_url
        M-->>U: 4. Redirect or open a new page to open session_url
        U->>P: 5. Open session_url and enter the customer portal
        P-->>U: 6. Identify or create customer by merchant + userId, display subscriptions, bills, and personal information
    end

    rect rgb(234, 244, 255)
        Note over U,P: 2. Self-Service Management
        U->>P: 7. Change payment method / Cancel subscription / Revoke cancellation / Edit email
        P-->>U: 8. Return processing result (complete binding via checkout when changing payment method)
        P-->>M: 9. Webhook notifies subscription change result
    end

    rect rgb(234, 244, 255)
        Note over U,P: 3. Return to Merchant
        U->>M: 10. Click Return to {Merchant Name} to return to the merchant page
    end
```

## 3. Customer Portal Configuration

Merchants complete portal creation, feature toggle configuration, and preview in MMC.

- **Entry**: MMC → Acquiring Products → Subscription Management → Customer Portal Configuration;
- **Permission**: Permissions are assigned to the super administrator by default, and you can assign permissions to corresponding operations personnel again;
- **Effective Scope**: Merchant membership number dimension.

### 3.1 Manage Portal Configuration

When entering the page for the first time, there are default configuration items. You can update the default configuration or "Create a new Portal configuration" and name it. After creation, the page displays the configuration name and `portalId`, and supports copying the `portalId` and renaming it. Unmodified configuration items take effect according to default values.

You can create multiple sets of configurations, for example, setting different feature toggles for different businesses. After switching configurations, the configuration corresponding to the current `portalId` is displayed below.

### 3.2 Configuration Items

|**Configuration Module**|**Configuration Item**|**Description**|
|---|---|---|
|Update Subscription|Allow changing payment method|When enabled, users can change the payment method used for subsequent renewals of this subscription.|
|Cancel Subscription|Cancellation method|When enabled, users can cancel the subscription on the subscription details page. The specific method can be one of two options: **Cancel Immediately**, or **Cancel at Period End**. When Cancel at Period End is selected, users can revoke the cancellation before the period ends.|
|Billing Information|Allow editing personal information|When enabled, users are allowed to edit personal information.|
|Billing History|Allow viewing bills|When enabled, billing history is displayed.|
|Merchant Information|Title and Return URL|The title displayed on the portal and the page address to return to after the user clicks "Return to {Merchant Name}".|

## 4. Create Portal Session

After the user clicks the portal entry in the merchant system, the merchant server calls the create Portal Session API (API name: portalSessionCreate) to generate a portal access address for the user.

Test environment: https://pay-gate-uat.payermax.com/aggregate-pay/api/gateway/portalSessionCreate

Production environment: https://pay-gate.payermax.com/aggregate-pay/api/gateway/portalSessionCreate

### 4.1 API Request

**Request Example:**

```json
{
  "version": "1.5",
  "keyVersion": "1",
  "requestTime":   "2025-02-26T05:00:00+00:00",
  "appId":   "<your-app-id>",
  "merchantNo":   "<your-merchant-no>",
  "data": {
    "portalId":   "<your-portal-id>",
    "requestId":   "<your-request-id>",
    "outUserId":   "user_10001",
    "frontCallbackUrl":   "https://your-domain.com/account"
  }
}
```

### 4.2 API Response

**Response Example:**

```json
{
    "msg": "",
    "code": "APPLY_SUCCESS",
    "data": {
        "redirectUrl": "https://billing-uat.payermax.com/#/home?sessionId=<session-id>&merchantId=<your-merchant-no>&appId=<your-app-id>",
        "requestId": "<your-request-id>",
        "sessionId": "<session-id>",
        "outUserId": "user_10001",
        "merchantNo": "<your-merchant-no>"
    }
}
```

### 4.3 Open Portal

- After the merchant system receives the redirectUrl, open it by redirecting or opening a new page;

- `redirectUrl` is a one-time credential. Please use it immediately after creation, and do not cache or forward it to other users.

### 4.4 Session Rules

**Validity Period**

- The default validity period of a Session after creation is **60 minutes**;

- After the user successfully enters the portal, the portal maintains the current access session. If the user exits or closes the page and accesses again, the merchant system needs to create a new Session.

**Invalidation Handling**

When the Session has expired, has been used, or is invalid, the page displays a "Page expired/invalid" prompt page and provides an entry to "Return to merchant page". The user needs to return to the merchant system, obtain a new Session, and then enter the portal again.

**Security**

- The access address is tamper-proof and does not contain plaintext user information;

- Users cannot enter other users' portals by modifying the link;

- When creating a Session, PayerMax verifies whether the `portalId` matches the merchant.

### 4.5 Error Codes

|**Scenario**|**Error Code**|**Description**|
|---|---|---|
|Required parameter missing or invalid format|*PARAMS\_INVALID*|PARAMS is invalid\.|
|Repeatedly creating a new session using the requestId of an expired session|*SESSION\_EXPIRED*|Portal session has expired\.|
|Repeatedly creating a new session using the requestId of a session created within the validity period; the session URL will be returned at this time|*REPEAT\_REQUEST*
|Repeated request\.|
|UserId uses NA parameter|SESSION\_CREATE\_FAILED|NA user can not create portal session\.|
|portalId does not match the merchant or does not exist|*PORTAL\_NOT\_FOUND*|Portal configuration is not found\.|
|Failed to create session|*SESSION\_CREATE\_FAILED*|Failed to create portal session, please try it later\.|

## 5. Result Notification

When a subscription plan changes, PayerMax sends a notification to the subscription plan's callback address (callbackUrl). Whether the change is initiated by the user in the portal or by the merchant through the API, a notification will be sent.

The content that can be modified on the Portal includes: cancel subscription immediately, cancel subscription at period end, revoke cancel at period end, and change subscription payment method; after the above 4 operations are successful, the merchant will be notified of the operation result.

### 5.1 Cancel Subscription Immediately

Notification Example:

```json
{
  "code": "APPLY_SUCCESS",
  "msg": "",
  "keyVersion": "1",
  "appId": "3b242b56a8b64274bcc37dac281120e3",
  "merchantNo": "020213827212251",
  "notifyTime": "2022-01-17T09:33:54.540+00:00",
  "notifyType": "SUBSCRIPTION",
  "data": {
    "subscriptionRequestId": "testRequestId0001",
    "userId": "testUserId0001",
    "subscriptionPlan": {
      "subscriptionNo": "SUB20250417120949065211234",
      "subscriptionStatus": "CANCEL"
    }
  }
}
```

### 5.2 Set Cancel Subscription at Period End

Notification Example:

```json
{
    "code": "APPLY_SUCCESS",
    "msg": "",
    "keyVersion": "1",
    "appId": "3b242b56a8b64274bcc37dac281120e3",
    "merchantNo": "020213827212251",
    "notifyTime": "2022-01-17T09:33:54.540+00:00",
    "notifyType": "SUBSCRIPTION_UPDATE",
    "data": {
        "userId": "user_8fa3b2c1d4e5",
        "subscriptionRequestId": "SR20260909103012345678",
        "subscriptionPlan": {
            "subscriptionNo": "SUB20260909000123456789",
            "subscriptionStatus": "ACTIVE"
        },
        "updateSource": "CUSTOMER", // MERCHANT/CUSTOMER, operation source enum
        "updateType": "*SET_CANCEL_AT_PERIOD_END*",
        "updateDetail": {
            "cancellation": {
                "cancelExecuteTime": "2026-01-17T09:33:54.540+00:00", // *Execution time of cancellation*
                "cancelAtPeriodEnd": true // *Set cancel at period end*
            }
        }
    }
}
```

### 5.3 Revoke Cancel Subscription at Period End

Notification Example:

```json
{
    "code": "APPLY_SUCCESS",
    "msg": "",
    "keyVersion": "1",
    "appId": "3b242b56a8b64274bcc37dac281120e3",
    "merchantNo": "020213827212251",
    "notifyTime": "2022-01-17T09:33:54.540+00:00",
    "notifyType": "SUBSCRIPTION_UPDATE",
    "data": {
        "userId": "user_8fa3b2c1d4e5",
        "subscriptionRequestId": "SR20260909103012345678",
        "subscriptionPlan": {
            "subscriptionNo": "SUB20260909000123456789",
            "subscriptionStatus": "ACTIVE"
        },
        "updateSource": "CUSTOMER", // MERCHANT/CUSTOMER, operation source enum
        "updateType": "*REVOKE_CANCEL*", 
        "updateDetail": {
            "cancellation": {
                "revokeCancelTime": "2026-01-17T09:33:54.540+00:00" // *Time of revoking cancellation*
            }
        }
    }
}
```

### 5.4 Change Subscription Payment Method

Notification Example:

```json
{
    "code": "APPLY_SUCCESS",
    "msg": "",
    "keyVersion": "1",
    "appId": "3b242b56a8b64274bcc37dac281120e3",
    "merchantNo": "020213827212251",
    "notifyTime": "2022-01-17T09:33:54.540+00:00",
    "notifyType": "SUBSCRIPTION_UPDATE",
    "data": {
        "userId": "user_8fa3b2c1d4e5",
        "subscriptionRequestId": "SR20260909103012345678",
        "subscriptionPlan": {
            "subscriptionNo": "SUB20260909000123456789",
            "subscriptionStatus": "ACTIVE"
        },
        "updateSource": "CUSTOMER", // MERCHANT/CUSTOMER, operation source enum
        "updateType": "*CHANGE_PAYMENT_METHOD*",
        "updateDetail": {
            "paymentMethod": { // Updated payment method
                "paymentMethodType": "CARD",
                "cardOrg": "VISA",
                "targetOrg": "PayerMax",
                "cardInfo": {
                    "cardOrg": "VISA",
                    "type": "PAN",
                    "source": "CARD",
                    "totalCardOrg": [
                        "VISA",
                        "MASTERCARD"
                    ],
                    "cardBinNo": "411111",
                    "cardNumber": "411111******1234",
                    "cardHolderName": "J*** Doe",
                    "cardType": "CREDIT",
                    "cardLast4": "1234",
                    "country": "US",
                    "cardExpirationYear": "2028",
                    "cardExpirationMonth": "12"
                }
            }
        }
    }
}
```

Please update the subscription status and user service entitlements in your system based on the notifications sent by PayerMax and the queried subscription status.
