sunnysideup/afterpay

Integrate easily with the api.afterpay.com API

Installs: 357

Dependents: 0

Suggesters: 1

Security: 0

Stars: 1

Watchers: 4

Forks: 0

Open Issues: 0

Type:silverstripe-vendormodule

2.1.1 2023-10-06 04:26 UTC

This package is auto-updated.

Last update: 2024-11-27 08:59:42 UTC


README

Provides a basic implementation of afterpay as a payment method for sunnysideup/ecommerce - using https://github.com/culturekings/afterpay as a base.

installation

Use composer

composer require sunnysideup/afterpay

setup

Add the following code as outlined below:

  1. set up credentials

app/_config/afterpay.yml:

---
Name: Afterpay
---

Sunnysideup\Afterpay\Factory\SilverstripeMerchantApi:
  merchant_name: 'my merchant name'
  merchant_id: yyy
  secret_key: 'xxx'
  expectations_folder: 'app/afterpay/configurations'
  number_of_payments: 4
  1. set afterpay as a payment option

app/_config/ecommerce.yml:

EcommercePayment:
  supported_methods:
    # ...
    Sunnysideup\Afterpay\Model\AfterpayEcommercePayment: "Afterpay"
  1. add fields to EcomConfig (via data extension or otherwise)
    private static $db = [
        'ShowAfterpayOption' => 'Boolean',
        'AfterpayMinValue' => 'Int',
        'AfterpayMaxValue' => 'Int',
    ]
  1. add functionality to product:

app/src/Model/MyProduct.php:

use Sunnysideup\Afterpay\Factory\SilverstripeMerchantApi;

class MyProduct extends Product
{
    public function ShowAfterpay() : bool
    {
        return return $this->myAfterpayApi()->canProcessPayment($this->CalculatedPrice());
    }

    protected function myAfterpayApi() : SilverstripeMerchantApi
    {
        return SilverstripeMerchantApi::inst()
            ->setMinAndMaxPrice(
                (float) EcommerceConfig::inst()->AfterpayMinValue,
                (float) EcommerceConfig::inst()->AfterpayMaxValue
            )
            ->setIsServerAvailable(EcommerceConfig::inst()->ShowAfterpayOption);
    }

    public function getAfterpayNumberOfPayments() : int
    {
        return $this->myAfterpayApi()
            ->getNumberOfPayments();
    }

    public function getAfterpayNumberOfPaymentsInWeeks() : int
    {
        return $this->getAfterpayNumberOfPayments() * 2;
    }

    public function getAfterpayAmountPerPayment() :float
    {
        return $this->myAfterpayApi()
            ->getAmountPerPayment($this->CalculatedPrice());
    }

    public function getAfterpayAmountPerPaymentAsMoney() : Money
    {
        return EcommerceCurrency::get_money_object_from_order_currency(
            $this->getAfterpayAmountPerPayment()
        );
    }

    public function getAfterpayAmountPerPaymentAsCurrency(): Currency
    {
        return DBField::create_field('Currency', $this->getAfterpayAmountPerPayment());
    }


}