Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[PHP] How to integrated Paypal with PHP

Thursday 16 July 2020
|
Read: Completed in minutes

[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

🙇🏼 Your Feedback Is Valuable and Helpful to Us - Webzone - all things Tech Tips web development Zidane 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com