Monday 28 July 2014

Paypal Sandbox do-Directpayment, Authorization, Capture and Mass-payment - Example with Code Snippets

Paypal Sandbox do-Directpayment, Authorization, Capture and Mass-payment

Different Paypal Payment with API example (NVP Operation).
  1. DoDirect Payment - Sale
  2. Authorization - PayPal / Capture - PayPal
  3. Mass Pay
  4. Parallel Payment


Do-Direct Payment:
Direct Payment lets buyers pay using their credit cards and amount is directly transferred to merchant account.
https://developer.paypal.com/docs/classic/paypal-payments-pro/integration-guide/WPDirectPayment/


Payment Authorization:
In this case paypal will not charge immediately from the customer. We will hold the customer amount with the transaction-Id. Further with transaction-Id, we can decide whether charge the amount(Capture amount) OR return the amount to customer (Void Payment).
It have two parts
a. Authorization the customer money and store the transaction Id in our database.
b.  Capture Payment-  Charge the payment from hold amount with use of Transaction Id OR
     Void Payment – Cancel the payment (Return the payment to customer)
https://developer.paypal.com/docs/classic/admin/auth-capture/


MayPay Payment
In this case, One Merchant can send the money to his partner’s paypal email address (Paypal email address), mean each merchant must have paypal account.
https://developer.paypal.com/docs/classic/mass-pay/integration-guide/MassPayOverview/


Parallel Payment
In this case, We transfer the money between two or more partner instead of transferring all money in one merchant Account. This is possible with “Paypal Express Checkout” Method. In this method customer leave our website(like example.com) and redirect to paypal.com website, Here customer will see the all partner details and can pay by credit card/paypal Account. After paying the amount, customer will return back to our website(example.com).
https://developer.paypal.com/docs/classic/express-checkout/integration-guide/ECGettingStarted/#id084RM05055Z
https://developer.paypal.com/docs/classic/paypal-payments-pro/integration-guide/WPParallelPaymens/#id09ARE0F405Z




Following are Request and Response of paypal APIs (Please update User, Pwd and Signature, these are dummy)

Sr Operation Method Payment Action Request Sample Response Sample Introduction
1 DoDirectPayment DoDirectPayment Sale
Array
(
    [METHOD] => DoDirectPayment
    [USER] => testname_api1.no-spam.ws
    [PWD] => 55555526677
    [SIGNATURE] => Alsdfdsfafds.zYsROoDYkL2AigOq
    [VERSION] => 95
    [PAYMENTACTION] => Sale
    [IPADDRESS] => 127.0.0.1
    [CREDITCARDTYPE] => MasterCard
    [ACCT] => 5522340006063638
    [EXPDATE] => 092015
    [CVV2] => 123
    [FIRSTNAME] => Aman
    [LASTNAME] => Jaiswal
    [STREET] => Phase+7%2C+Mohali+
    [CITY] => Largo
    [STATE] => Punjab
    [COUNTRYCODE] => US
    [ZIP] => 160055
    [AMT] => 100
    [CURRENCYCODE] => USD
)
Array
(
    [TIMESTAMP] => 2014-07-29T06:24:35Z
    [CORRELATIONID] => 5866bc2f19ac7
    [ACK] => Success
    [VERSION] => 95
    [BUILD] => 11950065
    [AMT] => 100.00
    [CURRENCYCODE] => USD
    [AVSCODE] => X
    [CVV2MATCH] => M
    [TRANSACTIONID] => 4VT01737UB5183903
)
Get the Payment from Buyer to Merchant Account
2 MassPayment MassPay -NA-
Array
(
    [METHOD] => MassPay
    [USER] => testname_api1.no-spam.ws
    [PWD] => 55555526677
    [SIGNATURE] => Alsdfdsfafdsfs.zYsROoDYkL2AigOq
    [VERSION] => 95
    [RECEIVERTYPE] => EmailAddress
    [CURRENCYCODE] => USD
    [L_EMAIL0] => testnamel@no-spam.ws
    [L_AMT0] => 10.00
    [L_EMAIL1] => testname2@no-spam.ws
    [L_AMT1] => 10.00
)
Array
(
    [AUTHORIZATIONID] => 7FU87403AH3981018
    [TIMESTAMP] => 2014-07-29T05:39:09Z
    [CORRELATIONID] => c86dbba49cc4b
    [ACK] => Success
    [VERSION] => 95
    [BUILD] => 11950065
)
Distribute the Payment between 2 more partner
3 DoAuthorization DoDirectPayment authorization
Array
(
    [METHOD] => DoDirectPayment
    [USER] => testname_api1.no-spam.ws
    [PWD] => 55555526677
    [SIGNATURE] => Alsdfdsfafdsf.zYsROoDYkL2AigOq
    [VERSION] => 95
    [PAYMENTACTION] => authorization
    [IPADDRESS] => 127.0.0.1
    [CREDITCARDTYPE] => MasterCard
    [ACCT] => 5522340006063638
    [EXPDATE] => 102015
    [CVV2] => 123
    [FIRSTNAME] => Aman
    [LASTNAME] => Jaiswal
    [STREET] => Phase+8%2C+Mohali+
    [CITY] => Largo
    [STATE] => Punjab
    [COUNTRYCODE] => US
    [ZIP] => 160055
    [AMT] => 124.00
    [CURRENCYCODE] => USD
)
Array
(
    [TIMESTAMP] => 2014-07-29T06:26:30Z
    [CORRELATIONID] => 42ec9f542fc9f
    [ACK] => Success
    [VERSION] => 95
    [BUILD] => 11950065
    [AMT] => 124.00
    [CURRENCYCODE] => USD
    [AVSCODE] => X
    [CVV2MATCH] => M
    [TRANSACTIONID] => 2EY34762VW9685022
)
Authorize(Hold) the payment from Buyer
4 DoCapture DoCapture -NA-
Array
(
    [METHOD] => DoCapture
    [USER] => testname_api1.no-spam.ws
    [PWD] => 55555526677
    [SIGNATURE] => Alsdfdsfafdsf.zYsROoDYkL2AigOq
    [VERSION] => 95
    [AUTHORIZATIONID] => 2EY34762VW9685022
    [IPADDRESS] => 127.0.0.1
    [AMT] => 124
    [CURRENCYCODE] => USD
    [COMPLETETYPE] => Complete
)
Array
(
    [AUTHORIZATIONID] => 2EY34762VW9685022
    [TIMESTAMP] => 2014-07-29T06:28:02Z
    [CORRELATIONID] => 943ba369b948d
    [ACK] => Success
    [VERSION] => 95
    [BUILD] => 11950065
    [TRANSACTIONID] => 0U8601158C887733S
    [PARENTTRANSACTIONID] => 2EY34762VW9685022
    [RECEIPTID] => 3177-5474-1146-7724
    [TRANSACTIONTYPE] => webaccept
    [PAYMENTTYPE] => instant
    [ORDERTIME] => 2014-07-29T06:28:02Z
    [AMT] => 124.00
    [FEEAMT] => 3.90
    [TAXAMT] => 0.00
    [CURRENCYCODE] => USD
    [PAYMENTSTATUS] => Completed
    [PENDINGREASON] => None
    [REASONCODE] => None
    [PROTECTIONELIGIBILITY] => Ineligible
    
Capture money which we have Authorized(Hold)
5 DoVoid DoVoid -NA-
Array
(
    [METHOD] => DoVoid
    [USER] => testname_api1.no-spam.ws
    [PWD] => 55555526677
    [SIGNATURE] => Alsdfdsfafds.zYsROoDYkL2AigOq
    [VERSION] => 95
    [AUTHORIZATIONID] => 9VP549332Y830720W
    [IPADDRESS] => 127.0.0.1
    [AMT] => 124
    [CURRENCYCODE] => USD
    [COMPLETETYPE] => Complete
)
Array
(
    [AUTHORIZATIONID] => 9VP549332Y830720W
    [TIMESTAMP] => 2014-07-29T06:28:52Z
    [CORRELATIONID] => 8a5d49a877bad
    [ACK] => Success
    [VERSION] => 95
    [BUILD] => 11950065
)
Return the Authorized(Holded) Amount to buyer account





Include Paypal Library
class Global_Paypal {

    private $_api_version = '95';
    private $_api_username = 'test_api1.no-spam.ws';//this is dummpy plz update
    private $_api_password = '1406987677';//this is dummpy plz update
    private $_api_signature = 'ARDESpesJftWYRUVq94d7LvquBZIApzl9Jq3wuT.zYsROoDYkL2AigOq';//this is dummpy plz update
    private $_uri = 'https://api-3t.sandbox.paypal.com/nvp';

    
    /** DoDirect Payment - SALE* */
    function doDirectPayment(array $data) {
        $postData = array(
            'METHOD' => 'DoDirectPayment',
            'USER' => ($this->_api_username),
            'PWD' => ($this->_api_password),
            'SIGNATURE' => ($this->_api_signature),
            'VERSION' => ($this->_api_version),
            'PAYMENTACTION' => $data['paymentAction'],
            'IPADDRESS' => $data['ip'],
            'CREDITCARDTYPE' => $data['cctype'],
            'ACCT' => $data['ccno'],
            'EXPDATE' => $data['expdate'],
            'CVV2' => $data['cvv2'],
            'FIRSTNAME' => $data['fname'],
            'LASTNAME' => $data['lname'],
            'STREET' => $data['address'],
            'CITY' => $data['city'],
            'STATE' => $data['state'],
            'COUNTRYCODE' => $data['ccode'],
            'ZIP' => $data['zip'],
            'AMT' => $data['amt'],
            'CURRENCYCODE' => $data['currencycode'],
        );

        try {
            $client = new Zend_Http_Client($this->_uri);
            $postData = array_map('urlencode', $postData);
            $client->setParameterPost($postData);
            $response = $client->request('POST');
            $body = $response->getRawBody();
            parse_str($body, $nvpResponseArray);
            return $nvpResponseArray;
        } catch (Exception $e) {
            echo $e->getMessage();
            die;
        }
    }

    /** Transfer Money to number of peoples - MassPay **/
    function masspayment(array $receiverArray) {
        $postData = array(
            'METHOD' => 'MassPay',
            'USER' => ($this->_api_username),
            'PWD' => ($this->_api_password),
            'SIGNATURE' => ($this->_api_signature),
            'VERSION' => ($this->_api_version),
            'RECEIVERTYPE' => 'EmailAddress',
            'CURRENCYCODE' => 'USD',
        );
        $i = 0;
        foreach ($receiverArray as $emailAddress => $amt) {
            $postData["L_EMAIL{$i}"] = $emailAddress;
            $postData["L_AMT{$i}"] = $amt;
            $i++;
        }
        try {
            $client = new Zend_Http_Client($this->_uri);
            //$postData = array_map('urlencode', $postData);            
            pr($postData);
            $client->setParameterPost($postData);
            $response = $client->request('POST');
            $body = $response->getRawBody();
            parse_str($body, $nvpResponseArray);
            return $nvpResponseArray;
        } catch (Exception $e) {
            echo $e->getMessage();
            die;
        }
    }

    /** DoDirect Payment - Authorization* */
    function doauthPayment(array $data) {

        $postData = array(
            'METHOD' => 'DoDirectPayment',
            'USER' => ($this->_api_username),
            'PWD' => ($this->_api_password),
            'SIGNATURE' => ($this->_api_signature),
            'VERSION' => ($this->_api_version),
            'PAYMENTACTION' => $data['paymentAction'],
            'IPADDRESS' => $data['ip'],
            'CREDITCARDTYPE' => $data['cctype'],
            'ACCT' => $data['ccno'],
            'EXPDATE' => $data['expdate'],
            'CVV2' => $data['cvv2'],
            'FIRSTNAME' => $data['fname'],
            'LASTNAME' => $data['lname'],
            'STREET' => $data['address'],
            'CITY' => $data['city'],
            'STATE' => $data['state'],
            'COUNTRYCODE' => $data['ccode'],
            'ZIP' => $data['zip'],
            'AMT' => $data['amt'],
            'CURRENCYCODE' => $data['currencycode'],
        );

        try {
            /** Get the Transaction Id * */
            $client = new Zend_Http_Client($this->_uri);
            $postData = array_map('urlencode', $postData);
            $client->setParameterPost($postData);
            $response = $client->request('POST');
            $body = $response->getRawBody();
            parse_str($body, $nvpResponseArray);
            /** Get the Transaction Id * */
            if (!empty($nvpResponseArray['TRANSACTIONID'])) {
                return $nvpResponseArray;
            } else {
                pr($nvpResponseArray);
                die("TransactionId is empty");
            }

            return $nvpResponseArray;
        } catch (Exception $e) {
            echo $e->getMessage();
            die;
        }
    }

    /** DoCapture* */
    function doCapturePayment($data = array()) {
        $postData = array(
            'METHOD' => 'DoCapture',
            'USER' => ($this->_api_username),
            'PWD' => ($this->_api_password),
            'SIGNATURE' => ($this->_api_signature),
            'VERSION' => ($this->_api_version),
            'AUTHORIZATIONID' => $data['TransactionID'],
            'IPADDRESS' => $data['ip'],
            'AMT' => $data['amt'],
            'CURRENCYCODE' => $data['currencycode'],
            'COMPLETETYPE' => 'Complete'
        );
        try {
            pr($postData);
            /** Get the Transaction Id * */
            $client = new Zend_Http_Client($this->_uri);
            $postData = array_map('urlencode', $postData);
            $client->setParameterPost($postData);
            $response = $client->request('POST');
            $body = $response->getRawBody();
            parse_str($body, $nvpResponseArray);
            pr($nvpResponseArray);
            die;
            /** Get the Transaction Id * */
            if (!empty($nvpResponseArray['TRANSACTIONID'])) {
                return $nvpResponseArray;
            } else {
                pr($nvpResponseArray);
                die("TransactionId is empty");
            }

            return $nvpResponseArray;
        } catch (Exception $e) {
            echo $e->getMessage();
            die;
        }
    }

}



Following are different functions which you can used to achieve above actions


  1. doDirectPayment: DoDirect Payment - Sale
  2. masspayment: Mass Pay
  3. doauthPayment: Authorization - PayPal
  4. doCapturePayment: Capture - PayPal

class PaypalController extends Zend_Controller_Action {


    /** DoDirect Payment - SALE* */
    public function indexAction() {

        /** Sample Data* */
        /** set Data  * */
        $amount = 100.00;
        $creditCardType = 'MasterCard';
        $creditCardNumber = '5522340006063638';
        $expirationMonth = '09';
        $expirationYear = '2015';
        $cvv2 = '123';
        $firstName = 'Aman';
        $lastName = 'Jaiswal';
        $address1 = 'Phase 7, Mohali';
        $address2 = '';
        $city = 'Largo';
        $state = 'Punjab';
        $zip = '160055';
        $postData = array();
        $countryCode = 'US';
        $currencyCode = 'USD';
        $ipAddress = $_SERVER['REMOTE_ADDR'];
        /** set Data * */
        $postData = array(
            'paymentAction' => 'Sale',
            'ip' => $_SERVER['REMOTE_ADDR'],
            'cctype' => $creditCardType,
            'ccno' => $creditCardNumber,
            'expdate' => "{$expirationMonth}{$expirationYear}",
            'cvv2' => urlencode($cvv2),
            'fname' => $firstName,
            'lname' => $lastName,
            'address' => $address1 . ' ' . $address2,
            'city' => $city,
            'state' => $state,
            'ccode' => $countryCode,
            'zip' => $zip,
            'amt' => $amount,
            'currencycode' => $currencyCode,
            'DESC' => 'Testing Payments Pro'
        );

        $paypal = new Global_PayPal();
        $result = $paypal->doDirectPayment($postData);        
        print_r($result);
        die;        
        /** Sample Data* */        
        
    }

    /** Transfer Money to number of peoples - MassPay **/
    function masspayAction() {
        $paypal = new Global_PayPal();
        $receiverArray = array(
            'ajit@no-spam.ws' => '10.00',
            'ajit@no-spam.ws' => '10.00',
        );        

        $result = $paypal->masspayment($receiverArray);
        print_r($result);die;
        
    }
    
      /** DoDirect Payment - Authorization* */
    public function authAction() {
        
        /** set Data  * */
        $amount = '124.00';
        $creditCardType = 'MasterCard';
        $creditCardNumber = '5522340006063638';
        $expirationMonth = '10';
        $expirationYear = '2015';
        $cvv2 = '123';
        $firstName = 'Aman';
        $lastName = 'Jaiswal';
        $address1 = 'Phase 8, Mohali';
        $address2 = '';
        $city = 'Largo';
        $state = 'Punjab';
        $zip = '160055';
        $postData = array();
        $countryCode = 'US';
        $currencyCode = 'USD';
        $ipAddress = $_SERVER['REMOTE_ADDR'];
        $transActionId = '8f4de96650a85';
        /** set Data * */
        $postData = array(            
            'paymentAction' => 'authorization',
            'ip' => $_SERVER['REMOTE_ADDR'],
            'cctype' => $creditCardType,
            'ccno' => $creditCardNumber,
            'expdate' => "{$expirationMonth}{$expirationYear}",
            'cvv2' => urlencode($cvv2),
            'fname' => $firstName,
            'lname' => $lastName,
            'address' => $address1 . ' ' . $address2,
            'city' => $city,
            'state' => $state,
            'ccode' => $countryCode,
            'zip' => $zip,
            'amt' => $amount,
            'currencycode' => $currencyCode,            
        );

        $paypal = new Global_PayPal();
        $result = $paypal->doauthPayment($postData);        
        print_r($result);
        die;
        /** Sample Data* */        
        // action body
    }   
    
    /** DoCapture* */
    function captureAction(){
        $postData = array(            
            'paymentAction' => 'DoCapture',
            'ip' => $_SERVER['REMOTE_ADDR'],           
            'amt' => 124,
            'currencycode' => 'USD',            
            'TransactionID' => '49760658JE805135N',
        );

        $paypal = new Global_PayPal();
        $result = $paypal->doCapturePayment($postData);        
        print_r($result);
        die;
        
    }

}