728x90 AdSpace

  • Latest News

    Thursday 16 July 2020

    [PHP] How to integrated Paypal with PHP

    Welcome back to Learn Tech Tips blogspot. How are you today? Hope you well ~
    Today topic I will show you how to integrated paypal with PHP

    PayPal Holdings, Inc. is an American company operating a worldwide online payments system that supports online money transfers and serves as an electronic alternative to traditional paper methods like checks and money orders

    These are paypal url helpful link: https://developer.paypal.com/developer/applications/

    First, you have to recreate a App information:
    Client Id and Client Credentials



    https://api.sandbox.paypal.com/v1/oauth2/token




    If you running above api got below message, that mean you run api had succeed

    How to pass the params:
    Let take a look on postman demo
    Authorization: username (client id, client credentials)


    Headers.



    Body: grant_type:



    // php code here
    // Author: Zidane (huuvi168@gmail.com)
    // "intent": "order" 用途,如果商戶有過渡期,一般會onhold 客人信用卡信用額,到佐貨品發貨時扣除;或客人在過渡期期間申請退款,直接release 信用額,避免手續費
    // 如果"intent": "sale", 直接扣款,無佐中間onhold
    public function create_paypal_payment($invoice_number, $price, $description, $access_token, $currency = 'HKD', $role) {

    $curl = curl_init();
    curl_setopt_array($curl, array(
    CURLOPT_URL => Environment::read('paypal.url_create_payment'),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    ));


    $intent = "sale";
    $return_url = Environment::read('paypal.url_return_teacher');
    $cancel_url = Environment::read('paypal.url_cancel');

    if ($role == Environment::read('member.student')) {
    $return_url = Environment::read('paypal.url_return_student');
    $cancel_url = Environment::read('paypal.url_cancel');
    $intent = 'authorize'; // need approve by teacher

    }

    $params = array(

    "intent" => $intent,
    'payer' => array(
    'payment_method' => 'paypal',
    ),
    'redirect_urls' => array(
    "return_url" => $this->get_current_host_server_name() . $return_url, // "http://localhost/masterx/api/payment/payments/get_info_from_return_url_paypal.json", // get payer_id from this link
    "cancel_url" => $this->get_current_host_server_name() . $cancel_url, // "https://youtu.be/FigKDd_yhFo"
    ),
    'transactions' => array(
    array(
    'invoice_number' => $invoice_number, // must be different, because same invoice which paid, so the paypal will block it (Duplicate invoice Id detected.).
    'amount' => array(
    'total' => $price,
    'currency' => $currency,
    ),
    'description' => $description,
    )
    ),
    );

    //save transaction for later call

    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);

    if (!empty($access_token) && !is_null($access_token)) {
    curl_setopt($curl, CURLOPT_HTTPHEADER,
    array( // use this so we MUST post to header (postman)
    "authorization: Bearer " . $access_token,
    "cache-control: no-cache",
    "content-type: application/json"
    )
    );
    } else {
    curl_setopt($curl, CURLOPT_HTTPHEADER,
    array( // use this so we MUST post to header (postman)
    "accept: application/json",
    "accept-language: en_US",
    "authorization: Basic ". base64_encode($this->client_id . ":" . $this->secret),
    "cache-control: no-cache",
    "content-type: application/x-www-form-urlencoded"
    )
    );
    }

    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);
    $result = array();

    if ($err) {
    $result = array(
    'err' => $err,
    );

    } else {
    $result = json_decode($response, true);
    }
    return $result;
    }






    Hope you solved your problem,
    Any feedback or question, leave your comment, we can discuss about it~
    Zidane
    • Blogger Comments
    • Facebook Comments

    0 comments:

    Post a Comment

    Item Reviewed: [PHP] How to integrated Paypal with PHP Rating: 5 Reviewed By: Unknown
    Scroll to Top