Skip to content

Commit

Permalink
WIP: Allow for creation of recurring and adhoc subscriptions (#15)
Browse files Browse the repository at this point in the history
* WIP: Start work on getting subscription billing working

* When creating an adhoc subscription don't pass recurring settings over

* [security] Don't send over the passphrase as part of the request as it allows bad actors to change the amount being billed, etc.

* Reorder and make notes.  Email confirmation needs to be after the custom_xxx items
  • Loading branch information
jacques authored and barryvdh committed Feb 2, 2018
1 parent 21741c7 commit c3ff0de
Showing 1 changed file with 109 additions and 4 deletions.
113 changes: 109 additions & 4 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,66 @@ public function setCustomInt5($value)
return $this->setParameter('customInt5', $value);
}

public function getPaymentMethod()
{
return $this->getParameter('paymentMethod');
}

public function setPaymentMethod($value)
{
return $this->setParameter('paymentMethod', $value);
}

public function getSubscriptionType()
{
return $this->getParameter('subscriptionType');
}

public function getBillingDate()
{
return $this->getParameter('billingDate');
}

public function getRecurringAmount()
{
return $this->getParameter('recurringAmount');
}

public function getFrequency()
{
return $this->getParameter('frequency');
}

public function getCycles()
{
return $this->getParameter('cycles');
}

public function setSubscriptionType($value)
{
return $this->setParameter('subscriptionType', $value);
}

public function setBillingDate($value)
{
return $this->setParameter('billingDate', $value);
}

public function setRecurringAmount($value)
{
return $this->setParameter('recurringAmount', $value);
}

public function setFrequency($value)
{
return $this->setParameter('frequency', $value);
}

public function setCycles($value)
{
return $this->setParameter('cycles', $value);
}

public function getData()
{
$this->validate('amount', 'description');
Expand Down Expand Up @@ -182,8 +242,30 @@ public function getData()
$data['custom_str4'] = $this->getCustomStr4();
$data['custom_str5'] = $this->getCustomStr5();

/**
* Allow overriding the Payment Method
*/
if ($this->getPaymentMethod()) {
$data['payment_method'] = $this->getPaymentMethod();
}

/**
* Subscription billing options.
*/
if (1 == $this->getSubscriptionType()) {
$data['subscription_type'] = $this->getSubscriptionType();
$data['billing_date'] = $this->getBillingDate();
$data['recurring_amount'] = $this->getRecurringAmount();
$data['frequency'] = $this->getFrequency();
$data['cycles'] = $this->getCycles();
}
if (2 == $this->getSubscriptionType()) {
$data['subscription_type'] = $this->getSubscriptionType();
}

$data['passphrase'] = $this->getParameter('passphrase');
$data['signature'] = $this->generateSignature($data);
unset($data['passphrase']);

return $data;
}
Expand All @@ -193,11 +275,34 @@ protected function generateSignature($data)
$fields = array();

// specific order required by PayFast
// @see https://developers.payfast.co.za/documentation/#checkout-page
foreach (array('merchant_id', 'merchant_key', 'return_url', 'cancel_url', 'notify_url', 'name_first',
'name_last', 'email_address', 'm_payment_id', 'amount', 'item_name', 'item_description',
'email_confirmation', 'confirmation_address', 'custom_int1', 'custom_int2', 'custom_int3',
'custom_int4', 'custom_int5', 'custom_str1', 'custom_str2', 'custom_str3', 'custom_str4',
'custom_str5', 'passphrase') as $key) {
'name_last', 'email_address', 'cell_number',
/**
* Transaction Details
*/
'm_payment_id', 'amount', 'item_name', 'item_description',
/**
* Custom return data
*/
'custom_int1', 'custom_int2', 'custom_int3', 'custom_int4', 'custom_int5',
'custom_str1', 'custom_str2', 'custom_str3', 'custom_str4', 'custom_str5',
/**
* Email confirmation
*/
'email_confirmation', 'confirmation_address',
/**
* Payment Method
*/
'payment_method',
/**
* Subscriptions
*/
'subscription_type', 'billing_date', 'recurring_amount', 'frequency', 'cycles',
/**
* Passphrase for md5 signature generation
*/
'passphrase') as $key) {
if (!empty($data[$key])) {
$fields[$key] = $data[$key];
}
Expand Down

0 comments on commit c3ff0de

Please sign in to comment.