xyz
xyz

Reputation: 45

Paypal adaptive payment using curl

In my website ,i wish to implement adaptive paypal payment method. Is it possible to implement using curl .Can any one provide php code for the adaptive payment

Upvotes: 0

Views: 1481

Answers (3)

Hariom Kesharwani
Hariom Kesharwani

Reputation: 1

  //This example for making a payment 

//For Sandbox mode $apiUrl = 'https://svcs.sandbox.paypal.com/AdaptivePayments/Pay';

//For live mode //$apiUrl = 'https://svcs.paypal.com/AdaptivePayments/Pay';

$_username = "**********";
$_password = "**********";
$_ipaddress = "**********";
$_application_id = "**********";
$returnUrl = "**********"
$cancelUrl = "****************";
    $receivers = array(
        array(
            'email' => "[email protected]",
            'amount' => 50,
            'primary' => true
        ),
        array(
            'email' => "[email protected]",
            'amount' => 20,
            'primary' => false
        )
    );
$requestEnvelope = array(
        'errorLanguage' => 'en_US',
        'detailLevel' => 'ReturnAll'
    );
    $packet = array(
        'actionType' => "PAY",
        'currencyCode' => "USD",
        'feesPayer' => "EACHRECEIVER",
        'receiverList' => array(
            'receiver' => $receivers
        ),
        'memo' => "Here you can write memo desc",
        'returnUrl' => $returnUrl,
        'cancelUrl' => $cancelUrl,
        'requestEnvelope' => $requestEnvelope
    );

    $headers = array(
        "X-PAYPAL-SECURITY-USERID: " . $_username,
        "X-PAYPAL-SECURITY-PASSWORD: " . $_password,
        "X-PAYPAL-SECURITY-SIGNATURE: " . $_signature,
        "X-PAYPAL-DEVICE-IPADDRESS: ".$_ipaddress,
        "X-PAYPAL-REQUEST-DATA-FORMAT: JSON",
        "X-PAYPAL-RESPONSE-DATA-FORMAT: JSON",
        "X-PAYPAL-APPLICATION-ID: " . $_application_id);
    $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($packet));
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    print_r( json_decode(curl_exec($ch), TRUE));
//After execution of request , you got a paykey and using that key you can receive payment from Buyer.

Upvotes: 0

Satish Shinde
Satish Shinde

Reputation: 2996

Here is the code to curl call for paypal

For adaptive payment first you need to generate access token with your 'clientId' and 'secret key'. Then with that access token you can call Paypal adaptive payment API.

I have given example of generating access token. and then fetching transaction details from paykey (you change your call as per your requirement).

//Get access token from client Id

                $ch = curl_init();
                $clientId = PAYPAL_CLIENT_ID;
                $secret = PAYPAL_CLIENT_SECRET;
                curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/oauth2/token');// Change it to "https://api.paypal.com/v1/oauth2/token" for live
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $secret);
                curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
                $result = curl_exec($ch);
                $accessToken = null;
                if (empty($result))
                    return 'Access Token could not generated';
                else {
                    $json = json_decode($result);
                    $accessToken = $json->access_token;
                }
                curl_close($ch);



                /**
                 * Get payment transaction details from
                 * - AccessToken
                 * - PayKey
                 */
                $curl = curl_init('https://api.sandbox.paypal.com/v1/payments/payment/' . $input['payKey']);
                curl_setopt($curl, CURLOPT_POST, false);
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($curl, CURLOPT_HEADER, false);
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                    'Authorization: Bearer ' . $accessToken,
                    'Accept: application/json',
                    'Content-Type: application/json'
                ));
                $response = curl_exec($curl);
                $result = json_decode($response);

You can have reference example of paypal curl call https://github.com/paypal/rest-api-curlsamples/blob/master/execute_all_calls.php

Upvotes: 1

Muhammad
Muhammad

Reputation: 3250

you can download using this link https://github.com/paypal/adaptivepayments-sdk-php

Upvotes: 0

Related Questions