№22 Dars - Yii da paypalni integratsiya qilish.

PayPal o`zi nima?

Yiida PayPal ning maxsus extensionlari mavjud. Bulardan hozircha bittasi bilan tanishib qanday ishlashi haqida gaplashamiz. PayPal o`zi o`zaro pul aylanmasini taminlab beruvchi veb ilova. Bu yerdan pul o`tkazmalar qilayotganda ro`yxatdan o`tgan bo`lishingiz kerak. Ya`ni akkountingiz bo`lishi kerak. Bu ilovada ham siz ishlaringizni test qilib ko`rishingiz uchun sandbox turidan foydalanishingiz kerak.

Paypal o`zining API siga ega va biz undan Yii da foydalanishimiz mumkin.

Kerakli ishlar:

  1. Komponentani ishlatish uchun Yii framework 1.1 va undan yuqori versiyasi kerak bo`ladi
  2. PHP CURL extensioni o`rnatilgan va yoqilgan bo`lishi kerak
  3. PayPal ma`lumotlari (username, password, signature) lar bo`lishi kerak
  4. Exstensioni quyidagi havola orqali yuklab olishingiz kerak bo`ladi http://www.yiiframework.com/extension/paypal/

Bog`lash:

Ushbu murojaat orqali extensionni yuklab olishingiz kerak. Keyin main.php ga quyidagicha kod kiritishingiz kerak bo`ladi.

'Paypal' => array(
    'class'=>'application.components.Paypal',
    'apiUsername' => 'YOUR_API_USERNAME',  // paypaldagi usernameingiz
    'apiPassword' => 'YOUR_API_PASSWORD',  // paypaldagi passwordingiz
    'apiSignature' => 'YOUR_API_SIGNATURE',  // yagona tarzdagi kalit 
    'apiLive' => false,  // false bo`lsa sandbox orqali ishlatish, ya`ni test qilish uchun ishlatiladi. Agar haqiqiy pul aylanmasini o`rnatmoqchi bo`lsangiz true qilib qo`yish talab qilinadi. 
 
    'returnUrl' => 'paypal/confirm/', //PayPal serveridan ish muvofaqiyatli kelsa shu urlga yo`naltiradi.
    'cancelUrl' => 'paypal/cancel/', //agar ish muvofaqiyatsiz bo`lsa ushbu urlga yo`naltiradi.
 
    // qaysi birlikda pul aylanmasini amalga oshirishingiz
    'currency' => 'USD',
 
    // izohlar uchun 
    //'defaultDescription' => '',
 
    // mahsulot soni 
    //'defaultQuantity' => '1',
),

PayPal ni komponentidan foydalanish kontrollerga quyidagicha kod yozishimiz orqali amalga oshadi.

class PaypalController extends Controller
{
    public function actionBuy(){
  
        $paymentInfo['Order']['theTotal'] = 10.00;
        $paymentInfo['Order']['description'] = "Some payment description here";
        $paymentInfo['Order']['quantity'] = '1';
 
        // paypalni chaqirish 
        $result = Yii::app()->Paypal->SetExpressCheckout($paymentInfo); 
        //jarayon vaqtida xatolik chiqsa 
        if(!Yii::app()->Paypal->isCallSucceeded($result)){ 
            if(Yii::app()->Paypal->apiLive === true){
                //Live bo`lsa. Agar qandaydir xatolik bo`lsa shu xatolikni oladi.
                $error = 'We were unable to process your request. Please try again later';
            }else{
                //Sandbox bo`lsa. Agar xatolik bo`lsa shu haqdagi xatolik xabari keladi.
                $error = $result['L_LONGMESSAGE0'];
            }
            echo $error;
            Yii::app()->end();
 
        }else { 
            // paypalga userni jo`natish 
            $token = urldecode($result["TOKEN"]); 
 
            $payPalURL = Yii::app()->Paypal->paypalUrl.$token; 
            $this->redirect($payPalURL); 
        }
    }
 
    public function actionConfirm()
    {
        $token = trim($_GET['token']);
        $payerId = trim($_GET['PayerID']);
        $result = Yii::app()->Paypal->GetExpressCheckoutDetails($token);
 
        $result['PAYERID'] = $payerId; 
        $result['TOKEN'] = $token; 
        $result['ORDERTOTAL'] = 10.00;
 
        //jarayon vaqtida xatolik chiqsa 
        if(!Yii::app()->Paypal->isCallSucceeded($result)){ 
           if(Yii::app()->Paypal->apiLive === true){
             $error = 'We were unable to process your request. Please try again later';
            }else{
             $error = $result['L_LONGMESSAGE0'];
            }
            echo $error;
            Yii::app()->end();
        }else{ 
 
            $paymentResult = Yii::app()->Paypal->DoExpressCheckoutPayment($result);
             
            if(!Yii::app()->Paypal->isCallSucceeded($paymentResult)){
                if(Yii::app()->Paypal->apiLive === true){
                    
                    $error = 'We were unable to process your request. Please try again later';
                }else{
                    
                    $error = $paymentResult['L_LONGMESSAGE0'];
                }
                echo $error;
                Yii::app()->end();
            }else{
                //to`lov muvofaqiyatli amalga oshdi
 
                $this->render('confirm');
            }
 
        }
    }
 
    public function actionCancel()
    {
        $token = $_GET['token'];
        $this->render('cancel');
    }
 
    public function actionDirectPayment(){ 
        $paymentInfo = array('Member'=> 
            array( 
                'first_name'=>'name_here', 
                'last_name'=>'lastName_here', 
                'billing_address'=>'address_here', 
                'billing_address2'=>'address2_here', 
                'billing_country'=>'country_here', 
                'billing_city'=>'city_here', 
                'billing_state'=>'state_here', 
                'billing_zip'=>'zip_here' 
            ), 
            'CreditCard'=> 
            array( 
                'card_number'=>'number_here', 
                'expiration_month'=>'month_here', 
                'expiration_year'=>'year_here', 
                'cv_code'=>'code_here' 
            ), 
            'Order'=> 
            array('theTotal'=>1.00) 
        ); 
 
       /* 
        * Success bo`lsa, $result quyidagilarni o`z ichiga oladi [AMT] [CURRENCYCODE]   [AVSCODE] [CVV2MATCH]  
        * [TRANSACTIONID] [TIMESTAMP] [CORRELATIONID] [ACK] [VERSION] [BUILD] 
        *  
        * Xatolik bo`lsa, $ result quyidagilarni o`z ichiga oladi [AMT] [CURRENCYCODE] [TIMESTAMP] [CORRELATIONID]  
        * [ACK] [VERSION] [BUILD] [L_ERRORCODE0] [L_SHORTMESSAGE0] [L_LONGMESSAGE0]  
        * [L_SEVERITYCODE0]  
        */ 
 
        $result = Yii::app()->Paypal->DoDirectPayment($paymentInfo); 
 
        //jarayon vaqtida xatolik chiqsa 
        if(!Yii::app()->Paypal->isCallSucceeded($result)){ 
            if(Yii::app()->Paypal->apiLive === true){
                //Live bo`lsa. Agar qandaydir xatolik bo`lsa xatolini oladi.
                $error = 'We were unable to process your request. Please try again later';
            }else{
                //Sandbox bo`lsa. Agar xatolik bo`lsa xatolik habari keladi.
                $error = $result['L_LONGMESSAGE0'];
            }
            echo $error;
 
        }else { 
            // To`lov muvofaqiyatli yakunlandi
        }
 
        Yii::app()->end();
    } 
}

Siz platformangizda "Buy" actioniga murojaat qilasiz. Undan siz yuklab olgan komponentingizga boradi. U yerdan PayPal serveriga userni yo`naltiradi. U yerdan muvofaqiyatli qaytsa "confirm" actioniga keladi va jarayon oxirgi bosqichigacha xatoliksiz amalga oshib kelsa o`zingiz uchun ham ushbu pul o`tkazmalarini saqlash uchun kodlar yozishingiz mumkin bo`ladi.

Ko`rib turganingizdek bu foydalanuvchining bosqichlar jarayoni. Sizning serveringizdan paypal serveriga yo`naltiriladi va u yerda foydalanuvchi accounti orqali kirib to`lovni amalga oshiradi. Har bir to`lovning yagona idsi mavjud.

PayPal ni platformangizga biriktirganingizdan keyin har xil o`zgarishlar qilib ko`rib xar bir parametrni nima ish qilishini yana bir bor bilib olishingiz mumkin bo`ladi.

Manba:


JONNY

Muallif haqida

JONNY Arduino, Java, C#, Android, Windows, Linux, Debian, Javascript.


Blogdagi so‘nggi maqolalar:


Birinchi bo‘ling!

Iltimos, fikr bildirish uchun saytga kiring yoki ro‘yxatdan o‘ting!