phppaypalpayflowpropayflowlink

PHP code to refund a charge with Payflow Pro


There is no PHP SDK for PayPal Payflow Pro. How can a Payflow Pro credit card charge be refunded using PHP? How can this be done so that we are PCI compliant--without using a credit card number?


Solution

  • Here is some PHP code that uses curl to refund a credit card charge. This code is based on Radu Manole's SDK. For more info see Payflow Pro Developer's Guide: https://www.paypalobjects.com/webstatic/en_US/developer/docs/pdf/pp_payflowpro_guide.pdf

    $user = 'CHANGEME'; // API User Username
    $password = 'CHANGEME'; // API User Password
    $vendor = 'CHANGEME'; // Merchant Login ID
    
    // Reseller who registered you for Payflow or 'PayPal' if you registered
    // directly with PayPal
    $partner = 'PayPal'; 
    
    $sandbox = true;
    
    $transactionId = 'CHANGEME'; // The PNREF # returned when the card was charged
    $amount = '3';
    $currency = 'USD';
    
    $url = $sandbox ? 'https://pilot-payflowpro.paypal.com'
      : 'https://payflowpro.paypal.com';
    
    $params = array(
      'USER' => $user,
      'VENDOR' => $vendor,
      'PARTNER' => $partner,
      'PWD' => $password,
      'TENDER' => 'C', // C = credit card, P = PayPal
      'TRXTYPE' => 'C', //  S=Sale, A= Auth, C=Credit, D=Delayed Capture, V=Void                        
      'ORIGID' => $transactionId,
      'AMT' => $amount,
      'CURRENCY' => $currency
    );
    
    $data = '';
    $i = 0;
    foreach ($params as $n=>$v) {
        $data .= ($i++ > 0 ? '&' : '') . "$n=" . urlencode($v);
    }
    
    $headers = array();
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    $headers[] = 'Content-Length: ' . strlen($data);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HEADER, $headers);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec($ch);
    curl_close($ch);
    
    // Parse results
    $response = array();
    $result = strstr($result, 'RESULT');    
    $valArray = explode('&', $result);
    foreach ($valArray as $val) {
      $valArray2 = explode('=', $val);
      $response[$valArray2[0]] = $valArray2[1];
    }
    
    print_r($response);
    
    if (isset($response['RESULT']) && $response['RESULT'] == 0) {
      echo 'SUCCESS!';
    } else {
      echo 'FAILURE: ' . $response['RESPMSG'] . ' ['. $response['RESULT'] . ']';
    }