77 */
88
99use Omnipay \SagePay \Message \DirectAuthorizeRequest ;
10+ use Omnipay \Common \Exception \InvalidRequestException ;
1011
1112class AuthorizeRequest extends DirectAuthorizeRequest
1213{
1314 /**
1415 * Fields accepted by the Form API.
15- * "true" fields are mandatory.
16+ * "true" fields are mandatory, "false" fields are optional.
17+ * The DeliveryState is conditionally mandatory.
1618 */
1719 protected $ validFields = [
1820 'VendorTxCode ' => true ,
@@ -89,20 +91,43 @@ public function getCryptData()
8991
9092 $ data = array_intersect_key ($ data , $ this ->validFields );
9193
92- // TODO: throw exception if any mandatory fields are missing.
94+ // Throw exception if any mandatory fields are missing.
9395 // We need to catch it here before sending the user to an
94- // unexpected on the gateway site.
96+ // generic (and useless) error on the gateway site.
97+
98+ foreach ($ this ->validFields as $ fieldName => $ mandatoryFlag ) {
99+ if ($ mandatoryFlag && ! isset ($ data [$ fieldName ])) {
100+ throw new InvalidRequestException (sprintf (
101+ 'The %s parameter is required ' ,
102+ $ fieldName
103+ ));
104+ }
105+ }
106+
107+ // Two conditional checks on the "state" fields.
108+ // We don't check if it is a valid two-character state code.
95109
96- // ...
110+ if ($ data ['BillingCountry ' ] === 'US ' && empty ($ data ['BillingState ' ])
111+ || $ data ['DeliveryCountry ' ] === 'US ' && empty ($ data ['DeliveryState ' ])
112+ ) {
113+ throw new InvalidRequestException (
114+ 'Missing state code for billing or shipping address '
115+ );
116+ }
97117
98118 return $ data ;
99119 }
100120
101121 /**
102122 * Add the Form-specific details to the base data.
123+ * @reurn array
103124 */
104125 public function getData ()
105126 {
127+ $ this ->validate ('currency ' , 'description ' );
128+
129+ // The test mode is included to determine the redirect URL.
130+
106131 return [
107132 'VPSProtocol ' => $ this ->VPSProtocol ,
108133 'TxType ' => $ this ->getTxType (),
@@ -114,6 +139,8 @@ public function getData()
114139
115140 /**
116141 * Generate the crypt field from the source data.
142+ * @param array $data the name/value pairs to be encrypted
143+ * @return string encrypted data
117144 */
118145 public function generateCrypt (array $ data )
119146 {
0 commit comments