# Backend de validation des achats In-App Google Play (Stimart)

Backend **PHP** hébergé sur https://android.dynseo.com/ qui valide les achats In-App
Google Play des apps Stimart (abonnements et consommables) puis transmet l'achat validé
au serveur shop de Dynseo. Le contrat d'entrées/sorties est **identique au backend iOS**
(apple.dynseo.com) : seule la couche de vérification Apple est remplacée par la
vérification Google Play (Google Play Developer API, androidpublisher v3).

## Apps gérées

| Code app | Package Android |
|----------|------------------|
| EHPAD | `com.dynseo.stimart.edith` |
| PAPY | `com.dynseo.stimart.joe` |
| COCO | `com.dynseo.stimart.coco` |

## Structure

```
android-inapp/
├── composer.json                       # dépend de google/apiclient
├── htdocs/                             # racine web sur android.dynseo.com
│   ├── validPayment.php                # abonnements → shop.dynseo.com
│   ├── validPaymentTest.php            # abonnements → testshop.dynseo.com
│   ├── validConsumable.php             # consommables → shop.dynseo.com
│   ├── validConsumableTest.php         # consommables → testshop.dynseo.com
│   ├── googlePlayNotifications.php     # webhook RTDN (Pub/Sub)
│   └── lib/
│       ├── config.php                  # mapping apps + constantes à confirmer
│       ├── exceptions.php              # PurchaseError, GooglePlayException
│       ├── interfaces.php              # GooglePlay/Shop/Mailer/Logger
│       ├── handlers.php                # handleSubscription, handleConsumable
│       ├── notifications.php           # decodeRtdn
│       ├── GooglePlay.php              # adapter google/apiclient
│       └── io.php                      # CurlShopClient, PhpMailer, FileLogger
├── credentials/
│   └── service-account.json            # clé SA partagée (gitignored)
└── tests/                              # suite PHPUnit
```

## Endpoints

| Endpoint | Rôle |
|----------|------|
| `validPayment.php` | Abonnements → shop.dynseo.com |
| `validPaymentTest.php` | Abonnements → testshop.dynseo.com |
| `validConsumable.php` | Consommables → shop.dynseo.com |
| `validConsumableTest.php` | Consommables → testshop.dynseo.com |
| `googlePlayNotifications.php` | Webhook RTDN (Pub/Sub) |

## Installation

```bash
composer install
```

Déposer la clé du service account dans `credentials/service-account.json` (voir
[credentials/README.md](credentials/README.md)). Configurer les valeurs « à confirmer »
dans [htdocs/lib/config.php](htdocs/lib/config.php) (deviceType, nom du champ token, casse selling).

## Tests

```bash
vendor/bin/phpunit
```

## Prérequis Google Cloud

1. **API Google Play Android Developer** activée sur le projet GCP.
2. **Compte de service** ayant accès aux 3 apps Stimart dans la Play Console (scope `androidpublisher`).
3. **Topic Pub/Sub** configuré pour les RTDN (notifications en temps réel).
4. **Subscription Push** pointant vers `https://android.dynseo.com/googlePlayNotifications.php`.

## Points à confirmer avec l'équipe shop

1. `deviceType` attendu pour Android (`A` ?).
2. Nom du champ token (`purchaseToken` vs `receipt`/`transactionId`).
3. Casse `SELLING_TYPE` (abos) vs `sellingType` (consommables).
4. Service account couvrant les 3 packages Stimart.
