Brett
Brett

Reputation: 47

PayPal IPN working for single cart items but not multiple cart items

I found this Paypal IPN code with Google Sheets and it does work for single items in my cart, but not for any multi cart items. I have no idea what I'm doing and I'm hoping to edit this code a little to get it to work for multiple cart items. Any help would be much appreciated. Thanks!

 function doPost(e) {

  var rowData = [];

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet()

  rowData.push(new Date(e.parameter.payment_date));
    rowData.push(e.parameter.payment_type);
    rowData.push(e.parameter.payment_status);    
    rowData.push(e.parameter.payer_email);
    rowData.push(e.parameter.item_name1);
    rowData.push(e.parameter.item_number1);
    rowData.push(e.parameter.quantity);
    rowData.push(e.parameter.first_name);
    rowData.push(e.parameter.last_name);
        rowData.push(e.parameter.address_street);
        rowData.push(e.parameter.address_city);
        rowData.push(e.parameter.address_state);
        rowData.push(e.parameter.address_zip);
  
  sheet.appendRow(rowData);
}

Here is the IPN message I get back minus editing some of my personal information...

mc_gross=11.97&protection_eligibility=Eligible&address_status=confirmed&item_number1=&item_number2=&payer_id=HJXtestY&item_number3=&address_street=testS. test Road&payment_date=19:07:58 May 12, 2021 PDT&payment_status=Completed&charset=windows-1252&address_zip=test&first_name=Brett&mc_fee=0.65&address_country_code=US&address_name=Brett - detestto&notify_version=3.9&custom=/xotio-media/2019/2019-07-13-Tushar-Crusher-Photos&payer_status=verified&business=xotestail.com&address_country=United States&num_cart_items=3&address_city=test&verify_sign=test-test.test-cfj7Q1YCC6WrT&payer_email=testl.com&txn_id=0BFtest11545&payment_type=instant&payer_business_name=Brett testphy - detestoto&last_name=testr&address_state=UT&item_name1=2019-07-13-Tushar-Crusher-Photos/Top-Col-de-Crush/19-TC-111644-190713-BP3_3724.jpg :: Download Original&receiver_email=testil.com&item_name2=2019-07-13-Tushar-Crusher-Photos/Top-Col-de-Crush/19-TC-111645-190713-BP3_3725.jpg :: Download Original&payment_fee=0.65&item_name3=2019-07-13-Tushar-Crusher-Photos/Top-Col-de-Crush/19-TC-111646-190713-BP3_3726.jpg :: Download Original&shipping_discount=0.00&quantity1=1&insurance_amount=0.00&quantity2=1&receiver_id=3SKEBUJZCV7AS&quantity3=1&txn_type=cart&discount=0.00&mc_gross_1=3.99&mc_currency=USD&mc_gross_2=3.99&mc_gross_3=3.99&residence_country=US&shipping_method=Default&transaction_subject=&payment_gross=11.97&ipn_track_id=e0b32d1a10a85

Upvotes: 0

Views: 138

Answers (1)

Preston PHX
Preston PHX

Reputation: 30369

Log the text of what you receive when there are multiple items, and change the code accordingly to parse that.

You can also review the IPN history of the receiver account, via:

Something like:

    rowData.push(e.parameter.payment_type);
    rowData.push(e.parameter.payment_status);    
    rowData.push(e.parameter.payer_email);
    rowData.push(e.parameter.first_name);
    rowData.push(e.parameter.last_name);
    rowData.push(e.parameter.address_street);
    rowData.push(e.parameter.address_city);
    rowData.push(e.parameter.address_state);
    rowData.push(e.parameter.address_zip);

    e.parameter.quantity1 = e.parameter.quantity1 || e.parameter.quantity || '';
    let i = 1;
    while(e.parameter['item_name'+i]) {
        rowData.push(e.parameter['item_name'+i]);
        rowData.push(e.parameter['item_number'+i]);
        rowData.push(e.parameter['quantity'+i]);
        i++;
    }

Better yet, stop using something ancient like IPN and change to a current v2/checkout/orders integration. Make two routes on your server, one for 'Create Order' and one for 'Capture Order', documented here. These routes should return only JSON data (no HTML or text). The latter one should (on success) store the cart items and payment details in your database (or, uhm, spreadsheet) before it does the return -- particularly purchase_units[0].payments.captures[0].id, the PayPal transaction ID.

Pair those two routes with the following approval flow: https://developer.paypal.com/demo/checkout/#/pattern/server

Upvotes: 2

Related Questions