Lasso Soft Inc. > Home

[paypal_wsapi]

Linkpaypal_wsapi
AuthorJason Huck
CategoryUtility
Version8.x
LicensePublic Domain
Posted18 Sep 2007
Updated19 Sep 2007
More by this author...

Description

A set of custom types and tags to work with PayPal's Web Services API (i.e. Website Payments Pro and related services). The complete API (version 2.0) is represented, though not all aspects have been tested. It should definitely be considered beta quality. At the time it was written, Lasso's SOAP client could not construct the custom headers required for the response and request envelopes, so they are built using string manipulation instead. There is likely a better way to do it now.

The types and tags match up to the services and methods in the API in a one-to-one fashion. Apologies for the lack of any further documentation, but hopfeully it saves some people some time.

Sample Usage

// do direct payment (excerpted from a larger script)

// build the request components
var('address') = paypal_address(
	-Street1 = $billingdata->find('billaddress1'),
	-Street2 = $billingdata->find('billaddress2'),
	-CityName = $billingdata->find('billcity'),
	-StateOrProvince = $billingdata->find('billstate'),
	-Country = $billingdata->find('billcountry'),
	-PostalCode = $billingdata->find('billzip')
);

var('payername') = paypal_payername(
	-FirstName = $billingdata->find('billfirst'),
	-LastName = $billingdata->find('billlast')
);

var('payerinfo') = paypal_payerinfo(
	-Payer = $formdata->find('email'),
	-PayerName = $payername,
	-Address = $address
);

var('creditcarddetails') = paypal_creditcard(
	-CardOwner = $payerinfo,
	-CreditCardType = $billingdata->find('cardtype'),
	-CreditCardNumber = $billingdata->find('cardnumber'),
	-ExpMonth = $billingdata->find('expiresmonth'),
	-ExpYear = $billingdata->find('expiresyear'),
	-CVV2 = $billingdata->find('securitycode')
);

var('paymentdetails') = paypal_paymentdetails(
	-OrderTotal = '75.00'
);

// submit the request
var('response') = paypal_dodirectpayment(
	-auth=$pp_auth,
	-PaymentAction = 'Sale',
	-PaymentDetails = $paymentdetails,
	-CreditCard = $creditcarddetails,
	-IPAddress = client_ip
);

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

// Lasso PayPal API 1.0b
// Author: Jason Huck/Core Five Creative
// Implements version 2.0 of the PayPal Web Services API for Lasso Professional 8.x.
// This should definitely be considered beta software.

// PayPal API configuration
// You must configure these variables in order to use the tags.
var(
	'paypal_version' = '2.0',
	'paypal_serviceurl' = 'https://api.sandbox.paypal.com/2.0/',
	'paypal_sslcertpath' = '',
	'paypal_sslkeypath' = '',
	'pp_username' = '',
	'pp_password' = '',
	'pp_buyeremail' = ''
);

if(!lasso_tagexists('paypal_countrycodes'));
	define_constant(
		'paypal_countrycodes', 
		array(
			'Anguilla' = 'AI',
			'Argentina' = 'AR',
			'Australia' = 'AU',
			'Austria' = 'AT',
			'Belgium' = 'BE',
			'Brazil' = 'BR',
			'Canada' = 'CA',
			'Chile' = 'CL',
			'China' = 'CN',
			'Costa Rica' = 'CR',
			'Cyprus' = 'CY',
			'Czech Republic' = 'CZ',
			'Denmark' = 'DK',
			'Dominican Republic' = 'DO',
			'Ecuador' = 'EC',
			'Estonia' = 'EE',
			'Finland' = 'FI',
			'France' = 'FR',
			'Germany' = 'DE',
			'Greece' = 'GR',
			'Hong Kong' = 'HK',
			'Hungary' = 'HU',
			'Iceland' = 'IS',
			'India' = 'IN',
			'Ireland' = 'IE',
			'Israel' = 'IL',
			'Italy' = 'IT',
			'Jamaica' = 'JM',
			'Japan' = 'JP',
			'Latvia' = 'LV',
			'Lithuania' = 'LT',
			'Luxembourg' = 'LU',
			'Malaysia' = 'MY',
			'Malta' = 'MT',
			'Mexico' = 'MX',
			'Netherlands' = 'NL',
			'New Zealand' = 'NZ',
			'Norway' = 'NO',
			'Poland' = 'PL',
			'Portugal' = 'PT',
			'Singapore' = 'SG',
			'Slovakia' = 'SK',
			'Slovenia' = 'SI',
			'South Africa' = 'ZA',
			'South Korea' = 'KR',
			'Spain' = 'ES',
			'Sweden' = 'SE',
			'Switzerland' = 'CH',
			'Taiwan' = 'TW',
			'Thailand' = 'TH',
			'Turkey' = 'TR',
			'United Kingdom' = 'GB',
			'United States' = 'US',
			'Uruguay' = 'UY',
			'Venezuela' = 'VE'
		)
	);
/if;



define_tag(
	'docapture',
	-namespace='paypal_',
	-priority='replace',
	-required='auth',
	-required='AuthorizationID',
	-required='Amount',
	-required='CompleteType',
	-optional='InvoiceID',
	-optional='Note',
	-optional='currencyID',
	-optional='outputonly',
	-description='Sends a DoCapture request to PayPal WS/API and parses the response.'
);
	!local_defined('currencyID') ? local('currencyID' = 'USD');

	local('elements') = array(
		paypal_element(
			-name='AuthorizationID',
			-datatype='xs:string',
			-value=#AuthorizationID
		),
		paypal_element(
			-name='Amount',
			-datatype='ebl:BasicAmountType',
			-value=#Amount,
			-attributes=array('currencyID' = #currencyID)
		),
		paypal_element(
			-name='CompleteType',
			-datatype='ebl:CompleteCodeType',
			-value=#CompleteType
		)
	);
	
	local_defined('InvoiceID') ? #elements->insert(
		paypal_element(
			-name='InvoiceID',
			-datatype='xs:string',
			-value=#InvoiceID
		)
	);
	
	local_defined('Note') ? #elements->insert(
		paypal_element(
			-name='Note',
			-datatype='xs:string',
			-value=#Note
		)
	);
	
	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='DoCapture',
			-elements=#elements
		)
	);
	
	if(params >> '-outputonly');
		local('out') = #request->output;
	else;
		local('out') = paypal_parseresponse(#request->send);
	/if;

	// local('out') = #request->output;
	// local('out') = #request->send;

	return(#out);
/define_tag;





define_tag(
	'doauthorization',
	-namespace='paypal_',
	-priority='replace',
	-required='auth',
	-required='TransactionID',
	-required='Amount',
	-optional='TransactionEntity',
	-optional='currencyID',
	-optional='outputonly',
	-description='Sends a DoAuthorization request to PayPal WS/API and parses the response.'
);
	!local_defined('currencyID') ? local('currencyID' = 'USD');

	local('elements') = array(
		paypal_element(
			-name='TransactionID',
			-datatype='xs:string',
			-value=#TransactionID
		),
		paypal_element(
			-name='Amount',
			-datatype='ebl:BasicAmountType',
			-value=#Amount,
			-attributes=array('currencyID' = #currencyID)
		)
	);
	
	local_defined('TransactionEntity') ? #elements->insert(
		paypal_element(
			-name='TransactionEntity',
			-datatype='ebl:TransactionEntityType',
			-value=#TransactionEntity
		)
	);
	
	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='DoAuthorization',
			-elements=#elements
		)
	);
	
	if(params >> '-outputonly');
		local('out') = #request->output;
	else;
		local('out') = paypal_parseresponse(#request->send);
	/if;

	// local('out') = #request->output;
	// local('out') = #request->send;

	return(#out);		
/define_tag;





define_tag(
	'dovoid',
	-namespace='paypal_',
	-priority='replace',
	-required='auth',
	-required='AuthorizationID',
	-optional='Note',
	-optional='outputonly',
	-description='Sends a DoVoid request to PayPal WS/API and parses the response.'
);
	local('elements') = array(
		paypal_element(
			-name='AuthorizationID',
			-datatype='xs:string',
			-value=#AuthorizationID
		),
		paypal_element(
			-name='Note',
			-datatype='xs:string',
			-value=#Note
		)
	);
	
	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='DoVoid',
			-elements=#elements
		)
	);
	
	if(params >> '-outputonly');
		local('out') = #request->output;
	else;
		local('out') = paypal_parseresponse(#request->send);
	/if;

	// local('out') = #request->output;
	// local('out') = #request->send;

	return(#out);		
/define_tag;




define_tag(
	'doreauthorization',
	-namespace='paypal_',
	-priority='replace',
	-required='auth',
	-required='AuthorizationID',
	-required='Amount',
	-optional='currencyID',
	-optional='outputonly',
	-description='Sends a DoReauthorization request to PayPal WS/API and parses the response.'
);
	!local_defined('currencyID') ? local('currencyID' = 'USD');

	local('elements') = array(
		paypal_element(
			-name='AuthorizationID',
			-datatype='xs:string',
			-value=#AuthorizationID
		),
		paypal_element(
			-name='Amount',
			-datatype='ebl:BasicAmountType',
			-value=#Amount,
			-attributes=array('currencyID' = #currencyID)
		)
	);
	
	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='DoReauthorization',
			-elements=#elements
		)
	);
	
	if(params >> '-outputonly');
		local('out') = #request->output;
	else;
		local('out') = paypal_parseresponse(#request->send);
	/if;

	// local('out') = #request->output;
	// local('out') = #request->send;

	return(#out);		
/define_tag;



define_tag(
	'dodirectpayment',
	-namespace='paypal_',
	-required='auth',
	-required='PaymentAction',
	-required='CreditCard',
	-required='PaymentDetails',
	-required='IPAddress',
	-optional='MerchantSessionID',
	-optional='outputonly',
	-priority='replace',
	-description='Sends a DoDirectPayment request to the PayPal WS/API and parses the response.'
);
	local('elements') = array(
		paypal_element(
			-name='PaymentAction',
			-datatype='',
			// -datatype='ebl:PaymentActionCodeType',
			-value=#PaymentAction,
			-namespace='urn:ebay:apis:eBLBaseComponents'
		),
		#CreditCard,
		#PaymentDetails,
		paypal_element(
			-name='IPAddress',
			-datatype='xs:string',
			-value=#IPAddress
		)
	);

	local_defined('MerchantSessionID') ? #elements->insert(
		paypal_element(
			-name='MerchantSessionID',
			-datatype='xs:string',
			-value=#MerchantSessionID
		)
	);

	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='DoDirectPayment',
			-elements=#elements,
			-namespace='urn:ebay:apis:eBLBaseComponents'
		)
	);
	
	if(params >> '-outputonly');
		local('out') = #request->output;
	else;
		local('out') = paypal_parseresponse(#request->send);
	/if;
	
	// local('out') = #request->output;
	// local('out') = #request->send;

	return(@#out);
/define_tag;



define_tag(
	'setexpresscheckout',
	-namespace='paypal_',
	-priority='replace',
	-required='ordertotal',
	-required='returnurl',
	-required='cancelurl',
	-optional='token',
	-optional='maxamount',
	-optional='orderdescription',
	-optional='custom',
	-optional='invoiceid',
	-optional='reqconfirmshipping',
	-optional='noshipping',
	-optional='addressoverride',
	-optional='localcode',
	-optional='pagestyle',
	-optional='cpp-header-image',
	-optional='cpp-header-border-color',
	-optional='cpp-header-back-color',
	-optional='cpp-payflow-color',
	-optional='address',
	-optional='paymentaction',
	-optional='buyeremail',
	-optional='currencyID',
	-required='auth',
	-optional='outputonly',	
	-description='Sends a SetExpressCheckout request to the PayPal WS/API and parses the response.'
);
	!local_defined('currencyID') ? local('currencyID' = 'USD');
			
	local(
		'elements' = array,
		'element' = null,
		'datatype' = string,
		'attributes' = array
	);
	
	iterate(params, local('i'));
		local('name' = #i->first);
		#name->removeleading('-');
	
		if((: 'Address', 'currencyID', 'auth') !>> #name && #i->second != '');
			local_reset(
				'element' = null,
				'datatype' = string,
				'attributes' = null
			);			
	
			select(true);
				case((:'OrderTotal','MaxAmount') >> #name);
					#datatype = 'cc:BasicAmountType';
					#attributes = array('currencyID' = #currencyID);
				case(#name == 'PaymentAction');
					#datatype = 'ebl:PaymentActionCodeType';
					#attributes = null;
				case;
					#datatype = 'xs:string';
					#attributes = null;
			/select;
				
			local('element') = paypal_element(
				-name=#name,
				-datatype=#datatype,
				-value=#i->second
			);
			
			#attributes->size ? #element->attributes = #attributes;					
			#elements->insert(#element);
		/if;
	/iterate;
	
	local_defined('Address') ? #elements->insert(#Address);
	
	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='SetExpressCheckout',
			-elements=#elements,
			-namespace='urn:ebay:apis:eBLBaseComponents'
		)
	);
	
	if(params >> '-outputonly');
		local('out') = #request->output;
	else;
		local('out') = paypal_parseresponse(#request->send);
	/if;

	// local('out') = #request->output;
	
	return(#out);
/define_tag;




define_tag(
	'getexpresscheckoutdetails',
	-namespace='paypal_',
	-required='token',
	-required='auth',
	-optional='outputonly',
	-priority='replace',
	-description='Sends a GetExpressCheckoutDetails request to the PayPal WS/API and parses the response.'
);
	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_simplemethod(
			-method='GetExpressCheckoutDetails',
			-elements=array(
				paypal_element(
					-name='Token',
					-datatype='xsd:string',
					-value=#token
				)
			),
			-namespace='urn:ebay:api:PayPalAPI'
		)
	);

	if(params >> '-outputonly');
		local('out') = #request->output;
	else;
		local('out') = paypal_parseresponse(#request->send);
	/if;

	// local('out') = #request->output;		

	return(#out);
/define_tag;




define_tag(
	'doexpresscheckoutpayment',
	-namespace='paypal_',
	-required='auth',
	-required='Token',
	-required='PaymentAction',
	-required='PayerID',
	-required='PaymentDetails',
	-optional='outputonly',
	-priority='replace',
	-description='Sends a DoExpressCheckoutPayment request to the PayPal WS/API and parses the response.'
);
	local('elements') = array(
		paypal_element(
			-name='Token',
			-datatype='xs:string',
			-value=#Token
		),
		paypal_element(
			-name='PaymentAction',
			-datatype='',
			// -datatype='ebl:PaymentActionCodeType',
			-value=#PaymentAction,
			-namespace='urn:ebay:apis:eBLBaseComponents'
		),
		paypal_element(
			-name='PayerID',
			-datatype='ebl:UserIDType',
			-value=#PayerID
		),
		#PaymentDetails
	);

	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='DoExpressCheckoutPayment',
			-elements=#elements,
			-namespace='urn:ebay:apis:eBLBaseComponents'
		)
	);
	
	if(params >> '-outputonly');
		local('out') = #request->output;
	else;
		local('out') = paypal_parseresponse(#request->send);
	/if;

	// local('out') = #request->output;
	// local('out') = #request->send;

	return(#out);
/define_tag;



define_tag(
	'gettransactiondetails',
	-namespace='paypal_',
	-priority='replace',
	-required='auth',
	-required='TransactionID',
	-optional='outputonly',
	-description='Sends a GetTransactionDetails request to PayPal WS/API and parses the response.'
);
	local('elements') = array(
		paypal_element(
			-name='TransactionID',
			-datatype='xs:string',
			-value=#AuthorizationID
		)
	);
	
	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='GetTransactionDetails',
			-elements=#elements
		)
	);
	
	if(params >> '-outputonly');
		local('out') = #request->output;
	else;
		local('out') = paypal_parseresponse(#request->send);
	/if;

	// local('out') = #request->output;
	// local('out') = #request->send;

	return(#out);		
/define_tag;



define_type(
	'masspayitem',
	-namespace='paypal_',
	-prototype,
	-description='The MassPayRequestItem type for the PayPal WS/API.'
);
	local(
		'ReceiverEmail' = string,
		'Amount' = decimal,
		'UniqueID' = string,
		'Note' = string,
		'currencyID' = 'USD'
	);
	
	define_tag(
		'onCreate',
		-required='ReceiverEmail',
		-required='Amount',
		-optional='UniqueID',
		-optional='Note',
		-optional='currencyID'
	);
		self->ReceiverEmail = #ReceiverEmail;
		self->Amount = #Amount;
		local_defined('UniqueID') ? self->UniqueID = #UniqueID;
		local_defined('Note') ? self->Note = #Note;
		local_defined('currencyID') ? self->currencyID = #currencyID;
	/define_tag;
	
	define_tag('output');
		local('elements') = array(
			paypal_element(
				-name='ReceiverEmail',
				-datatype='xs:string',
				-value=self->ReceiverEmail
			),
			paypal_element(
				-name='Amount',
				-datatype='xs:string',
				-value=self->Amount,
				-attributes=array('currencyID' = self->currencyID)
			),
		);
		
		local_defined('UniqueID') ? #elements->insert(
			paypal_element(
				-name='UniqueID',
				-datatype='xs:string',
				-value=self->UniqueID
			),
		);

		local_defined('Note') ? #elements->insert(
			paypal_element(
				-name='Note',
				-datatype='xs:string',
				-value=self->Note
			),
		);
		
		local('out') = paypal_wrapper(
			-name='MassPayItem',
			-elements=#elements
		);
		
		return(@#out->output);
	/define_tag;
/define_type;




define_tag(
	'masspay',
	-namespace='paypal_',
	-priority='replace',
	-required='auth',
	-optional='EmailSubject',
	-required='MassPayItems',
	-description='Sends a MassPay request to PayPal WS/API and parses the response.'
);
	local('elements' = array);
	
	local_defined('EmailSubject') ? #elements->insert(
		paypal_element(
			-name='EmailSubject',
			-datatype='xs:string',
			-value=#EmailSubject
		)
	);
	
	#elements->merge(#MassPayItems);
	
	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='MassPay',
			-elements=#elements
		)
	);
	
	local('out') = paypal_parseresponse(#request->send);
	// local('out') = #request->output;
	// local('out') = #request->send;

	return(#out);
/define_tag;



define_tag(
	'refundtransaction',
	-namespace='paypal_',
	-priority='replace',
	-required='auth',
	-required='TransactionID',
	-required='RefundType',
	-required='Amount',
	-optional='Memo',
	-optional='currencyID',
	-description='Sends a RefundTransaction request to PayPal WS/API and parses the response.'
);
	!local_defined('currencyID') ? local('currencyID' = 'USD');

	local('elements') = array(
		paypal_element(
			-name='TransactionID',
			-datatype='xs:string',
			-value=#TransactionID
		),
		paypal_element(
			-name='RefundType',
			-datatype='ebl:RefundPurposeTypeCodeType',
			-value=#RefundType
		),
		paypal_element(
			-name='Amount',
			-datatype='ebl:BasicAmountType',
			-value=#Amount,
			-attributes=array('currencyID' = #currencyID)
		)
	);
	
	local_defined('Memo') ? #elements->insert(
		paypal_element(
			-name='Memo',
			-datatype='xs:string',
			-value=#Memo
		)
	);
	
	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='RefundTransaction',
			-elements=#elements
		)
	);
	
	local('out') = paypal_parseresponse(#request->send);
	// local('out') = #request->output;
	// local('out') = #request->send;

	return(#out);		
/define_tag;



define_type(
	'request',
	-namespace='paypal_',
	-prototype,
	-description='A SOAP envelope for PayPal web service requests.'
);
	local(
		'auth' = null,
		'request' = null,
		'wrapper' = '\



##AUTH##


##REQUEST##

	
		'
	);
	
	define_tag(
		'onCreate',
		-required='auth',
		-required='request'
	);
		self->auth = #auth;
		self->request = #request;
	/define_tag;
	
	define_tag('output');
		local('out' = self->wrapper);
		#out->replace('##AUTH##', self->auth->output)&replace('##REQUEST##', self->request->output);
		return(@#out);
	/define_tag;
	
	define_tag('send');
		local('response') = include_url(
			$paypal_serviceurl,
			-postparams = self->output,
			-sendmimeheaders = array(
				'soapaction' = self->request->method, 
				'content-type' = 'text/xml; charset=utf-8'
			),
			-sslcert = $paypal_sslcertpath,
			-sslkey = $paypal_sslkeypath
		);
		
		#response->trim;
		
		return(@#response);
	/define_tag;
/define_type;




define_type(
	'authentication',
	-namespace='paypal_',
	-prototype,
	-description='Authorization wrapper for a PayPal web services request.'
);
	local(
		'username' = string,
		'password' = string,
		'wrapper' = '
	
		
			##USR##
			##PWD##
			
		
	
		'
	);
	
	define_tag(
		'onCreate',
		-required='username',
		-required='password'
	);
		self->username = #username;
		self->password = #password;
	/define_tag;
	
	define_tag('output');
		local('out' = self->wrapper);
		#out->replace('##USR##', self->username)&replace('##PWD##', self->password);
		return(@#out);
	/define_tag;
/define_type;




define_type(
	'method',
	-namespace='paypal_',
	-prototype,
	-description='Generic method wrapper for a PayPal web services request.'
);
	local(
		'method' = string,
		'elements' = array,
		'namespace' = '',
		'methodwrapper' = '
	<##METHOD##Req xmlns="urn:ebay:api:PayPalAPI">
		<##METHOD##Request xsi:type="ns:##METHOD##RequestType">
			' + $paypal_version + '
			<##METHOD##RequestDetails ##NS##>
##ELEMENTS##
			
		
	'
	);
	
	define_tag(
		'onCreate',
		-required='method',
		-type='string',
		-required='elements',
		-type='array',
		-optional='namespace',
		-type='string'
	);
		self->method = #method;
		self->elements = #elements;
		local_defined('namespace') ? self->namespace = #namespace;
	/define_tag;
	
	define_tag('output');
		local(
			'out' = self->methodwrapper,
			'data' = string
		);
		
		#out->replace('##METHOD##', self->method);
		
		self->namespace != '' ?
			#out->replace('##NS##', 'xmlns="' + self->namespace + '"')
			| #out->replace('##NS##', '');
		
		iterate(self->elements, local('i'));				
			#data += #i->output;
		/iterate;
		
		#out->replace('##ELEMENTS##', #data);
		
		return(@#out);
	/define_tag;
/define_type;





define_type(
	'simplemethod',
	-namespace='paypal_',
	-prototype,
	-description='Basic method wrapper for a PayPal web services request.'
);
	local(
		'method' = string,
		'elements' = array,
		'namespace' = '',
		'methodwrapper' = '
	<##METHOD##Req xmlns="urn:ebay:api:PayPalAPI">
		<##METHOD##Request xsi:type="ns:##METHOD##RequestType">
			' + $paypal_version + '
##ELEMENTS##
		
	'
	);
	
	define_tag(
		'onCreate',
		-required='method',
		-type='string',
		-required='elements',
		-type='array',
		-optional='namespace',
		-type='string'
	);
		self->method = #method;
		self->elements = #elements;
		local_defined('namespace') ? self->namespace = #namespace;
	/define_tag;
	
	define_tag('output');
		local(
			'out' = self->methodwrapper,
			'data' = string
		);
		
		#out->replace('##METHOD##', self->method);
		
		self->namespace != '' ?
			#out->replace('##NS##', 'xmlns="' + self->namespace + '"')
			| #out->replace('##NS##', '');
		
		iterate(self->elements, local('i'));				
			#data += #i->output;
		/iterate;
		
		#out->replace('##ELEMENTS##', #data);
		
		return(@#out);
	/define_tag;
/define_type;




define_type(
	'element',
	-namespace='paypal_',
	-prototype,
	-description='Simple type to hold method elements for PayPal web services requests.'
);
	local(
		'name' = string,
		'datatype' = string,
		'value' = string,
		'attributes' = array,
		'namespace' = '',
		'wrapper' = '<##ELNAME## ##NS## ##ATTS## xsi:type="##ELTYPE##">##ELVALUE##'
	);
	
	define_tag(
		'onCreate',
		-required='name',
		-type='string',
		-required='datatype',
		-type='string',
		-required='value',
		-optional='attributes',
		-type='array'
	);
		self->name = #name;
		self->datatype = #datatype;
		self->value = encode_xml(#value);
		local_defined('attributes') ? self->attributes = #attributes;
		local_defined('namespace') ? self->namespace = #namespace;
	/define_tag;
	
	define_tag('output');	
		local('out' = self->wrapper);
		#out->replace('##ELNAME##', self->name)
			&replace('##ELTYPE##', self->datatype)
			&replace('##ELVALUE##', self->value);
			
		if(self->attributes->size);
			local('atts' = string);
		
			iterate(self->attributes, local('a'));
				#atts += (#a->first + '="' + #a->second + '" ');
			/iterate;
			
			#out->replace('##ATTS##', #atts);
		else;
			#out->replace('##ATTS##', '');
		/if;
		
		#out->replace('##NS##', (self->namespace != '' ? 'xmlns="' + self->namespace + '"'| ''));
		
		return(@#out);	
	/define_tag;
/define_type;




define_type(
	'wrapper',
	-namespace='paypal_',
	-prototype,
	-description='A generic wrapper to create complex types for the PayPal WS/API.'
);
	local(
		'name' = string,
		'elements' = array,
		'namespace' = '',
		'datatype' = '',
		'wrapper' = '\
<##NAME## ##NS## ##TYPE##>
##ELEMENTS##

		'
	);
	
	define_tag(
		'onCreate',
		-required='name',
		-type='string',
		-required='elements',
		-type='array',
		-optional='namespace',
		-type='string',
		-optional='datatype',
		-type='string'
	);
		self->name = #name;
		self->elements = #elements;
		local_defined('namespace') ? self->namespace = #namespace;
		local_defined('datatype') ? self->datatype = #datatype;
	/define_tag;
	
	define_tag('output');
		local('out' = self->wrapper);
		#out->replace('##NAME##', self->name)
			&replace('##NS##', (self->namespace != '' ? 'xmlns="' + self->namespace + '"' | ''))
			&replace('##TYPE##', (self->datatype != '' ? 'xsi:type="' + self->datatype + '"' | ''));
		
		local('data' = string);
		
		iterate(self->elements, local('i'));
			#data += #i->output;
		/iterate;
		
		#out->replace('##ELEMENTS##', #data);
		
		return(@#out);
	/define_tag;
/define_type;



define_tag(
	'parseresponse',
	-namespace='paypal_',
	-priority='replace',
	-description='Returns a map of values from the XML response to a PayPal WS/API request.'
);	
	fail_if(
		!params->size || !params->first->isa('bytes'),
		-1,
		'[paypal_response] requires an xml bytes type as input.'
	);
	
	local(
		'in' = xml(string(params->first)),
		'out' = map
	);
	
	iterate(#in->extract('//*'), local('i'));
		#i->extract('text()')->size ? #out->insert(#i->extract('local-name()') = #i->extract('text()')->first);
	/iterate;
	
	return(#out);
/define_tag;



define_type(
	'address',
	-namespace='paypal_',
	-prototype,
	-description='Address type for the PayPal WS/API.'
);
	local(
		'Name' = string,
		'Street1' = string,
		'Street2' = string,
		'CityName' = string,
		'StateOrProvince' = string,
		'PostalCode' = string,
		'Country' = string,
		'AddressType' = 'Address'
	);

	define_tag(
		'onCreate',
		-optional='name',
		-required='street1',
		-optional='street2',
		-required='cityname',
		-required='stateorprovince',
		-required='postalcode',
		-required='country'
	);
		local_defined('name') ? self->name = #name;
		self->street1 = #street1;
		local_defined('street2') ? self->street2 = #street2;
		self->cityname = #cityname;
		self->stateorprovince = #stateorprovince;
		self->postalcode = #postalcode;
		self->country = #country;
	/define_tag;
	
	define_tag('output');
		local('elements' = array);
		
		iterate(self->properties->first->keys, local('i'));
			if(self->#i != '');
				local('datatype' = self->#i == 'country' ? 'ebl:CountryCodeType' | 'xs:string');
			
				#elements->insert(
					paypal_element(
						-name=#i,
						-datatype=#datatype,
						-value=self->#i
					)
				);
			/if;
		/iterate;
		
		local('out') = paypal_wrapper(
			-name=self->AddressType,
			-elements=#elements,
			-namespace='urn:ebay:apis:eBLBaseComponents',
			-datatype='ebl:AddressType'
		);
		
		return(@#out->output);
	/define_tag;
/define_type;




define_type(
	'paymentdetailsitem',
	-namespace='paypal_',
	-prototype,
	-description='Holds line item payment details for PayPal WS/API requests.'
);
	local(
		'Name' = string,
		'Amount' = decimal,
		'Number' = string,
		'Quantity' = string,
		'SalesTax' = string,
		'currencyID' = 'USD'
	);
	
	define_tag(
		'onCreate',
		-optional='Name',
		-optional='Amount',
		-optional='Number',
		-optional='Quantity',
		-optional='SalesTax',
		-optional='currencyID'
	);
		iterate(params, local('i'));
			local('pname' = #i->first);
			#pname->removeleading('-');
			
			if(#pname == 'Name');
				self->'Name' = #i->second;
			else;
				self->#pname = #i->second;
			/if;
		/iterate;
	/define_tag;
	
	define_tag('output');
		local('elements' = array);
		
		iterate(self->properties->first->keys, local('i'));
			if(self->#i != '');
				if(#i == 'Amount');
					local('datatype' = 'ebl:BasicAmountType');
					local('attributes') = array('currencyID' = self->currencyID);
				else;
					local('datatype' = 'xs:string');
					local('attributes' = null);
				/if;
			
				local('element') = paypal_element(
					-name=#i,
					-datatype=#datatype,
					-value=self->#i
				);

				#attributes->size ? #element->attributes = #attributes;				
				#elements->insert(#element);
			/if;
		/iterate;
		
		local('out') = paypal_wrapper(
			-name='PaymentDetailsItem',
			-elements=#elements
		);
		
		return(@#out->output);
	/define_tag;
/define_type;




define_type(
	'paymentdetails',
	-namespace='paypal_',
	-prototype,
	-description='Holds payment details for a PayPal WS/API request.'
);
	local(
		'OrderTotal' = decimal,
		'ItemTotal' = decimal,
		'ShippingTotal' = decimal,
		'HandlingTotal' = decimal,
		'TaxTotal' = decimal,
		'Order Description' = string,
		'Custom' = string,
		'InvoiceID' = string,
		'ButtonSource' = string,
		'NotifyURL' = string,
		'ShipToAddress' = null,
		'PaymentDetailsItem' = null,
		'currencyID' = 'USD'
	);
	
	define_tag(
		'onCreate',
		-required='OrderTotal',
		-optional='ItemTotal',
		-optional='ShippingTotal',
		-optional='HandlingTotal',
		-optional='TaxTotal',
		-optional='Order Description',
		-optional='Custom',
		-optional='InvoiceID',
		-optional='ButtonSource',
		-optional='NotifyURL',
		-optional='ShipToAddress',
		-optional='PaymentDetailsItem',
		-optional='currencyID'
	);
		iterate(params, local('i'));
			local('name' = #i->first);
			#name->removeleading('-');
			self->#name = #i->second;
		/iterate;		
	/define_tag;

	define_tag('output');
		local('elements' = array);
		
		local('amountTypes') = array(
			'OrderTotal',
			'ItemTotal',
			'ShippingTotal',
			'HandlingTotal',
			'TaxTotal'
		);
		
		local('complexTypes') = array(
			'ShipToAddress',
			'PaymentDetailsItem'
		);
		
		local(
			'datatype' = string,
			'attributes' = array
		);
		
		iterate(self->properties->first->keys, local('i'));
			if(
				(#complexTypes !>> #i)
				&& (self->#i != '') 
				&& (#i != 'currencyID')
			);
				if(#amountTypes >> #i);
					local('datatype' = 'ebl:BasicAmountType');
					local('attributes') = array('currencyID' = self->currencyID);
				else;
					local('datatype' = 'xs:string');
					local('attributes' = null);
				/if;
			
				local('element') = paypal_element(
					-name=#i,
					-datatype=#datatype,
					-value=self->#i
				);

				#attributes->size ? #element->attributes = #attributes;				
				#elements->insert(#element);				
			/if;
		/iterate;
		
		if(self->ShipToAddress != null);
			self->ShipToAddress->AddressType = 'ShipToAddress';
			#elements->insert(self->ShipToAddress);
		/if;		
		
		// this bit is acting funny
		if(self->PaymentDetailsItem->size > 0);
			#elements->merge(self->PaymentDetailsItem);
		/if;
		
		local('out') = paypal_wrapper(
			-name='PaymentDetails',
			-elements=#elements
		);
		
		return(@#out->output);
	/define_tag;
/define_type;




define_type(
	'payername',
	-namespace='paypal_',
	-prototype,
	-description='PayerName type for PayPal WS/API requests.'
);
	local(
		'Salutation' = string,
		'FirstName' = string,
		'MiddleName' = string,
		'LastName' = string,
		'Suffix' = string
	);
	
	define_tag(
		'onCreate',
		-optional='Salutation',
		-required='FirstName',
		-optional='MiddleName',
		-required='LastName',
		-optional='Suffix'
	);
		local_defined('Salutation') ? self->Salutation = #Salutation;
		self->FirstName = #FirstName;
		local_defined('MiddleName') ? self->MiddleName = #MiddleName;
		self->LastName = #LastName;
		local_defined('Suffix') ? self->Suffix = #Suffix;
	/define_tag;
	
	define_tag('output');
		local('elements' = array);
	
		self->Salutation != '' ? #elements->insert(
			paypal_element(
				-name='Salutation',
				-datatype='ns:PersonNameType',
				-value=self->Salutation
			)
		);
	
		#elements->insert(
			paypal_element(
				-name='FirstName',
				-datatype='ns:PersonNameType',
				-value=self->FirstName
			)
		);

		self->MiddleName != '' ? #elements->insert(
			paypal_element(
				-name='MiddleName',
				-datatype='ns:PersonNameType',
				-value=self->MiddleName
			)
		);

		#elements->insert(
			paypal_element(
				-name='LastName',
				-datatype='ns:PersonNameType',
				-value=self->LastName
			)
		);

		self->Suffix != '' ? #elements->insert(
			paypal_element(
				-name='Suffix',
				-datatype='ns:PersonNameType',
				-value=self->Suffix
			)
		);
		
		local('out') = paypal_wrapper(
			-name='PayerName',
			-elements=#elements
		);
		
		return(@#out->output);
	/define_tag;
/define_type;



define_type(
	'payerinfo',
	-namespace='paypal',
	-prototype,
	-description='The PayerInfo type for PayPal WS/API requests.'
);
	local(
		'Payer' = string,
		'PayerName' = null,
		'Address' = null
	);
	
	define_tag(
		'onCreate',
		-optional='Payer',
		-required='PayerName',
		-required='Address'
	);
		local_defined('Payer') ? self->Payer = #Payer;
		self->PayerName = #PayerName;
		self->Address = #Address;
	/define_tag;
	
	define_tag('output');
		local('elements' = array);
		
		self->Payer != '' ? #elements->insert(
			paypal_element(
				-name='Payer',
				-datatype='ns:EmailAddressType',
				-value=self->Payer
			)
		);
		
		#elements->insert(self->PayerName);
		#elements->insert(self->Address);
		
		local('out') = paypal_wrapper(
			-name='CardOwner',
			-elements=#elements
		);
		
		return(@#out->output);
	/define_tag;
/define_type;




define_type(
	'creditcard',
	-namespace='paypal_',
	-prototype,
	-description='The CreditCardDetailsType for PayPal WS/API requests.'
);
	local(
		'CreditCardType' = string,
		'CreditCardNumber' = string,
		'ExpMonth' = integer,
		'ExpYear' = integer,
		'CVV2' = string,
		'CardOwner' = null
	);
	
	define_tag(
		'onCreate',
		-required='CreditCardType',
		-required='CreditCardNumber',
		-required='ExpMonth',
		-required='ExpYear',
		-optional='CVV2',
		-required='CardOwner'
	);
		iterate(self->properties->first->keys, local('i'));
			local_defined(#i) ? self->#i = local(#i);
		/iterate;
	/define_tag;
	
	define_tag('output');
		local('elements' = array);
		
		local('datatypes') = map(
			'CreditCardType' = '',
			'CreditCardNumber' = 'xs:string',
			'ExpMonth' = 'xs:int',
			'ExpYear' = 'xs:int',
			'CVV2' = 'xs:string'
		);
		
		iterate(#datatypes->keys, local('i'));
			self->#i != '' ? #elements->insert(
				paypal_element(
					-name=#i,
					-datatype=#datatypes->find(#i),
					-value=self->#i
				)
			);
		/iterate;
		
		#elements->insert(self->CardOwner);
		
		local('out') = paypal_wrapper(
			-name='CreditCard',
			-elements=#elements
		);
		
		return(@#out->output);
	/define_tag;
/define_type;



define_tag(
	'transactionsearch',
	-namespace='paypal_',
	-priority='replace',
	-required='auth',
	-optional='currencyID',
	-required='StartDate',
	-type='date',
	-optional='EndDate',
	-type='date',
	-optional='Payer',
	-optional='Receiver',
	-optional='ReceiptID',
	-optional='TransactionID',
	-optional='InvoiceID',
	-optional='PayerName',
	-optional='AuctionItemNumber',
	-optional='TransactionClass',
	-optional='Amount',
	-optional='CurrencyCode',
	-optional='Status',
	-description='Sends a TransactionSearch request to PayPal WS/API and parses the response.'
);
	!local_defined('currencyID') ? local('currencyID' = 'USD');

	local('elements' = array);
	
	#elements->insert(
		paypal_element(
			-name='StartDate',
			-datatype='xs:dateTime',
			-value=date_localtogmt(#StartDate)->format('%Y-%m-%dT%H:%M:%SZ')
		)
	);
	
	local_defined('EndDate') ? #elements->insert(
		paypal_element(
			-name='EndDate',
			-datatype='xs:dateTime',
			-value=date_localtogmt(#EndDate)->format('%Y-%m-%dT%H:%M:%SZ')
		)
	);
	
	local_defined('Payer') ? #elements->insert(
		paypal_element(
			-name='Payer',
			-datatype='ebl:EmailAddressType',
			-value=#Payer
		)
	);

	local_defined('Receiver') ? #elements->insert(
		paypal_element(
			-name='Receiver',
			-datatype='ebl:EmailAddressType',
			-value=#Receiver
		)
	);

	local_defined('ReceiptID') ? #elements->insert(
		paypal_element(
			-name='ReceiptID',
			-datatype='xs:string',
			-value=#ReceiptID
		)
	);

	local_defined('TransactionID') ? #elements->insert(
		paypal_element(
			-name='TransactionID',
			-datatype='ebl:TransactionID',
			-value=#TransactionID
		)
	);

	local_defined('InvoiceID') ? #elements->insert(
		paypal_element(
			-name='InvoiceID',
			-datatype='xs:string',
			-value=#InvoiceID
		)
	);

	local_defined('PayerName') ? #elements->insert(#PayerName);

	local_defined('AuctionItemNumber') ? #elements->insert(
		paypal_element(
			-name='AuctionItemNumber',
			-datatype='xs:string',
			-value=#AuctionItemNumber
		)
	);

	local_defined('TransactionClass') ? #elements->insert(
		paypal_element(
			-name='TransactionClass',
			-datatype='ebl:PaymentTransactionClassCodeType',
			-value=#TransactionClass
		)
	);

	local_defined('Amount') ? #elements->insert(
		paypal_element(
			-name='Amount',
			-datatype='ebl:BasicAmountType',
			-value=#Amount,
			-attributes=array('currencyID' = #currencyID)
		)
	);

	local_defined('CurrencyCode') ? #elements->insert(
		paypal_element(
			-name='CurrencyCode',
			-datatype='xs:token',
			-value=#CurrencyCode
		)
	);

	local_defined('Status') ? #elements->insert(
		paypal_element(
			-name='Status',
			-datatype='ebl:PaymentTransactionStatusCodeType',
			-value=#Status
		)
	);

	local('request') = paypal_request(
		-auth=#auth,
		-request=paypal_method(
			-method='TransactionSearch',
			-elements=#elements
		)
	);
	
	local('out') = paypal_parseresponse(#request->send);
	// local('out') = #request->output;
	// local('out') = #request->send;

	return(#out);
/define_tag;

Related Tags

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft