Paypal Express Checkout定期付款

时间:2018-11-12 10:40:54

标签: express paypal response paypal-ipn checkout

我正在尝试在Paypal快速结帐中进行定期付款。使用以下代码成功创建了贝宝中的即时付款和重复资料。在Paypal中进行定期付款时,ipn响应错误,但我没有收到任何响应。请验证以下代码

SetExpressCheckout

$total = 88; //Total do carrinho do cliente
$item_id = 'CLT-06';
$nvp = array(
    'USER'                              => 'paypalsellerpay_api1.gmail.com',
    'PWD'                               => 'C572HJQVLFCTRNC8',
    'SIGNATURE'                         => 'APbFQnrgReyuSoEWhJ8cBfn34GiDATmy50R-KvFePYyoWPqBSA3JVkHp',
    'METHOD'                            => 'SetExpressCheckout',
    'VERSION'                           => '63',
    'L_BILLINGTYPE0'                    => 'RecurringPayments',     
    'cancelUrl'                         => 'siteurl/cancel.php',
    'returnUrl'                         => 'siteurl/retorno.php',
    'PAYMENTREQUEST_n_NOTIFYURL'        => 'siteurl/ipn_response.php',
    'PAYMENTREQUEST_0_AMT'              => $total,
    'PAYMENTREQUEST_0_CURRENCYCODE'     => 'USD',
    'PAYMENTREQUEST_0_PAYMENTACTION'    => 'Sale',  
    'AMT'                               => $total,
    'AUTOBILLOUTAMT'                    => 'AddToNextBilling',
    'L_BILLINGAGREEMENTDESCRIPTION0'    => 'NOC Live Express Test code',    
    'PAYMENTREQUEST_0_DESC'             => 'NOC Live Express Test code',    
    'L_PAYMENTREQUEST_0_NAME0'          => 'Recurring payment',
    'L_PAYMENTTYPE0'                    =>  'Any',
    'item_number'                       => $item_id,    
    'L_PAYMENTREQUEST_0_NUMBER0'        => $item_id,
    'PAYMENTREQUEST_0_PAYMENTREQUESTID' => $item_id,
    'L_PAYMENTREQUEST_0_DESC0'          => 'Live Express Test codes',
    'L_PAYMENTREQUEST_0_AMT0'           => $total,
    'CURRENCYCODE'                      => 'USD',
    'PAYMENTACTION'                     => 'Sale',
    'BILLINGPERIOD'                     => 'Day',
    'BILLINGFREQUENCY'                  =>  '3',    
    'NOSHIPPING'                        => '1',
    'ALLOWNOTE'                         => '1',


);

$curl = curl_init();

curl_setopt( $curl , CURLOPT_URL , 'https://api-3t.sandbox.paypal.com/nvp' ); //Link para ambiente de teste: https://api-3t.sandbox.paypal.com/nvp || https://api-3t.paypal.com/nvp
curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false );
curl_setopt( $curl , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt( $curl , CURLOPT_POST , 1 );
curl_setopt( $curl , CURLOPT_POSTFIELDS , http_build_query($nvp));
$response = urldecode(curl_exec($curl));
curl_close( $curl );
$response_data = array();
if ( preg_match_all( '/(?<name>[^\=]+)\=(?<value>[^&]+)&?/' , $response , $matches ) ) {
    foreach ( $matches[ 'name' ] as $offset => $name ) {
        $response_data[ $name ] = $matches[ 'value' ][ $offset ];
    }
}

if ( isset( $response_data[ 'ACK' ] ) && $response_data[ 'ACK' ] == 'Success' ) {
    $paypalURL = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
    $query = array(
        'cmd'   => '_express-checkout',
        'token' => $response_data[ 'TOKEN' ]
    );

    header( 'Location: ' . $paypalURL . '?' . http_build_query( $query ) );
} else {
    echo 'Transaction failed';
}

GetExpressCheckoutDetails,DoExpressCheckoutPayment

$total = 58; 
$init_total = 88;
$item_id = 'CLT-07';
if ( isset( $_GET[ 'token' ] ) ) {
    $token = $_GET[ 'token' ];
    $nvp = array(
        'TOKEN'                             => $token,
        'METHOD'                            => 'GetExpressCheckoutDetails',
        'VERSION'                           => '63',
        'USER'                              => 'paypalsellerpay_api1.gmail.com',
        'PWD'                               => 'C572HJQVLFCTRNC8',
        'SIGNATURE'                         => 'APbFQnrgReyuSoEWhJ8cBfn34GiDATmy50R-KvFePYyoWPqBSA3JVkHp',
    );

    $curl = curl_init();

    curl_setopt( $curl , CURLOPT_URL , 'https://api-3t.sandbox.paypal.com/nvp' ); 
    curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false );
    curl_setopt( $curl , CURLOPT_RETURNTRANSFER , 1 );
    curl_setopt( $curl , CURLOPT_POST , 1 );
    curl_setopt( $curl , CURLOPT_POSTFIELDS , http_build_query( $nvp ) );

    $response = urldecode( curl_exec( $curl ) );    
    $response_data = array();
    $response_data_get = array();

    if ( preg_match_all( '/(?<name>[^\=]+)\=(?<value>[^&]+)&?/' , $response , $matches ) ) {
        foreach ( $matches[ 'name' ] as $offset => $name ) {
            $response_data[ $name ] = $matches[ 'value' ][ $offset ];
        }
    }
    $response_data_get = $response_data;

    if ( isset( $response_data_get[ 'TOKEN' ] ) && isset( $response_data_get[ 'ACK' ]) ) {
        if ( $response_data_get[ 'TOKEN' ] == $token && $response_data_get[ 'ACK' ] == 'Success' ) {
            $nvp['TOKEN']                               = $response_data_get[ 'TOKEN' ];
            $nvp['PAYERID']                             = $response_data_get[ 'PAYERID' ];
            $nvp['AMT']                                 = $response_data_get[ 'AMT' ];
            $nvp['CURRENCYCODE']                        = $response_data_get[ 'CURRENCYCODE' ];
            $nvp['L_PAYMENTREQUEST_0_NUMBER0']          = $item_id;
            $nvp['PAYMENTREQUEST_0_PAYMENTREQUESTID']   = $item_id;
            $nvp['NOTIFYURL']                           = 'siteurl/ipn_response.php';           
            $nvp['METHOD']                              = 'DoExpressCheckoutPayment';
            $nvp['PAYMENTREQUEST_0_PAYMENTACTION']      = 'Sale';


            curl_setopt( $curl , CURLOPT_POSTFIELDS , http_build_query( $nvp ) );

            $response = urldecode( curl_exec( $curl ) );
            $response_data = array();

            if ( preg_match_all( '/(?<name>[^\=]+)\=(?<value>[^&]+)&?/' , $response , $matches ) ) {
                foreach ( $matches[ 'name' ] as $offset => $name ) {
                    $response_data[ $name ] = $matches[ 'value' ][ $offset ];
                }
            }   
            $response_data_recurr = array();
            $response_data_recurr = recurr_payment($token, $init_total,$total);

            if ( $response_data[ 'PAYMENTINFO_0_PAYMENTSTATUS' ] == 'Completed' ) {
                echo 'Before Payment <br>'.print_r($response_data_get,true);
                echo 'Sucess! <br>'.print_r($response_data,true);

                pay_log('Payment Success'); 
            } else {
                echo 'Transaction Failed On Payment Failed <br>'.print_r($response_data,true);  
                pay_log('Payment Failed');                      
            }
        } else {
            echo 'Transaction Failed On ACK & Token Failed <br>'.print_r($response_data,true);  
            pay_log('ACK && TOKEN Failed');                 
        }
    } else {
        echo 'Transaction Failed On ACK & Token Not set<br>'.print_r($response_data,true);  
        pay_log('ISSET ACK && TOKEN NOT SET');          
    }

    curl_close( $curl );
}

CreateRecurringPaymentsProfile

function recurr_payment($token, $init_total,$total){
    $nvp = array();
    $notifyurl='siteurl/ipn_response.php';
    $nvp = array(
        'USER'                              => 'paypalsellerpay_api1.gmail.com',
        'PWD'                               => 'C572HJQVLFCTRNC8',
        'SIGNATURE'                         => 'APbFQnrgReyuSoEWhJ8cBfn34GiDATmy50R-KvFePYyoWPqBSA3JVkHp',
        'METHOD'                            => 'CreateRecurringPaymentsProfile',
        'VERSION'                           => '63',
        'TOKEN'                             => $token,
        'SUBSCRIBERNAME'                    => 'Live Code Demo',
        'PROFILESTARTDATE'                  => '2018-11-12 T00:00:00Z', 
        'DESC'                              => 'NOC Live Express Test code',        
        'BILLINGPERIOD'                     => 'Day',
        'BILLINGFREQUENCY'                  => '1', 
        'TOTALBILLINGCYCLES'                => '10',
        'INITAMT'                           => $init_total,
        'AMT'                               => $total,
        'CURRENCYCODE'                      => 'USD',
        'SHIPPINGAMT'                       => '1',
        'NOTIFYURL'                         => urlencode($notifyurl),
        'AUTOBILLOUTAMT'                    => 'AddToNextBilling'
    );

    $curl = curl_init();

    curl_setopt( $curl , CURLOPT_URL , 'https://api-3t.sandbox.paypal.com/nvp' ); //Link para ambiente de teste: https://api-3t.sandbox.paypal.com/nvp || https://api-3t.paypal.com/nvp
    curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER , false );
    curl_setopt( $curl , CURLOPT_RETURNTRANSFER , 1 );
    curl_setopt( $curl , CURLOPT_POST , 1 );
    curl_setopt( $curl , CURLOPT_POSTFIELDS , http_build_query( $nvp ) );

    $response = urldecode( curl_exec( $curl ) );    
    $response_data = array();

    if ( preg_match_all( '/(?<name>[^\=]+)\=(?<value>[^&]+)&?/' , $response , $matches ) ) {
        foreach ( $matches[ 'name' ] as $offset => $name ) {
            $response_data[ $name ] = $matches[ 'value' ][ $offset ];
        }
    }
    return $response_data;
}

0 个答案:

没有答案