Skip to content

Instantly share code, notes, and snippets.

@daliborgogic
Last active March 20, 2025 10:20
Show Gist options
  • Save daliborgogic/df6728d8aeb13054049a7596f653146a to your computer and use it in GitHub Desktop.
Save daliborgogic/df6728d8aeb13054049a7596f653146a to your computer and use it in GitHub Desktop.
export interface PaymentLink {
data: PaymentLinkData
env: Env
error?: {
status: number
statusText: string
message: string
cause?: string
}
}
export interface OrderItem {
// Id of item
id?: string
// Item number
itemnumber?: string
// Product code
productcode?: string
// Quantity
qty?: number
price?: number
desc?: string
}
export interface CreateHashParams {
// Origin value will be SPL for Sinlgle Payment Link & MPL for Multiple Payment Link
origin: string
// Merchant Id
clientId: string
}
export interface Extra {
// Defines requested link type. Values may be SINGLE_LINK_PAY MENT and MULTIPLE_LINK_PA YMENT.
PAYMENTLINKTYPE: 'SINGLE_LINK_PAYMENT' | 'MULTIPLE_LINK_PAYMENT'
// Lınk Expiration Date Amount
// Lınk Expiration Date Amount
PAYMENTLINKEXPIRY?: number
// Defines unit type of Extra. PAYMENTLINKEXPIRY parameter
PAYMENTLINKEXPIRYUNIT?: 'D' | 'W' | 'M'
// Defines unit type of Extra. PAYMENTLINKEXPIRY parameter
PAYMENTLINKLANGUAGE?: string
// Defines whether amount can be changed in link page or not
PAYMENTLINKAMOUNT_EDITABLE?: 'true' | 'false'
// Defines whether phone info can be changed in link page or not
PAYMENTLINKCUSTOMERPHONEEDITABLE?: 'true' | 'false'
// Defines whether item id can be changed in link page or not
PAYMENTLINKITEMIDEDITABLE?: boolean
// Defines whether customer name can be changed in link page or not
PAYMENTLINKCUSTOMERNAMEEDITABLE?: 'true' | 'false'
// Defines whether address can be changed in link page or not
PAYMENTLINKADDRESS_EDITABLE?: 'true' | 'false'
// Defines whether item description can be changed in link page or not
PAYMENTLINKITEMDESCRIPTIONEDITABLE?: 'true' | 'false'
// Defines whether email info can be changed in link page or not
PAYMENTLINKCUSTOMEREMAILEDITABLE?: 'true' | 'false'
}
@daliborgogic
Copy link
Author

daliborgogic commented Mar 19, 2025

Using NestPay Generated Payment Links and Tokens

After sending the payment link request to NestPay's API, you'll receive a response that contains the payment link URL and a token. Here's how to use them:

Response Structure

When your payment link request is successful, NestPay will return an XML response containing:

  1. A payment link URL
  2. A token for reference/validation
  3. Additional status information

Using the Payment Link

  1. Extract the payment link URL from the response (typically in the URL or PaymentLink field)

  2. .Share the link with your customer through:

    • Email
    • SMS
    • our website/application interface
    • QR code
  3. Customer experience:

    • When customers click the link, they'll be directed to NestPay's secure payment page
    • They can complete the payment with their card details
    • Fields will be editable based on your configuration (the PAYMENTLINK*EDITABLE parameters)

Using the Token

The token serves several purposes:

  1. Transaction reference: Store this token in your database to associate with the order
  2. Status checking: Use the token to query the payment status through NestPay's status API
  3. Validation: When NestPay sends notifications about completed payments, verify using this token

Implementation Example

Here's how you might handle the response in your code:

TBD

Best Practices

  1. Store payment link information in your database:

    • Payment link URL
    • Token
    • Expiration date
    • Order ID and customer information
  2. Implement webhook handling to receive payment notifications from NestPay

  3. Provide status updates to customers after they've received the payment link

  4. Handle expiration by checking the status of payment links and potentially regenerating them if needed

  5. Implement proper error handling for cases where payment link creation fails

Store Payment Link Information in Database

async function storePaymentLinkInfo(paymentData) {
  // Using a generic database interface - replace with your specific DB
  const { db } = require('./your-database')
  
  try {
    await db.paymentLinks.create({
      orderId: paymentData.orderId,
      paymentLink: paymentData.paymentLink,
      paymentLinkId: paymentData.paymentLinkId,
      expiryDate: new Date(paymentData.expiryDate),
      transactionId: paymentData.transactionId,
      status: 'CREATED',
      createdAt: new Date(),
      updatedAt: new Date()
    })
    
    return { success: true }
  } catch (error) {
    console.error('Failed to store payment link info:', error)
    return { success: false, error: error.message }
  }
}

Send Payment Link to Customer

async function sendPaymentLinkToCustomer(customerEmail, paymentData) {
  const { sendEmail } = require('./your-email-service')
  
  const emailTemplate = `
    <h1>Complete Your Payment</h1>
    <p>Thank you for your order #${paymentData.orderId}.</p>
    <p>Please click the link below to complete your payment:</p>
    <a href="${paymentData.paymentLink}" style="display: inline-block; padding: 10px 20px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 4px;">Pay Now</a>
    <p>This payment link will expire on ${new Date(paymentData.expiryDate).toLocaleString()}.</p>
  `
  
  try {
    await sendEmail({
      to: customerEmail,
      subject: `Payment Link for Order #${paymentData.orderId}`,
      html: emailTemplate
    })
    
    return { success: true }
  } catch (error) {
    console.error('Failed to send payment link email:', error)
    return { success: false, error: error.message }
  }
}

Complete Payment Flow Diagram

sequenceDiagram
    participant M as Merchant System
    participant N as NestPay API
    participant C as Customer
    participant W as Webhook Handler
    
    M->>N: Create Payment Link Request
    N-->>M: Return Payment Link & Token
    M->>M: Store Link & Token
    M->>C: Send Payment Link (Email/SMS)
    C->>N: Open Payment Link
    C->>N: Enter Payment Details
    N-->>C: Confirm Payment
    N->>W: Send Payment Notification
    W->>M: Update Order Status
    M->>N: Verify Payment Status (Optional)
    N-->>M: Confirm Payment Status
    M->>C: Send Order Confirmation
Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment