Search code examples
phppaypalpaypal-ipnpaypal-sandboxpaypal-adaptive-payments

Adding custom variable to paypal parallel payments IPN PHP


Hello i'm trying to implement the PAYPAL PARALLEL API, and i'm trying to pass a custom variable to paypal IPN this my PHP code, its working without the custom variable, and i don"t know where to add this custom field:

$apiUrl = "https://svcs.sandbox.paypal.com/AdaptivePayments/";
$paypalUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_ap-payment&paykey=";

$headers = array(
    "X-PAYPAL-SECURITY-USERID : ".$paypal_user_id,
    "X-PAYPAL-SECURITY-PASSWORD : ".$paypal_pass,
    "X-PAYPAL-SECURITY-SIGNATURE : ".$paypal_user_signature,
    "X-PAYPAL-APPLICATION-ID : ".$paypal_app_id,
    "X-PAYPAL-REQUEST-DATA-FORMAT : JSON",
    "X-PAYPAL-RESPONSE-DATA-FORMAT : JSON"
);

// curl wrapper for sending thhings to paypal
function paypalSend($data,$call){
    global $apiUrl;
    global $headers;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL , $apiUrl.$call);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    return json_decode(curl_exec($ch),true);
}

//create the pay request
$createPacket = array(
    "actionType"=>"PAY",
    "currencyCode"=>"USD",
    "receiverList"=> array(
        "receiver" => array(
            array(
                "amount" => $amount,
                "email" => $email
            )
        )
    ),
    "returnUrl" => "http://www.mywebsite.com/finish_withdraw.php",
    "cancelUrl" => "http://www.mywebsite.com/cancel_withdraw.php",

    "requestEnvelope"=> array(
        "errorLanguage"=>"en_US",    // Language used to display errors
        "detailLevel"=>"ReturnAll"
    )
);

$response = paypalSend($createPacket, "Pay");

$payKey = $response['payKey'];
$paymentExecStatus = $response['paymentExecStatus'];

$detailPacket = array(
    "requestEnvelope"=> array(
        "errorLanguage"=>"en_US",    // Language used to display errors
        "detailLevel"=>"ReturnAll"
    ),
    "payKey" => $payKey,
    "receiverOptions" => array(
        array(
            "receiver"=> array("email" => $email),
            "invoiceData" => array(
                "item" => array(
                    array(
                        "name" => "product 1",
                        "price" => $amount,
                        "identifier" => "p1"
                    )
                )
            )
        )
    )
);

$response = paypalSend($detailPacket, "SetPaymentOptions");

//Wrapper to get payent details
$packet = array(
    "requestEnvelope"=> array(
        "errorLanguage"=>"en_US",    // Language used to display errors
        "detailLevel"=>"ReturnAll"
    ),
    "payKey" => $payKey,
);

$dets = paypalSend($packet, "GetPaymentOptions");

header('location: '.$paypalUrl.$payKey);

Solution

  • Unless paypal fixed something in the last 3 years, the ONLY way I could accomplish this was to send hyphenated data as the item_number

    "item_number1": "t-253-22768-",
    

    This format was "type" - "product id" - "user id" - "discount code"