使用curl的Paypal自适应支付

时间:2014-10-15 10:44:06

标签: php paypal

在我的网站上,我希望实现自适应paypal支付方式。是否可以使用curl实现。任何人都可以为自适应支付提供php代码

3 个答案:

答案 0 :(得分:1)

以下是卷曲PayPal调用的代码

首先,对于自适应付款,您需要使用' clientId'生成访问令牌。和秘密密钥'。然后使用该访问令牌,您可以调用Paypal自适应支付API。

我举了一个生成访问令牌的例子。然后从paykey获取交易详情(根据您的要求更改您的电话)。

//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);

您可以参考paypal curl call的参考示例 https://github.com/paypal/rest-api-curlsamples/blob/master/execute_all_calls.php

答案 1 :(得分:0)

答案 2 :(得分:0)

  //This example for making a payment 

//适用于沙箱模式 $ apiUrl ='https://svcs.sandbox.paypal.com/AdaptivePayments/Pay';

//对于实时模式 // $ apiUrl ='https://svcs.paypal.com/AdaptivePayments/Pay';

$_username = "**********";
$_password = "**********";
$_ipaddress = "**********";
$_application_id = "**********";
$returnUrl = "**********"
$cancelUrl = "****************";
    $receivers = array(
        array(
            'email' => "receiver1@abc.com",
            'amount' => 50,
            'primary' => true
        ),
        array(
            'email' => "receiver2@abc.com",
            '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.