class.swpm-transactions.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /*
  3. * Provides some helpful functions to deal with the transactions
  4. */
  5. class SwpmTransactions {
  6. static function save_txn_record( $ipn_data, $items = array() ) {
  7. global $wpdb;
  8. $current_date = SwpmUtils::get_current_date_in_wp_zone();//date( 'Y-m-d' );
  9. $custom_var = self::parse_custom_var( $ipn_data['custom'] );
  10. $txn_data = array();
  11. $txn_data['email'] = $ipn_data['payer_email'];
  12. $txn_data['first_name'] = $ipn_data['first_name'];
  13. $txn_data['last_name'] = $ipn_data['last_name'];
  14. $txn_data['ip_address'] = $ipn_data['ip'];
  15. $txn_data['member_id'] = isset ( $custom_var['swpm_id'] ) ? $custom_var['swpm_id'] : '';
  16. $txn_data['membership_level'] = isset ( $custom_var['subsc_ref'] ) ? $custom_var['subsc_ref'] : '';
  17. $txn_data['txn_date'] = $current_date;
  18. $txn_data['txn_id'] = $ipn_data['txn_id'];
  19. $txn_data['subscr_id'] = $ipn_data['subscr_id'];
  20. $txn_data['reference'] = isset( $custom_var['reference'] ) ? $custom_var['reference'] : '';
  21. $txn_data['payment_amount'] = $ipn_data['mc_gross'];
  22. $txn_data['gateway'] = $ipn_data['gateway'];
  23. $txn_data['status'] = $ipn_data['status'];
  24. $txn_data = array_filter( $txn_data );//Remove any null values.
  25. $wpdb->insert( $wpdb->prefix . 'swpm_payments_tbl', $txn_data );
  26. $db_row_id = $wpdb->insert_id;
  27. //let's also store transactions data in swpm_transactions CPT
  28. $post = array();
  29. $post['post_title'] = '';
  30. $post['post_status'] = 'publish';
  31. $post['content'] = '';
  32. $post['post_type'] = 'swpm_transactions';
  33. $post_id = wp_insert_post( $post );
  34. update_post_meta( $post_id, 'db_row_id', $db_row_id );
  35. if ( isset( $ipn_data['payment_button_id'] ) ) {
  36. $txn_data['payment_button_id'] = $ipn_data['payment_button_id'];
  37. }
  38. if ( isset( $ipn_data['is_live'] ) ) {
  39. $txn_data['is_live'] = $ipn_data['is_live'];
  40. }
  41. foreach ( $txn_data as $key => $value ) {
  42. update_post_meta( $post_id, $key, $value );
  43. }
  44. do_action( 'swpm_txn_record_saved', $txn_data, $db_row_id, $post_id );
  45. }
  46. static function parse_custom_var( $custom ) {
  47. $delimiter = '&';
  48. $customvariables = array();
  49. $namevaluecombos = explode( $delimiter, $custom );
  50. foreach ( $namevaluecombos as $keyval_unparsed ) {
  51. $equalsignposition = strpos( $keyval_unparsed, '=' );
  52. if ( $equalsignposition === false ) {
  53. $customvariables[ $keyval_unparsed ] = '';
  54. continue;
  55. }
  56. $key = substr( $keyval_unparsed, 0, $equalsignposition );
  57. $value = substr( $keyval_unparsed, $equalsignposition + 1 );
  58. $customvariables[ $key ] = $value;
  59. }
  60. return $customvariables;
  61. }
  62. static function get_transaction_row_by_subscr_id ($subscr_id) {
  63. global $wpdb;
  64. $query_db = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}swpm_payments_tbl WHERE subscr_id = %s", $subscr_id ), OBJECT );
  65. return $query_db;
  66. }
  67. }