ઈ-કોમર્સ ચેકઆઉટ સિસ્ટમમાં પેમેન્ટ ઈન્ટેન્ટ્સ સાથે કાર્ટ અપડેટ્સ મેનેજ કરવાની પગલું-દર-પગલું માર્ગદર્શિકા.

આ બ્લોગ પોસ્ટમાં, આપણે ઈ-કોમર્સ ચેકઆઉટ સિસ્ટમમાં પેમેન્ટ ઈન્ટેન્ટ્સ સાથે કાર્ટ અપડેટ્સ મેનેજ કરવાની પ્રક્રિયા વિશે વાત કરીશું. આપણે પ્રક્રિયાને સરળ પગલાંમાં વિભાજિત કરીશું અને બેકએન્ડ (PHP) અને ફ્રન્ટએન્ડ (જાવાસ્ક્રિપ્ટ) બંનેના અમલ માટે કોડ ઉદાહરણો પ્રદાન કરીશું. અમારો હેતુ એ છે કે પેમેન્ટ્સ ઓથરાઈઝ થાય અને માત્ર વેચનારની મંજૂરી મેળવ્યા પછી જ કૅપ્ચર થાય, ભલે કાર્ટ પેમેન્ટ ઈન્ટેન્ટ બનાવ્યા પછી અપડેટ થતું હોય.

પગલું 1: પ્રવાહને સમજવું.

સૌપ્રથમ, ચાલો પેમેન્ટ ઈન્ટેન્ટ્સ સાથે કાર્ટ અપડેટ્સ મેનેજ કરવાનો કુલ પ્રવાહ સમજીએ:

  1. વપરાશકર્તા “Pay via Card” પર ક્લિક કરે છે.
  2. સિસ્ટમ ચકાસે છે કે કાર્ટમાં પેમેન્ટ ઈન્ટેન્ટ છે કે નહીં:
    • જો છે, તો પેમેન્ટ ઈન્ટેન્ટ અપડેટ થાય છે.
    • જો નહીં હોય, તો નવો પેમેન્ટ ઈન્ટેન્ટ બનાવવામાં આવે છે.
  3. સિસ્ટમ PaymentIntentSecret મેળવે છે.
  4. ફ્રન્ટએન્ડ સિક્રેટનો ઉપયોગ કરીને કાર્ડ વિગતો સુરક્ષિત રીતે એકત્રિત કરે છે.
  5. ચુકવણીને અધિકૃત કરવામાં આવે છે (રકમ રાખવામાં આવે છે) પરંતુ કૅપ્ચર કરવામાં આવતી નથી.
  6. ચુકવણી કૅપ્ચર કરવા માટે સિસ્ટમ વેચનારની મંજૂરીની રાહ જુએ છે.
  7. જો કાર્ટ અપડેટ થાય છે, તો સિસ્ટમ સુનિશ્ચિત કરે છે કે પેમેન્ટ ઈન્ટેન્ટ પણ અપડેટ થાય.

પગલું 2: ફ્રન્ટએન્ડ અમલ.

કાર્ડ વિગતો એકત્રિત કરવી અને ચુકવણી અધિકૃત કરવી.

ફ્રન્ટએન્ડમાં, આપણે વપરાશકર્તાના “કાર્ડ દ્વારા ચુકવણી”ના ક્રિયાને સંભાળવાની અને કાર્ડની વિગતો સુરક્ષિત રીતે એકત્રિત કરવાની જરૂર છે. આ રીતે તમે તેનો અમલ કરી શકો છો:

async function handlePayViaCard() {
    const response = await fetch('/api/getPaymentIntentSecret', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ cartId: cart.id }),
    });
    const { paymentIntentSecret } = await response.json();
    await handleCardDetailsSubmission(paymentIntentSecret);
}

async function handleCardDetailsSubmission(paymentIntentSecret) {
    // Collect card details using the payment processor's SDK
    const { cardDetails } = await collectCardDetails(paymentIntentSecret);

    const response = await fetch('/api/authorizePayment', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ paymentIntentId: cardDetails.paymentIntentId }),
    });

    const paymentResult = await response.json();
    if (paymentResult.success) {
        // Payment authorization successful
        // Wait for seller approval
        await waitForSellerApproval(cardDetails.paymentIntentId);
    } else {
        // Handle payment authorization failure
    }
}

async function waitForSellerApproval(paymentIntentId) {
    // Logic to wait for seller approval (e.g., polling or real-time updates)
    const sellerApproved = await checkSellerApproval();

    if (sellerApproved) {
        const response = await fetch('/api/capturePayment', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ paymentIntentId }),
        });
        const paymentResult = await response.json();
        if (paymentResult.success) {
            // Payment captured successfully
        } else {
            // Handle payment capture failure
        }
    } else {
        const response = await fetch('/api/cancelPayment', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ paymentIntentId }),
        });
        const cancelResult = await response.json();
        if (cancelResult.success) {
            // Payment canceled successfully
        } else {
            // Handle payment cancelation failure
        }
    }
}

async function updateCart(updates) {
    const response = await fetch('/api/updateCart', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ cartId: cart.id, updates }),
    });
    const updatedCart = await response.json();
    if (updatedCart.cart.payment_intent_id) {
        // Update the payment intent as well
        await fetch('/api/getPaymentIntentSecret', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({ cartId: cart.id }),
        });
    }
}

પગલું 3: બેકએન્ડ અમલ.

પેમેન્ટ ઈન્ટેન્ટ્સ અને કાર્ટ અપડેટ્સને મેનેજ કરવું.

બેકએન્ડમાં, આપણે પેમેન્ટ ઈન્ટેન્ટ્સનું સર્જન અને અપડેટ કરવું પડશે, તેમજ યોગ્ય રીતે કાર્ટ અપડેટ્સને સંભાળવા પડશે.

// Get PaymentIntentSecret
$app->post('/api/getPaymentIntentSecret', function ($request, $response, $args) {
    $data = $request->getParsedBody();
    $cartId = $data['cartId'];
    
    // Retrieve cart from database
    $cart = getCartById($cartId);
    $paymentIntent = null;

    if ($cart['payment_intent_id']) {
        // Update existing payment intent
        $paymentIntent = updatePaymentIntent($cart['payment_intent_id']);
    } else {
        // Create new payment intent
        $paymentIntent = createPaymentIntent();
        updateCart($cartId, ['payment_intent_id' => $paymentIntent->id]);
    }

    return $response->withJson(['paymentIntentSecret' => $paymentIntent->client_secret]);
});

// Authorize Payment
$app->post('/api/authorizePayment', function ($request, $response, $args) {
    $data = $request->getParsedBody();
    $paymentIntentId = $data['paymentIntentId'];

    // Authorize the payment
    $paymentResult = authorizePayment($paymentIntentId);

    if ($paymentResult->success) {
        return $response->withJson(['success' => true]);
    } else {
        return $response->withJson(['success' => false, 'error' => $paymentResult->error]);
    }
});

// Capture Payment
$app->post('/api/capturePayment', function ($request, $response, $args) {
    $data = $request->getParsedBody();
    $paymentIntentId = $data['paymentIntentId'];

    // Capture the payment
    $paymentResult = capturePayment($paymentIntentId);

    if ($paymentResult->success) {
        return $response->withJson(['success' => true]);
    } else {
        return $response->withJson(['success' => false, 'error' => $paymentResult->error]);
    }
});

// Release Authorization (Cancel Payment)
$app->post('/api/cancelPayment', function ($request, $response, $args) {
    $data = $request->getParsedBody();
    $paymentIntentId = $data['paymentIntentId'];

    // Cancel the payment authorization
    $paymentResult = cancelPayment($paymentIntentId);

    if ($paymentResult->success) {
        return $response->withJson(['success' => true]);
    } else {
        return $response->withJson(['success' => false, 'error' => $paymentResult->error]);
    }
});

// Update Cart
$app->post('/api/updateCart', function ($request, $response, $args) {
    $data = $request->getParsedBody();
    $cartId = $data['cartId'];
    $updates = $data['updates'];

    // Retrieve cart from database
    $cart = getCartById($cartId);

    if ($cart['payment_intent_id']) {
        // Update payment intent if it exists
        updatePaymentIntent($cart['payment_intent_id']);
    }

    // Update the cart
    $updatedCart = updateCart($cartId, $updates);

    return $response->withJson(['cart' => $updatedCart]);
});

પગલું 4: કાર્ટ અપડેટ્સ સંભાળવું.

જ્યારે પેમેન્ટ ઈન્ટેન્ટ બનાવ્યા પછી કાર્ટ અપડેટ થાય છે, ત્યારે અમને સુનિશ્ચિત કરવું પડશે કે પેમેન્ટ ઈન્ટેન્ટ મુજબ અપડેટ થાય. તેમાં ચકાસવું સામેલ છે કે કાર્ટમાં પેમેન્ટ ઈન્ટેન્ટ છે કે નહીં અને જરૂરી હોય ત્યારે તેને અપડેટ કરવું.

કાર્ટ અને પેમેન્ટ ઈન્ટેન્ટને અપડેટ કરવું (PHP બેકએન્ડ):

function updateCart($cartId, $updates) {
    // Retrieve the cart from the database
    $cart = getCartById($cartId);

    if ($cart['payment_intent_id']) {
        // Update the existing payment intent
        updatePaymentIntent($cart['payment_intent_id']);
    }

    // Update the cart details
    $updatedCart = saveCartUpdates($cartId, $updates);

    return $updatedCart;
}

function updatePaymentIntent($paymentIntentId) {
    // Logic to update the payment intent using the payment processor's API
    // ...
    return $updatedPaymentIntent;
}

function saveCartUpdates($cartId, $updates) {
    // Logic to save the cart updates to the database
    // ...
    return $updatedCart;
}

નિષ્કર્ષ

આ પગલાંઓને અનુસરીને, તમે તમારા ઈ-કોમર્સ ચેકઆઉટ સિસ્ટમમાં કાર્ટ અપડેટ્સ અને પેમેન્ટ ઈન્ટેન્ટ્સને અસરકારક રીતે મેનેજ કરી શકો છો. આ સુનિશ્ચિત કરે છે કે ચુકવણીઓ અધિકૃત છે અને માત્ર વેચનારની મંજૂરી પછી જ કૅપ્ચર થાય છે, ભલે પેમેન્ટ ઈન્ટેન્ટ બનાવ્યા પછી કાર્ટ અપડેટ થઈ હોય.

આ ફ્રન્ટએન્ડ અને બેકએન્ડ પ્રક્રિયાઓને એકત્રીત કરીને, તમે તમારા વપરાશકર્તાઓ માટે એક સરળ અને સુરક્ષિત ચેકઆઉટ અનુભવ બનાવી શકો છો. આ વિગતવાર પ્રવાહ સુનિશ્ચિત કરે છે કે તમામ પરિસ્થિતિઓ, જેમાં કાર્ટ અપડેટ્સ પણ શામેલ છે, યોગ્ય રીતે સંભાળવામાં આવે છે, જે તમારા ઈ-કોમર્સ પ્લેટફોર્મમાં પેમેન્ટ્સ મેનેજ કરવા માટે એક મજબૂત ઉકેલ પ્રદાન કરે છે.