Skip to content

Commit d580f7e

Browse files
Add handling for creating a token to rebill a card when card not… present
This scenario occurs when the pay with paypal button is clicked and authentication is done offsite
1 parent 2d407fd commit d580f7e

File tree

1 file changed

+60
-26
lines changed

1 file changed

+60
-26
lines changed

src/Message/RestCreateCardRequest.php

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -72,39 +72,73 @@ class RestCreateCardRequest extends AbstractRestRequest
7272
{
7373
public function getData()
7474
{
75-
$this->validate('card');
76-
$this->getCard()->validate();
75+
if ($this->isCardPresent()) {
76+
$this->validate('card');
77+
$this->getCard()->validate();
7778

78-
$data = array(
79-
'number' => $this->getCard()->getNumber(),
80-
'type' => $this->getCard()->getBrand(),
81-
'expire_month' => $this->getCard()->getExpiryMonth(),
82-
'expire_year' => $this->getCard()->getExpiryYear(),
83-
'cvv2' => $this->getCard()->getCvv(),
84-
'first_name' => $this->getCard()->getFirstName(),
85-
'last_name' => $this->getCard()->getLastName(),
86-
'billing_address' => array(
87-
'line1' => $this->getCard()->getAddress1(),
88-
//'line2' => $this->getCard()->getAddress2(),
89-
'city' => $this->getCard()->getCity(),
90-
'state' => $this->getCard()->getState(),
91-
'postal_code' => $this->getCard()->getPostcode(),
92-
'country_code' => strtoupper($this->getCard()->getCountry()),
93-
)
94-
);
79+
$data = array(
80+
'number' => $this->getCard()->getNumber(),
81+
'type' => $this->getCard()->getBrand(),
82+
'expire_month' => $this->getCard()->getExpiryMonth(),
83+
'expire_year' => $this->getCard()->getExpiryYear(),
84+
'cvv2' => $this->getCard()->getCvv(),
85+
'first_name' => $this->getCard()->getFirstName(),
86+
'last_name' => $this->getCard()->getLastName(),
87+
'billing_address' => array(
88+
'line1' => $this->getCard()->getAddress1(),
89+
//'line2' => $this->getCard()->getAddress2(),
90+
'city' => $this->getCard()->getCity(),
91+
'state' => $this->getCard()->getState(),
92+
'postal_code' => $this->getCard()->getPostcode(),
93+
'country_code' => strtoupper($this->getCard()->getCountry()),
94+
)
95+
);
9596

96-
// There's currently a quirk with the REST API that requires line2 to be
97-
// non-empty if it's present. Jul 14, 2014
98-
$line2 = $this->getCard()->getAddress2();
99-
if (!empty($line2)) {
100-
$data['billing_address']['line2'] = $line2;
97+
// There's currently a quirk with the REST API that requires line2 to be
98+
// non-empty if it's present. Jul 14, 2014
99+
$line2 = $this->getCard()->getAddress2();
100+
if (!empty($line2)) {
101+
$data['billing_address']['line2'] = $line2;
102+
}
103+
} else {
104+
// We are creating a token to rebill a paypal account.
105+
// this equates to the meaning of 'createCard' in other processors
106+
// such as Stripe.
107+
// https://developer.paypal.com/docs/limited-release/reference-transactions/#create-billing-agreement
108+
$data = ["description" => $this->getDescription(),
109+
"payer" => ["payment_method" => "PAYPAL"],
110+
"plan" =>
111+
[
112+
"type" => "MERCHANT_INITIATED_BILLING",
113+
"merchant_preferences" =>
114+
[
115+
"return_url" => $this->getReturnUrl(),
116+
"cancel_url" => $this->getCancelUrl(),
117+
"notify_url" => $this->getNotifyUrl(),
118+
"accepted_pymt_type" => "INSTANT",
119+
"skip_shipping_address" => true,
120+
"immutable_shipping_address" => false,
121+
]
122+
]
123+
];
101124
}
102-
103125
return $data;
104126
}
105127

106128
protected function getEndpoint()
107129
{
108-
return parent::getEndpoint() . '/vault/credit-cards';
130+
if ($this->isCardPresent()) {
131+
return parent::getEndpoint() . '/vault/credit-cards';
132+
} else {
133+
return parent::getEndpoint() . '/billing-agreements/agreement-tokens';
134+
}
135+
}
136+
137+
/**
138+
* Is the card present (or are we dealing with a paypal account transaction)
139+
*/
140+
protected function isCardPresent()
141+
{
142+
return $this->getCard()->getNumber();
109143
}
110144
}

0 commit comments

Comments
 (0)