Skip to content
Permalink
Browse files

Add support for DoReferenceTransactionRequest to PayPal Express API;

Allow using BAID recieved from DoExpressCheckoutPaymentRequest (after setting up the transaction by passing BillingAgreementDetails into SetExpressCheckoutRequest).
  • Loading branch information...
kenmazaika committed Feb 29, 2012
1 parent cabef7d commit d20c79cfce5318e8e05b0d0a47a5b45671e404ba
@@ -8,7 +8,7 @@ def self.included(base)
base.cattr_accessor :signature
end

API_VERSION = '62.0'
API_VERSION = '72'

URLS = {
:test => { :certificate => 'https://api.sandbox.paypal.com/2.0/',
@@ -41,6 +41,12 @@ def purchase(money, options = {})
commit 'DoExpressCheckoutPayment', build_sale_or_authorization_request('Sale', money, options)
end

def reference_transaction(money, options = {})
requires!(options, :reference_id, :payment_type, :invoice_id, :description, :ip)

commit 'DoReferenceTransaction', build_reference_transaction_request('Sale', money, options)
end

private
def build_get_details_request(token)
xml = Builder::XmlMarkup.new :indent => 2
@@ -172,6 +178,32 @@ def build_setup_request(action, money, options)
xml.target!
end

def build_reference_transaction_request(action, money, options)
currency_code = options[:currency] || currency(money)

xml = Builder::XmlMarkup.new :indent => 2
xml.tag! 'DoReferenceTransactionReq', 'xmlns' => PAYPAL_NAMESPACE do
xml.tag! 'DoReferenceTransactionRequest', 'xmlns:n2' => EBAY_NAMESPACE do
xml.tag! 'n2:Version', API_VERSION
xml.tag! 'n2:DoReferenceTransactionRequestDetails' do
xml.tag! 'n2:ReferenceID', options[:reference_id]
xml.tag! 'n2:PaymentAction', action
xml.tag! 'n2:PaymentType', options[:payment_type] || 'Any'
xml.tag! 'n2:PaymentDetails' do
xml.tag! 'n2:OrderTotal', amount(money).to_f.zero? ? localized_amount(100, currency_code) : localized_amount(money, currency_code), 'currencyID' => currency_code
xml.tag! 'n2:OrderDescription', options[:description]
xml.tag! 'n2:InvoiceID', options[:invoice_id]
xml.tag! 'n2:ButtonSource', 'ActiveMerchant'
xml.tag! 'n2:NotifyURL', ''
end
xml.tag! 'n2:IPAddress', options[:ip]
end
end
end

xml.target!
end

def build_response(success, message, response, options = {})
PaypalExpressResponse.new(success, message, response, options)
end
@@ -327,6 +327,57 @@ def test_items_are_included_if_specified_in_build_sale_or_authorization_request
assert_equal '2', REXML::XPath.match(xml, '//n2:PaymentDetails/n2:PaymentDetailsItem/n2:Number')[1].text
assert_equal '4', REXML::XPath.match(xml, '//n2:PaymentDetails/n2:PaymentDetailsItem/n2:Quantity')[1].text
end

def test_build_reference_transaction_test
xml = REXML::Document.new(@gateway.send(:build_reference_transaction_request, 'Sale', 2000, {
:reference_id => "ref_id",
:payment_type => 'Any',
:invoice_id => 'invoice_id',
:description => 'Description',
:ip => '127.0.0.1' }))

assert_equal '72', REXML::XPath.first(xml, '//DoReferenceTransactionReq/DoReferenceTransactionRequest/n2:Version').text
assert_equal 'ref_id', REXML::XPath.first(xml, '//DoReferenceTransactionReq/DoReferenceTransactionRequest/n2:DoReferenceTransactionRequestDetails/n2:ReferenceID').text
assert_equal 'Sale', REXML::XPath.first(xml, '//DoReferenceTransactionReq/DoReferenceTransactionRequest/n2:DoReferenceTransactionRequestDetails/n2:PaymentAction').text
assert_equal 'Any', REXML::XPath.first(xml, '//DoReferenceTransactionReq/DoReferenceTransactionRequest/n2:DoReferenceTransactionRequestDetails/n2:PaymentType').text
assert_equal '20.00', REXML::XPath.first(xml, '//DoReferenceTransactionReq/DoReferenceTransactionRequest/n2:DoReferenceTransactionRequestDetails/n2:PaymentDetails/n2:OrderTotal').text
assert_equal 'Description', REXML::XPath.first(xml, '//DoReferenceTransactionReq/DoReferenceTransactionRequest/n2:DoReferenceTransactionRequestDetails/n2:PaymentDetails/n2:OrderDescription').text
assert_equal 'invoice_id', REXML::XPath.first(xml, '//DoReferenceTransactionReq/DoReferenceTransactionRequest/n2:DoReferenceTransactionRequestDetails/n2:PaymentDetails/n2:InvoiceID').text
assert_equal 'ActiveMerchant', REXML::XPath.first(xml, '//DoReferenceTransactionReq/DoReferenceTransactionRequest/n2:DoReferenceTransactionRequestDetails/n2:PaymentDetails/n2:ButtonSource').text
assert_equal '127.0.0.1', REXML::XPath.first(xml, '//DoReferenceTransactionReq/DoReferenceTransactionRequest/n2:DoReferenceTransactionRequestDetails/n2:IPAddress').text
end

def test_reference_transaction
@gateway.expects(:ssl_post).returns(successful_reference_transaction_response)

response = @gateway.reference_transaction(2000, {
:reference_id => "ref_id",
:payment_type => 'Any',
:invoice_id => 'invoice_id',
:description => 'Description',
:ip => '127.0.0.1' })

assert_equal "Success", response.params['ack']
assert_equal "Success", response.message
assert_equal "9R43552341412482K", response.authorization
end

def test_reference_transaction_requires_fields
valid_options = {
:reference_id => "ref_id",
:payment_type => 'Any',
:invoice_id => 'invoice_id',
:description => 'Description',
:ip => '127.0.0.1' }

[:reference_id, :payment_type, :invoice_id, :description, :ip].each do |field|
options = valid_options.dup
options.delete(field)
assert_raise ArgumentError do
@gateway.reference_transaction(2000, options)
end
end
end

def test_error_code_for_single_error
@gateway.expects(:ssl_post).returns(response_with_error)
@@ -433,6 +484,55 @@ def test_structure_correct
end

private
def successful_reference_transaction_response
<<-RESPONSE
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cc="urn:ebay:apis:CoreComponentTypes" xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext" xmlns:ed="urn:ebay:apis:EnhancedDataTypes" xmlns:ebl="urn:ebay:apis:eBLBaseComponents" xmlns:ns="urn:ebay:api:PayPalAPI">
<SOAP-ENV:Header>
<Security xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext" xsi:type="wsse:SecurityType"></Security>
<RequesterCredentials xmlns="urn:ebay:api:PayPalAPI" xsi:type="ebl:CustomSecurityHeaderType">
<Credentials xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:UserIdPasswordType">
<Username xsi:type="xs:string"></Username>
<Password xsi:type="xs:string"></Password>
<Signature xsi:type="xs:string">OMGOMGOMG</Signature>
<Subject xsi:type="xs:string"></Subject>
</Credentials>
</RequesterCredentials>
</SOAP-ENV:Header>
<SOAP-ENV:Body id="_0">
<DoReferenceTransactionResponse xmlns="urn:ebay:api:PayPalAPI">
<Timestamp xmlns="urn:ebay:apis:eBLBaseComponents">2011-05-23T21:36:32Z</Timestamp>
<Ack xmlns="urn:ebay:apis:eBLBaseComponents">Success</Ack>
<CorrelationID xmlns="urn:ebay:apis:eBLBaseComponents">4d6d3af55369b</CorrelationID>
<Version xmlns="urn:ebay:apis:eBLBaseComponents">72</Version>
<Build xmlns="urn:ebay:apis:eBLBaseComponents">1863577</Build>
<DoReferenceTransactionResponseDetails xmlns="urn:ebay:apis:eBLBaseComponents" xsi:type="ebl:DoReferenceTransactionResponseDetailsType">
<BillingAgreementID xsi:type="xs:string">B-3R788221G4476823M</BillingAgreementID>
<PaymentInfo xsi:type="ebl:PaymentInfoType">
<TransactionID>9R43552341412482K</TransactionID>
<ParentTransactionID xsi:type="ebl:TransactionId"></ParentTransactionID>
<ReceiptID></ReceiptID>
<TransactionType xsi:type="ebl:PaymentTransactionCodeType">mercht-pmt</TransactionType>
<PaymentType xsi:type="ebl:PaymentCodeType">instant</PaymentType>
<PaymentDate xsi:type="xs:dateTime">2011-05-23T21:36:32Z</PaymentDate>
<GrossAmount xsi:type="cc:BasicAmountType" currencyID="USD">190.00</GrossAmount>
<FeeAmount xsi:type="cc:BasicAmountType" currencyID="USD">5.81</FeeAmount>
<TaxAmount xsi:type="cc:BasicAmountType" currencyID="USD">0.00</TaxAmount>
<ExchangeRate xsi:type="xs:string"></ExchangeRate>
<PaymentStatus xsi:type="ebl:PaymentStatusCodeType">Completed</PaymentStatus>
<PendingReason xsi:type="ebl:PendingStatusCodeType">none</PendingReason>
<ReasonCode xsi:type="ebl:ReversalReasonCodeType">none</ReasonCode>
<ProtectionEligibility xsi:type="xs:string">Ineligible</ProtectionEligibility>
<ProtectionEligibilityType xsi:type="xs:string">None</ProtectionEligibilityType>
</PaymentInfo>
</DoReferenceTransactionResponseDetails>
</DoReferenceTransactionResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
RESPONSE
end


def successful_details_response
<<-RESPONSE
<?xml version="1.0" encoding="UTF-8"?>

0 comments on commit d20c79c

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.