PageRenderTime 28ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-user-frontend/lib/gateway/paypal.php

https://gitlab.com/Gashler/sg
PHP | 174 lines | 106 code | 30 blank | 38 comment | 14 complexity | 48f7c80c5db964707cd11b6e3d3142d7 MD5 | raw file
  1. <?php
  2. /**
  3. * WP User Frotnend Paypal gateway
  4. *
  5. * @since 0.8
  6. * @package WP User Frontend
  7. */
  8. class WPUF_Paypal {
  9. private $gateway_url;
  10. private $test_mode;
  11. function __construct() {
  12. $this->gateway_url = 'https://www.paypal.com/webscr/';
  13. $this->test_mode = false;
  14. add_action( 'wpuf_gateway_paypal', array($this, 'prepare_to_send') );
  15. add_action( 'wpuf_options_payment', array($this, 'payment_options') );
  16. add_action( 'init', array($this, 'paypal_success') );
  17. }
  18. /**
  19. * Adds paypal specific options to the admin panel
  20. *
  21. * @param type $options
  22. * @return string
  23. */
  24. function payment_options( $options ) {
  25. $options[] = array(
  26. 'name' => 'paypal_email',
  27. 'label' => __( 'Paypal Email', 'wpuf' )
  28. );
  29. return $options;
  30. }
  31. /**
  32. * Prepare the payment form and send to paypal
  33. *
  34. * @since 0.8
  35. * @param array $data payment info
  36. */
  37. function prepare_to_send( $data ) {
  38. $listener_url = add_query_arg( 'action', 'wpuf_paypal_success', home_url( '/' ) );
  39. $return_url = add_query_arg( 'action', 'wpuf_paypal_success', get_permalink( wpuf_get_option( 'payment_success', 'wpuf_payment' ) ) );
  40. $paypal_args = array(
  41. 'cmd' => '_xclick',
  42. 'amount' => $data['price'],
  43. 'business' => wpuf_get_option( 'paypal_email', 'wpuf_payment' ),
  44. 'item_name' => $data['item_name'],
  45. 'item_number' => $data['item_number'],
  46. 'email' => $data['user_info']['email'],
  47. 'no_shipping' => '1',
  48. 'no_note' => '1',
  49. 'currency_code' => $data['currency'],
  50. 'charset' => 'UTF-8',
  51. 'custom' => json_encode( array( 'user_id' => get_current_user_id(), 'type' => $data['type'] ) ),
  52. 'rm' => '2',
  53. 'return' => $return_url,
  54. 'notify_url' => $listener_url,
  55. 'cbt' => sprintf( __( 'Click here to complete the purchase on %s', 'wpuf' ), get_bloginfo( 'name' ) )
  56. );
  57. $this->set_mode();
  58. $paypal_url = $this->gateway_url . '?' . http_build_query( $paypal_args );
  59. wp_redirect( $paypal_url );
  60. exit;
  61. }
  62. /**
  63. * Set the payment mode to sandbox or live
  64. *
  65. * @since 0.8
  66. */
  67. function set_mode() {
  68. if ( wpuf_get_option( 'sandbox_mode', 'wpuf_payment' ) == 'on' ) {
  69. $this->gateway_url = 'https://www.sandbox.paypal.com/webscr/';
  70. $this->test_mode = true;
  71. }
  72. }
  73. /**
  74. * Handle the payment info sent from paypal
  75. *
  76. * @since 0.8
  77. */
  78. function paypal_success() {
  79. if ( isset( $_GET['action'] ) && $_GET['action'] == 'wpuf_paypal_success' ) {
  80. $postdata = $_POST;
  81. //var_dump( $postdata );exit;
  82. $item_number = $postdata['item_number'];
  83. $amount = $postdata['mc_gross'];
  84. $payment_status = strtolower( $postdata['payment_status'] );
  85. //verify payment
  86. $verified = $this->validateIpn();
  87. $custom = json_decode( stripcslashes( $postdata['custom'] ) );
  88. switch ($custom->type ) {
  89. case 'post':
  90. $post_id = $item_number;
  91. $pack_id = 0;
  92. break;
  93. case 'pack':
  94. $post_id = 0;
  95. $pack_id = $item_number;
  96. break;
  97. }
  98. if ( $verified || $this->test_mode ) {
  99. $data = array(
  100. 'user_id' => (int) $custom->user_id,
  101. 'status' => 'completed',
  102. 'cost' => $postdata['mc_gross'],
  103. 'post_id' => $post_id,
  104. 'pack_id' => $pack_id,
  105. 'payer_first_name' => $postdata['first_name'],
  106. 'payer_last_name' => $postdata['last_name'],
  107. 'payer_email' => $postdata['payer_email'],
  108. 'payment_type' => 'Paypal',
  109. 'payer_address' => $postdata['residence_country'],
  110. 'transaction_id' => $postdata['txn_id'],
  111. 'created' => current_time( 'mysql' )
  112. );
  113. WPUF_Payment::insert_payment( $data, $postdata['txn_id'] );
  114. }
  115. }
  116. }
  117. /**
  118. * Validate the IPN notification
  119. *
  120. * @param none
  121. * @return boolean
  122. */
  123. public function validateIpn() {
  124. $this->set_mode();
  125. // Get recieved values from post data
  126. $ipn_data = (array) stripslashes_deep( $_POST );
  127. $ipn_data['cmd'] = '_notify-validate';
  128. // Send back post vars to paypal
  129. $params = array(
  130. 'body' => $ipn_data,
  131. 'sslverify' => false,
  132. 'timeout' => 30,
  133. 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ),
  134. );
  135. $response = wp_remote_post( $this->gateway_url, $params );
  136. if ( !is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && (strcmp( $response['body'], "VERIFIED" ) == 0) ) {
  137. return true;
  138. } else {
  139. WPUF_Main::log( 'error', "IPN Failed\n" . $ipn_response );
  140. return false;
  141. }
  142. }
  143. }
  144. $wpuf_paypal = new WPUF_Paypal();