Advertisement
kul1010

curl in wordpress

Jul 7th, 2016
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.83 KB | None | 0 0
  1.  
  2. Conversation opened. 1 unread message.
  3.  
  4. Skip to content
  5. Using Gmail with screen readers
  6. Click here to enable desktop notifications for Gmail. Learn more Hide
  7.  
  8.  
  9. More
  10. 1 of 22,559
  11.  
  12. (no subject)
  13. Inbox
  14. x
  15. manish rajput
  16.  
  17. Attachments1:11 PM (4 minutes ago)
  18.  
  19. to me
  20. Attachments area
  21.  
  22. Click here to Reply or Forward
  23. 5.85 GB (39%) of 15 GB used
  24. Manage
  25. Terms - Privacy
  26. Last account activity: 2 hours ago
  27. Details
  28.  
  29.  
  30. manish rajput's profile photo
  31. manish rajput
  32. Show details
  33.  
  34. <?php
  35.  
  36. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  37.  
  38. /**
  39. * Cash on Delivery Gateway
  40. *
  41. * Provides a Cash on Delivery Payment Gateway.
  42. *
  43. * @class WC_Gateway_COD
  44. * @extends WC_Payment_Gateway
  45. * @version 2.0.0
  46. * @package WooCommerce/Classes/Payment
  47. * @author Patrick Garman
  48. */
  49. class WC_Gateway_COD extends WC_Payment_Gateway {
  50.  
  51. /**
  52. * Constructor for the gateway.
  53. *
  54. * @access public
  55. * @return void
  56. */
  57. function __construct() {
  58. $this->id = 'cod';
  59. $this->icon = apply_filters( 'woocommerce_cod_icon', '' );
  60. $this->method_title = __( 'Cash on Delivery', 'woocommerce' );
  61. $this->has_fields = false;
  62.  
  63. // Load the settings
  64. $this->init_form_fields();
  65. $this->init_settings();
  66.  
  67. // Get settings
  68. $this->title = $this->get_option( 'title' );
  69. $this->description = $this->get_option( 'description' );
  70. $this->instructions = $this->get_option( 'instructions' );
  71. $this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
  72.  
  73. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
  74. add_action( 'woocommerce_thankyou_cod', array( $this, 'thankyou' ) );
  75. }
  76.  
  77.  
  78.  
  79. /*******api Function************/
  80.  
  81.  
  82. function verify_student($data,$access_token) {
  83. //session_start();
  84. $url="http://stg.thegateacademy.info:8080/api/subjectpack/GetStudentDetails/?param=".$data;
  85.  
  86. $access_token=trim($access_token);
  87.  
  88. if($access_token==""){
  89. $authorization="Authorization: bearer 8Fyqo27_8n7HE8rq5tPpdlwy-laYwCMCuL21pBLKze-pTqPxUiJgVGbW8vraaD706D0ZHraCBQjEgvTDt0rRJOrltDVo4RcUI1muOO60icno3K9xw8uNZfyr_uM9-wvn_2D88MR2Z0wVCsV9ubAXI7SxEaOPR20M_-Six7-KGVpq83ViwRUWHzuOfNd4fMdxgzUWH043Llgg4XmQV-F8TklGAIpS6J_JnyM7hRCfz_buvpDI-REQC2qKaLNbPDl85nXGoLKxnK3F-G7N3VV5wcmJQyBS1F7gQkm8EcXdMf-WImEdTw7Q9XaXjnhqFSWlba6oWR1isPdxTOu0CoJOhYuTnIxQgtIuXlat5MqTTZzlh91tPYZqTDDK_hdWiyL6BqMEA9tPq9fPkDxSmwt8EC_bUngZua6iOSlLBSwv2B2BpCGDp-hD443tAOEZBWIJKeVP6Y9sdpREACl_TFnxB3UmsdFgyVa6L21jSnQhXkVkBer1kESZB2nFjPwvq4F86HRE2AHRfu4dkLMRlwXwwHtHzrec2yR3v14ftYqaQW3Pv7LGUlnwmHzNuyu5Hd0aBa5UzILWBJmHQaX9FntejQ";
  90. }
  91. else {$authorization="Authorization: bearer ".$access_token;}
  92. $ch = curl_init();
  93. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml' , $authorization ));
  94. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  95. curl_setopt($ch, CURLOPT_URL, $url);
  96. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  97. // curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
  98. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  99. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
  100. //$result = curl_exec($ch);
  101. $output = curl_exec($ch);
  102.  
  103. //return $output;
  104.  
  105.  
  106. if ($output === FALSE) {
  107.  
  108. echo "cURL Error: " . curl_error($ch);
  109. exit;
  110.  
  111. }
  112.  
  113. curl_close($ch);
  114.  
  115. $obj =json_decode($output);
  116. $verification =$obj->{'StudentExists'};
  117. $phone= $obj->{'PhoneNumber'};
  118. $roll_no= $obj->{'RollNumber'};
  119. $st_name= $obj->{'StudentName'};
  120. $st_mail=$obj->{'Email'};
  121.  
  122. $_SESSION['st_phone']=$phone;
  123.  
  124. $_SESSION['st_rollno']=$roll_no;
  125. $_SESSION['st_mail']=$st_mail;
  126. $_SESSION['st_name']=$st_name;
  127. $str = explode(" ",$_SESSION['st_name']);
  128. $_SESSION['f_name']=$str[0];
  129. $_SESSION['l_name']=$str[1];
  130.  
  131. return $verification;
  132.  
  133. }
  134.  
  135. function add_student_checkout_detail($fields_string) {
  136. session_start();
  137. global $wpdb;
  138. $access_token=$this->get_access_token();
  139. $url="http://stg.thegateacademy.info:8080/api/subjectpack/AddStudentCourse";
  140. $authorization="Authorization: bearer ".$access_token;
  141. $ch = curl_init();
  142. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded' , $authorization ));
  143.  
  144. curl_setopt($ch, CURLOPT_URL, $url);
  145. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  146.  
  147.  
  148. curl_setopt($ch, CURLOPT_POST, true); // tell curl you want to post something
  149. curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); // define what you want to post
  150.  
  151.  
  152. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  153. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
  154. //$result = curl_exec($ch);
  155. $output = curl_exec($ch);
  156.  
  157.  
  158. $wpdb->insert( 'tga_request_api',
  159. array(
  160. 'string_passed' => $fields_string,
  161. 'output' => $output
  162. )
  163. );
  164.  
  165.  
  166. return $output;
  167.  
  168.  
  169. }
  170.  
  171.  
  172. function get_access_token()
  173. {
  174. $access_token="";
  175. date_default_timezone_set("Asia/Calcutta");
  176. $current_timestamp=time();
  177.  
  178. if(get_option( "expirein")){
  179.  
  180. if(get_option( "expirein")<=$current_timestamp){
  181. $url="http://stg.thegateacademy.info:8080/token";
  182.  
  183. $ch = curl_init();
  184. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
  185. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  186. curl_setopt($ch, CURLOPT_URL, $url);
  187. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  188. curl_setopt($ch, CURLOPT_POSTFIELDS,"grant_type=password&username=subjectpack&password=Abcd123$");
  189. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  190. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
  191.  
  192. $output = curl_exec($ch);
  193.  
  194. if ($output === FALSE) {
  195.  
  196. echo "cURL Error: " . curl_error($ch);
  197. exit;
  198.  
  199. }
  200.  
  201. curl_close($ch);
  202.  
  203. $obj =json_decode($output);
  204.  
  205. // print_r($obj);
  206. $access_token= $obj->{'access_token'};
  207.  
  208. $token_type= $obj->{'token_type'};
  209.  
  210. $expirein= $obj->{'expirein'};
  211.  
  212. update_option( "access_token", $access_token);
  213. update_option( "token_type", $token_type);
  214. update_option( "expirein", $expirein);
  215. }
  216.  
  217. else {
  218.  
  219. $access_token=get_option( "access_token");
  220.  
  221. }
  222. }
  223.  
  224. else
  225. {
  226. $url="http://stg.thegateacademy.info:8080/token";
  227.  
  228. $ch = curl_init();
  229. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
  230. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  231. curl_setopt($ch, CURLOPT_URL, $url);
  232. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  233. curl_setopt($ch, CURLOPT_POSTFIELDS,"grant_type=password&username=subjectpack&password=Abcd123$");
  234. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  235. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
  236.  
  237. $output = curl_exec($ch);
  238.  
  239. if ($output === FALSE) {
  240.  
  241. echo "cURL Error: " . curl_error($ch);
  242. exit;
  243.  
  244. }
  245.  
  246. curl_close($ch);
  247.  
  248. $obj =json_decode($output);
  249.  
  250. // print_r($obj);
  251. $access_token= $obj->{'access_token'};
  252.  
  253. $token_type= $obj->{'token_type'};
  254.  
  255. $expires_in= strtotime($obj->{'.expires'});
  256.  
  257. update_option( "access_token", $access_token);
  258. update_option( "token_type", $token_type);
  259. update_option( "expires_in", $expires_in);
  260.  
  261. }
  262. return $access_token;
  263.  
  264. }
  265.  
  266.  
  267. function get_stream_code($stream)
  268.  
  269. {
  270. switch($stream){
  271.  
  272. case "Civil Engineering": $stream_code="CE";break;
  273. case "Computer Science Engineering": $stream_code="CS & IT";break;
  274. case "Electrical Engineering": $stream_code="EEE";break;
  275. case "Electronics &amp; Communication Engineering": $stream_code="ECE";break;
  276. case "Electronics & Communication Engineering": $stream_code="ECE";break;
  277. case "Electronics & Communication Engineering": $stream_code="ECE";break;
  278. case "Instrumentation Engineering": $stream_code="IN";break;
  279. case "Mechanical Engineering": $stream_code="ME";break;
  280. case "Monthaly Subscription Pack": $stream_code="SUB";break;
  281.  
  282. }
  283. return $stream_code;
  284.  
  285. }
  286. function get_eluctureCode($str)
  287. {
  288.  
  289.  
  290.  
  291. switch($str){
  292.  
  293. case "Online": $Code="OL";break;
  294. case "dvd": $Code="DVD";break;
  295. case "N/A": $Code="NA";break;
  296. case "Tablet": $Code="TAB";break;
  297. case "SDcard": $Code="SDC";break;
  298. case "Calling Tablet": $Code="CALTAB";break;
  299. case "Samsung Tablet": $Code="SAMTAB";break;
  300. case "tabGATE": $Code="TABG";break;
  301. case "USB": $Code="USB";break;
  302.  
  303. }
  304. return $Code;
  305.  
  306.  
  307.  
  308.  
  309.  
  310. }
  311.  
  312.  
  313.  
  314. function get_testSeriesCode($str)
  315. {
  316.  
  317.  
  318.  
  319. switch($str){
  320.  
  321. case "Online": $Code="OL";break;
  322.  
  323.  
  324. }
  325. return $Code;
  326.  
  327.  
  328.  
  329.  
  330.  
  331. }
  332. function apiSmcInteratio($ord_id){
  333.  
  334.  
  335.  
  336.  
  337. $order = new WC_Order($ord_id);
  338.  
  339. date_default_timezone_set("Asia/Calcutta");
  340.  
  341. $date=date("m-d-Y");
  342. $items = $order->get_items();
  343.  
  344.  
  345.  
  346. $Stream=$order->order_custom_fields['Stream'][0];
  347.  
  348.  
  349.  
  350.  
  351. $stream_code=$this->get_stream_code($Stream);
  352.  
  353.  
  354.  
  355.  
  356. if(isset($order->order_custom_fields['Mode of E-Lectures'][0])){
  357.  
  358. $Electure=$order->order_custom_fields['Mode of E-Lectures'][0];
  359.  
  360. $ElectureType= $this->get_eluctureCode($Electure);
  361.  
  362.  
  363.  
  364. }
  365. else {$ElectureType= "";}
  366.  
  367. if(isset($order->order_custom_fields['Mode of Test Series'][0])){
  368.  
  369. $TestSeries=$order->order_custom_fields['Mode of Test Series'][0];
  370.  
  371.  
  372.  
  373.  
  374.  
  375. $TestSeriesType= $this->get_testSeriesCode($TestSeries);
  376.  
  377.  
  378. }
  379. else {$TestSeriesType= "";}
  380.  
  381.  
  382.  
  383.  
  384. $tar_year="";
  385. foreach ( $items as $item ) {
  386. $product_name = $item['name'];
  387. $product_id = $item['product_id'];
  388.  
  389.  
  390.  
  391. if(isset($item['pa_gate-target-year'])){
  392. $tar_year= $item['pa_gate-target-year'];
  393.  
  394. $tar_year=preg_replace("/[^0-9]/","", $tar_year);
  395. $ValidityDate="02-28-".$tar_year;
  396.  
  397. }
  398.  
  399. elseif(isset($item['Yearly Subscription'])){
  400.  
  401.  
  402. $tar_year= $item['Yearly Subscription'];
  403.  
  404. $tar_year=preg_replace("/[^0-9]/","", $tar_year);
  405.  
  406. $ValidityDate="02-28-".$tar_year;
  407.  
  408. }
  409. else{
  410. $tar_year=preg_replace("/[^0-9]/","","Target Year 2017");
  411. $ValidityDate="02-28-".$tar_year;
  412.  
  413. }
  414.  
  415.  
  416. $sub_code= rwmb_meta( "sub_code",'type=text',$product_id);
  417. $BookType=rwmb_meta( "BookType",'type=select',$product_id);
  418. if($BookType=="NA"){$BookType="";}
  419.  
  420.  
  421. //print_r($item);
  422.  
  423. if (array_key_exists("Monthly Package", $item))
  424. {
  425.  
  426. $month=preg_replace("/[^0-9]/","",$item['Monthly Package']);
  427. $sub_code=$sub_code.$month;
  428.  
  429. $timestamp1 = strtotime(date("y-m-d"))+(2592000*$month);
  430.  
  431. $ValidityDate=gmdate("m-d-Y", $timestamp1);
  432.  
  433. $tar_year=preg_replace("/[^0-9]/","","Target Year 2017");
  434.  
  435. }
  436.  
  437.  
  438. $product_cats = wp_get_post_terms( $product_id, 'product_cat' );
  439. $i=0;
  440. while($product_cats){
  441.  
  442.  
  443. $single_cat = array_shift( $product_cats );
  444. $cat_array[$i] =$single_cat->slug;
  445. $cat_id_array[$i] =$single_cat->id;
  446. $i++;
  447.  
  448.  
  449.  
  450. }
  451.  
  452.  
  453.  
  454. $res_code=$_REQUEST['pgRespCode'];
  455. /*if($order->payment_method_title!="Cash on Delivery" && $sub_code!=""){*/
  456. if(1){
  457.  
  458. $paymentMode="Net Banking";
  459.  
  460. $transaction_key=$order->order_key;
  461.  
  462. $pi=0;
  463.  
  464. $pterms = get_the_terms( $product_id, 'product_cat' );
  465. $micropack=0;
  466. foreach ($pterms as $pterm) {
  467. if($pterm->name=="micropack"){$micropack=1;break;}
  468.  
  469. }
  470. if($micropack){
  471. $TestSeriesType='OL';
  472. $ElectureType='OL';
  473. }
  474.  
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481. if($order->payment_method_title=="Net Banking"){$paymentMode="Net Banking";}
  482. elseif($order->payment_method_title=="Credit Card"){$paymentMode="Credit Card";}
  483. else{$paymentMode="Debit Card";}
  484.  
  485. $fields = array(
  486. 'StudentName' =>urlencode( $order->billing_first_name." ".$order->billing_last_name),
  487. 'PhoneNumber' =>urlencode( $order->billing_phone),
  488. 'EmailId' => urlencode($order->billing_email),
  489. 'CourseCode' => urlencode($sub_code),
  490. 'StreamCode' => urlencode($stream_code),
  491. 'ExamTargetYear' => urlencode( $tar_year),
  492. 'ElectureType' => urlencode($ElectureType),
  493. 'TestSeriesType' => urlencode($TestSeriesType),
  494. 'BookType' => urlencode($BookType),
  495. 'AdmissionDate' => urlencode($date),
  496. 'CourseFees' => urlencode($order->order_total),
  497. 'DiscountAmount' =>urlencode($order->order_discount),
  498. 'TransactionID' => urlencode($transaction_key),
  499. 'PaymentMode' =>urlencode($paymentMode),
  500. 'ValidityDate' =>urlencode($ValidityDate)
  501. );
  502.  
  503.  
  504.  
  505.  
  506. $fields_string="";
  507.  
  508.  
  509. //url-ify the data for the POST
  510. foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
  511.  
  512. trim($fields_string, ' ');
  513. $fields_string=rtrim($fields_string, '&');
  514.  
  515. $out_json_string=$this->add_student_checkout_detail($fields_string);
  516.  
  517.  
  518. }
  519.  
  520. }
  521.  
  522.  
  523.  
  524. }
  525. /********End Api Integration***********/
  526. /**
  527. * Admin Panel Options
  528. * - Options for bits like 'title' and availability on a country-by-country basis
  529. *
  530. * @access public
  531. * @return void
  532. */
  533. function admin_options() {
  534. ?>
  535. <h3><?php _e('Cash on Delivery','woocommerce'); ?></h3>
  536. <p><?php _e('Have your customers pay with cash (or by other means) upon delivery.', 'woocommerce' ); ?></p>
  537. <table class="form-table">
  538. <?php $this->generate_settings_html(); ?>
  539. </table> <?php
  540. }
  541.  
  542.  
  543. /**
  544. * Initialise Gateway Settings Form Fields
  545. *
  546. * @access public
  547. * @return void
  548. */
  549. function init_form_fields() {
  550. global $woocommerce;
  551.  
  552. $shipping_methods = array();
  553.  
  554. if ( is_admin() )
  555. foreach ( $woocommerce->shipping->load_shipping_methods() as $method ) {
  556. $shipping_methods[ $method->id ] = $method->get_title();
  557. }
  558.  
  559. $this->form_fields = array(
  560. 'enabled' => array(
  561. 'title' => __( 'Enable COD', 'woocommerce' ),
  562. 'label' => __( 'Enable Cash on Delivery', 'woocommerce' ),
  563. 'type' => 'checkbox',
  564. 'description' => '',
  565. 'default' => 'no'
  566. ),
  567. 'title' => array(
  568. 'title' => __( 'Title', 'woocommerce' ),
  569. 'type' => 'text',
  570. 'description' => __( 'Payment method title that the customer will see on your website.', 'woocommerce' ),
  571. 'default' => __( 'Cash on Delivery', 'woocommerce' ),
  572. 'desc_tip' => true,
  573. ),
  574. 'description' => array(
  575. 'title' => __( 'Description', 'woocommerce' ),
  576. 'type' => 'textarea',
  577. 'description' => __( 'Payment method description that the customer will see on your website.', 'woocommerce' ),
  578. 'default' => __( 'Pay with cash upon delivery.', 'woocommerce' ),
  579. ),
  580. 'instructions' => array(
  581. 'title' => __( 'Instructions', 'woocommerce' ),
  582. 'type' => 'textarea',
  583. 'description' => __( 'Instructions that will be added to the thank you page.', 'woocommerce' ),
  584. 'default' => __( 'Pay with cash upon delivery.', 'woocommerce' )
  585. ),
  586. 'enable_for_methods' => array(
  587. 'title' => __( 'Enable for shipping methods', 'woocommerce' ),
  588. 'type' => 'multiselect',
  589. 'class' => 'chosen_select',
  590. 'css' => 'width: 450px;',
  591. 'default' => '',
  592. 'description' => __( 'If COD is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'woocommerce' ),
  593. 'options' => $shipping_methods,
  594. 'desc_tip' => true,
  595. )
  596. );
  597. }
  598.  
  599.  
  600. /**
  601. * Check If The Gateway Is Available For Use
  602. *
  603. * @access public
  604. * @return bool
  605. */
  606. function is_available() {
  607. global $woocommerce;
  608.  
  609. if ( ! empty( $this->enable_for_methods ) ) {
  610.  
  611. if ( is_page( woocommerce_get_page_id( 'pay' ) ) ) {
  612.  
  613. $order_id = (int) $_GET['order_id'];
  614. $order = new WC_Order( $order_id );
  615.  
  616. if ( ! $order->shipping_method )
  617. return false;
  618.  
  619. $chosen_method = $order->shipping_method;
  620.  
  621. } elseif ( empty( $woocommerce->session->chosen_shipping_method ) ) {
  622. return false;
  623. } else {
  624. $chosen_method = $woocommerce->session->chosen_shipping_method;
  625. }
  626.  
  627. $found = false;
  628.  
  629. foreach ( $this->enable_for_methods as $method_id ) {
  630. if ( strpos( $chosen_method, $method_id ) === 0 ) {
  631. $found = true;
  632. break;
  633. }
  634. }
  635.  
  636. if ( ! $found )
  637. return false;
  638. }
  639.  
  640. return parent::is_available();
  641. }
  642.  
  643.  
  644. /**
  645. * Process the payment and return the result
  646. *
  647. * @access public
  648. * @param int $order_id
  649. * @return array
  650. */
  651. function process_payment ($order_id) {
  652. global $woocommerce;
  653.  
  654. $order = new WC_Order( $order_id );
  655.  
  656. // Mark as on-hold (we're awaiting the cheque)
  657. $order->update_status('on-hold', __( 'Payment to be made upon delivery.', 'woocommerce' ));
  658.  
  659. // Reduce stock levels
  660. $order->reduce_order_stock();
  661.  
  662. // Remove cart
  663. $woocommerce->cart->empty_cart();
  664. $this->apiSmcInteratio($order_id);
  665. // Return thankyou redirect
  666. return array(
  667. 'result' => 'success',
  668. 'redirect' => add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(woocommerce_get_page_id('thanks'))))
  669. );
  670. }
  671.  
  672.  
  673. /**
  674. * Output for the order received page.
  675. *
  676. * @access public
  677. * @return void
  678. */
  679. function thankyou() {
  680. echo $this->instructions != '' ? wpautop( $this->instructions ) : '';
  681. }
  682.  
  683. }
  684.  
  685. class-wc-gateway-cod.php
  686. Displaying class-wc-gateway-cod.php.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement