Thursday, 21 May 2015

Paypal Reference Transaction with do direct payments in Zend Framework

In Paypal reference transaction,
We do first payment with customer credit card details.
and then stored the transaction id (Its also reference id for next payment)
For Next payment we use transaction id to charge the customer for next payment without asking Credit card details.


paypal reference transaction with do direct payments in Zend Framework


Reference Transaction have following two parts.
1. Do Direct payment
In Do Direct payment, We ask the customer to fill credit card details and customer details.

Based on this details we process the request and charge the payment from customer account.
Following are request parameter required for Do Direct payment.
Endpoint URL: https://api-3t.sandbox.paypal.com/nvp
HTTP method: POST
POST data:
USER=insert_merchant_user_name_here
&PWD=insert_merchant_password_here
&SIGNATURE=insert_merchant_signature_value_here
&METHOD=DoDirectPayment
&VERSION=86
&PAYMENTACTION=Sale     
&AMT=10    
&ACCT=4641631486853053    #The credit card number
&CREDITCARDTYPE=VISA    #The type of credit card i.e visa/mastercard/disover
&CVV2=123    #The CVV2 number
&FIRSTNAME=James
&LASTNAME=Smith
&STREET=FirstStreet
&CITY=SanJose
&STATE=CA
&ZIP=95131
&COUNTRYCODE=US
&CURRENCYCODE=USD    #The currency
&EXPDATE=052018    #expiry date of the credit card i.e mm-yyyy

When we execute the above details we get following output:
ACK=Success
&AMT=10%2e00
&CURRENCYCODE=USD
&AVSCODE=X
&CVV2MATCH=M
&TRANSACTIONID=9KK85084958471234    #An ID on which to base a DoReferenceTransaction call

Now TRANSACTIONID should be stored safely in database.
This TRANSACTIONID will be used as reference Id from next payments.

We need not to stored the credit card details at our end.

Zend Framework Code for DoDirect Payment
        $postData = array(
            'METHOD' => 'DoDirectPayment',
            'USER' => 'API_USERNAME',
            'PWD' => 'API_PASSWORD',
            'SIGNATURE' => 'API_SIGNATURE',
            'VERSION' => 'API_VERSION',
            'PAYMENTACTION' => 'Sale',
            'IPADDRESS' => '127.0.0.1',
            'CREDITCARDTYPE' => 'Visa',
            'ACCT' => '4032037747991558',
            'EXPDATE' => '032020',
            'CVV2' => '123',
            'FIRSTNAME' => 'Web technology',
            'LASTNAME' => 'Experts Team',
            'STREET' => 'Your street',
            'CITY' => 'Your city',
            'STATE' => 'state',
            'COUNTRYCODE' => 'US',
            'ZIP' => '160055',
            'AMT' => '10',
            'CURRENCYCODE' => 'USD',
        );
        
        $postData = array_filter($postData);                
        try {
            $client = new Zend_Http_Client('https://api-3t.sandbox.paypal.com/nvp');
            $postData = array_map('urlencode', $postData);
            pr($postData);
            $client->setParameterPost($postData);
            $response = $client->request('POST');
            $body = $response->getRawBody();
            parse_str($body, $nvpResponseArray);
            print_r($nvpResponseArray);
        } catch (Exception $e) {
            echo $e->getMessage();
            die;
        }



2. Reference Transaction Payment.
We do reference payment on the behalf of reference Id (transaction id in above payment).
We require following details.

Endpoint URL: https://api-3t.sandbox.paypal.com/nvp
HTTP method: POST
POST data:
USER=insert_merchant_user_name_here
&PWD=insert_merchant_password_here
&SIGNATURE=insert_merchant_signature_value_here
&METHOD=DoReferenceTransaction
&VERSION=86
&AMT=2    #The amount of payment
&CURRENCYCODE=USD    #The currency, e.g. US dollars
&PAYMENTACTION=SALE     #Indicates that a payment will be processed
&REFERENCEID=9KK85084958471234     #transaction ID from a DoDirectPayment response

When we execute the above details we get following output:
AVSCODE=X
&CVV2MATCH=M
&ACK=Success
&TRANSACTIONID=7TX32596U93391234     #Transaction ID 
&AMT=2%2e00
&CURRENCYCODE=USD


Zend Framework Code for DoDirect Payment
        $postData = array(
            'METHOD' => 'DoReferenceTransaction',
            'USER' => 'API_USERNAME',
            'PWD' => 'API_PASSWORD',
            'SIGNATURE' => 'API_SIGNATURE',
            'VERSION' => 'API_VERSION',
            'PAYMENTACTION' => 'Sale',
            'IPADDRESS' => '127.0.0.1',
            'REFERENCEID' => '75G187189W046790W',            
            'AMT' => '10',
            'CURRENCYCODE' => 'USD',
        );
        
        $postData = array_filter($postData);                
        try {
            $client = new Zend_Http_Client('https://api-3t.sandbox.paypal.com/nvp');
            $postData = array_map('urlencode', $postData);
            pr($postData);
            $client->setParameterPost($postData);
            $response = $client->request('POST');
            $body = $response->getRawBody();
            parse_str($body, $nvpResponseArray);
            print_r($nvpResponseArray);
        } catch (Exception $e) {
            echo $e->getMessage();
            die;
        }

Please note following regarding Reference Transaction.
1) Business Pro Account Required for Reference Transaction.
2) No need to save the credit card details for next payment.