CollectPayment.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Action_CollectPayment
  4. */
  5. final class NF_Actions_CollectPayment extends NF_Abstracts_Action
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $_name = 'collectpayment';
  11. /**
  12. * @var array
  13. */
  14. protected $_tags = array();
  15. /**
  16. * @var string
  17. */
  18. protected $_timing = 'late';
  19. /**
  20. * @var int
  21. */
  22. protected $_priority = 0;
  23. /**
  24. * @var array
  25. */
  26. protected $payment_gateways = array();
  27. /**
  28. * Constructor
  29. *
  30. * @param string $cp_nice_name
  31. * @param string $cp_name
  32. */
  33. public function __construct( $cp_nice_name = 'Collect Payment',
  34. $cp_name = 'collectpayment' )
  35. {
  36. parent::__construct();
  37. // Set the nice name to what we passed in. 'Collect Payment' is default
  38. if( 'Collect Payment' == $cp_nice_name ) {
  39. $cp_nice_name = __( 'Collect Payment', 'ninja-forms' );
  40. }
  41. $this->_nicename = $cp_nice_name;
  42. // Set name to what we passed in. 'collectpayment' is default
  43. $this->_name = strtolower( $cp_name );
  44. $settings = Ninja_Forms::config( 'ActionCollectPaymentSettings' );
  45. /**
  46. * if we pass in something other than 'collectpayment', set the value
  47. * of the gateway drop-down
  48. **/
  49. if ( 'collectpayment' != $this->_name ) {
  50. $settings[ 'payment_gateways' ][ 'value' ] = $this->_name;
  51. }
  52. $this->_settings = array_merge( $this->_settings, $settings );
  53. add_action( 'ninja_forms_loaded', array( $this, 'register_payment_gateways' ), -1 );
  54. add_filter( 'ninja_forms_action_type_settings', array( $this, 'maybe_remove_action' ) );
  55. }
  56. public function save( $action_settings )
  57. {
  58. }
  59. public function process( $action_settings, $form_id, $data )
  60. {
  61. $payment_gateway = $action_settings[ 'payment_gateways' ];
  62. $payment_gateway_class = $this->payment_gateways[ $payment_gateway ];
  63. /*
  64. * Get our payment total if we have old data. (not used in current version)
  65. *
  66. * If we have selected "Calc" as our total type, then we want to use payment_total_calc
  67. *
  68. * If we have selected "Field" as our total type, then we want to use payment_total_field
  69. *
  70. * If we have selected "Custom" as our total type, then we want to use payment_total_fixed
  71. */
  72. // $total_type = isset( $action_settings[ 'payment_total_type' ] ) ? $action_settings[ 'payment_total_type' ] : 'payment_total_fixed';
  73. //
  74. // switch ( $total_type ) {
  75. // case 'calc':
  76. // $payment_total = $action_settings[ 'payment_total_calc' ];
  77. // break;
  78. // case 'field':
  79. // $payment_total = $action_settings[ 'payment_total_field' ];
  80. // break;
  81. // case 'fixed':
  82. // $payment_total = $action_settings[ 'payment_total_fixed' ];
  83. // break;
  84. // default:
  85. // $payment_total = $action_settings[ 'payment_total_fixed' ];
  86. // break;
  87. // }
  88. //
  89. // return $payment_gateway_class->process( $action_settings, $form_id, $data, $payment_total );
  90. // The above block is not actually being used.
  91. return $payment_gateway_class->process( $action_settings, $form_id, $data );
  92. }
  93. public function register_payment_gateways()
  94. {
  95. $this->payment_gateways = apply_filters( 'ninja_forms_register_payment_gateways', array() );
  96. foreach( $this->payment_gateways as $gateway ){
  97. if( ! is_subclass_of( $gateway, 'NF_Abstracts_PaymentGateway' ) ){
  98. continue;
  99. }
  100. $this->_settings[ 'payment_gateways' ][ 'options' ][] = array(
  101. 'label' => $gateway->get_name(),
  102. 'value' => $gateway->get_slug(),
  103. );
  104. $this->_settings = array_merge( $this->_settings, $gateway->get_settings() );
  105. }
  106. }
  107. public function maybe_remove_action( $action_type_settings )
  108. {
  109. if( empty( $this->payment_gateways ) ){
  110. unset( $action_type_settings[ $this->_name ] );
  111. }
  112. return $action_type_settings;
  113. }
  114. }