[#1396] How to connect initialize payment to an existing invoice

Migrated from Redmine #1396 | Author: JM Kuizon
Status: New | Priority: High, I’m very impatient | Created: 2025-03-06


Hi,

I’m experiencing an issue with the SB Pay payment API integration for our membership system. After a successful payment, we’re seeing duplicate payment records in the SB Pay dashboard.

h2. Current Implementation Flow

  • First, I create a membership instance

  await makeSimplyBookApiRequest(`/admin/memberships/make-membership-instance`, ApiMethods.POST, {
      membership_id: membershipId,
      period_start: new Date().toISOString().split('T')[0],
      is_invoice_needed: 1,
      payment_processor: 'delay',
      amount,
      clients: [clientId],
    });

Here is the code for it:


async initializePayment(paymentData: InitializePaymentDto) {
    try {
      const result = await this.simplyBookPayService.initializePayment({
        ...paymentData,
        paymentSystem: 'stripe',
        currency: 'SGD',
        timestamp: new Date().toISOString().replace('Z', '+00:00'),
        algo: 'sha256',
        autoMakeInvoiceOnReceivePayment: true,
      });

      return result;
    } catch (error) {
      throw new Error(`Failed to initialize payment: ${error.message}`);
    }
  }

And here is the sample paymentData for it:


{
    "referenceId": "312",
    "description": "Payment for membership of Reed Diaz",
    "lines": [
        {
            "name": "Commercial Organization (03.06.2025 - 03.06.2026)",
            "qty": 1,
            "price": 8000,
            "amount": 8000
        },
        {
            "name": "Additional Seats",
            "description": "Additional seats for the membership",
            "qty": 6,
            "price": 300,
            "amount": 1
        }
    ],
    "amount": 9,
    "customer": {
        "referenceId": "329",
        "name": "Reed Diaz",
        "email": "jm.kuizon+test25@symph.co",
        "phone": "+6512313121311"
    },
    "callbacks": {
        "returnUrl": "http://localhost:4200/account/create?strapiId=timpb4wjivpuqy6e3yob087y&clientId=329&membershipType=organisation",
        "cancelUrl": "http://localhost:4200?cancel=true",
        "failureUrl": "http://localhost:4200?error=true",
        "successUrl": "http://localhost:4200?success=true"
    }
}

h2. Issue

After a successful payment, I’m seeing duplicate entries in the SB Pay dashboard (screenshot: https://drive.google.com/file/d/1g5Rpi3hr4VSqkKIONVeJdWqwnN6I5dbZ/view?usp=sharing).

h2. Question

How can I properly update our existing membership instance as “paid” rather than creating new payment data? Is there a specific webhook or callback I should be implementing to handle successful payments?

I appreciate your assistance.

Thank you.

JM Kuizon wrote:

h1. UPDATE

We change the membership flow to the following:

  • First, we create a payment link and send it to the client. For the reference id we just fetch the id of the last invoice then add 1 to it, to make the reference id unique.
  • Next, if the payment is successful, that is the time we will create a membership instance:

Here is the updated create a membership instance function:


await makeSimplyBookApiRequest(`/admin/memberships/make-membership-instance`, ApiMethods.POST, {
      membership_id: membershipId,
      period_start: new Date().toISOString().split('T')[0],
      amount,
      clients: [clientId],
    });

    return { success: true };

h2. Note:

We are implementing a custom payment system.

Thank you.