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.



Tuesday, 19 May 2015

Facebook Login Integration - Multiple Facebook IDs for the same user[Solved]

Login Integration - Multiple Facebook IDs for the same user[Solved]

I have facebook login integration in my website - Its working fine.

When Facebook user first time comes in website, I register the facebook user with my website and login him successfully.
When same Facebook user comes back and login to website, then i login him in my website because he is returning user and his entity is already in database.


How I was checking Returning OR New user.
1. When user register with website, facebook return following details.
Array
(
    [id] => 1421305244855173
    [first_name] => Arun
    [gender] => male
    [last_name] => Kumar
    [link] => https://www.facebook.com/app_scoped_user_id/1421305244855173/
    [locale] => en_US
    [name] => Arun Kumar
    [timezone] => 5.5
    [updated_time] => 2015-04-23T09:57:26+0000
    [verified] => false    
)

2. I add facebook Id(here Id is 1421305244855173), in my database along with other user details.
3. Login him successfully.
4. When same user come back in website, I check the "facebook Id" in database.
5. If "facebook Id" does not exist in database, create account and login him successfully [New facebook user].
6. If "facebook Id" exist in database, login him successfully [Returning facebook user].


I have login login integration from below links:
http://www.web-technology-experts-notes.in/2014/09/facebook-login-with-javascript-sdk-example.html

Now Problem:
If same user login from web application and android application.
It add two different records in database which is not expecting.
Expection is "It must have single records" in database.

My finding are below:
1) Facebook gives different "facebook id" when same user login from web application and login from "Android Application".
2) Facebook gives different "facebook Id" when user using different version of facebook APIs (V1 and V2).


Why this is happing:
When Facebook giving two different Ids for same facebook user, It means
One "facebook id" is "Global Ids"
Second one is "App Scoped ID"


For More details check: https://developers.facebook.com/docs/apps/changelog#v2_0_graph_api


Solution
You need "Mapping the same user across multiple apps" by doing this issue will be fixed.

For this, you need need to create "Business Manager Account" in  facebook Apps.

How to create Business Manager Account:
https://developers.facebook.com/docs/apps/business-manager#create-business


After doing setup, you will be able to get the "token_for_business", which will be same for one user across all apps.
How to get token_for_business in javascript.
FB.api('/me?fields=token_for_business', function(response) {
    console.log(response);
});

For More Detailshttps://developers.facebook.com/docs/apps/for-business